TA80 INTG050 GosuAndXML
TA80 INTG050 GosuAndXML
This lesson uses the notes section for additional explanation and information.
To view the notes in PowerPoint, select View Normal or View Notes Page.
When printing notes, select Note Pages and Print hidden slides.
© Guidewire Software, Inc. 2001-2014. All rights reserved. Do not distribute without permission. 2
Lesson outline
• Overview of XML
© Guidewire Software, Inc. 2001-2014. All rights reserved. Do not distribute without permission. 3
XML
1 <?xml version="1.0"?>
2 <config xmlns="http://guidewire.com/config">
3 <param name="CurrencyURL" value="">http://acme.x.com</param>
4 <database name="TADatabase" dbtype="h2" autoupgrade="true">
5 <param name="jdbcURL" value="jdbc:h2:file:ta/db/ta"/>
6 <param name="jdbcURLtest" value="jdbc:h2:file:ta/db/test"/>
7 <param name="stmtPool.enabled" value=" false"/>
8 </database>
9 <security sessiontimeoutsecs="10800"/>
…
636 </config>
© Guidewire Software, Inc. 2001-2014. All rights reserved. Do not distribute without permission. 5
XML namespaces
1 <?xml version="1.0"?>
2 <config xmlns="http://guidewire.com/config
xmlns:ta="http://guidewire.com/configTA">
…
636 </config>
© Guidewire Software, Inc. 2001-2014. All rights reserved. Do not distribute without permission. 6
XSDs
1 <?xml version="1.0" encoding="UTF-8"?>
2 <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" … >
…4 <xsd:element name="config" >
5 <xsd:complexType>
6 <xsd:choice minOccurs="1" maxOccurs="unbounded">
…8 <xsd:element name="param"
minOccurs="0" maxOccurs="unbounded"/>
12 </xsd:choice>
13 </xsd:complexType>
14 </xsd:element>
…90 <xsd:schema/>
© Guidewire Software, Inc. 2001-2014. All rights reserved. Do not distribute without permission. 7
Untyped and strongly typed XML
Untyped Strongly typed
© Guidewire Software, Inc. 2001-2014. All rights reserved. Do not distribute without permission. 8
Serialization
01010010
<> serialization
01011010
11001001
deserialization <>
01100100
file.xml file-clone.xml
© Guidewire Software, Inc. 2001-2014. All rights reserved. Do not distribute without permission. 9
Lesson outline
• Overview of XML
© Guidewire Software, Inc. 2001-2014. All rights reserved. Do not distribute without permission. 10
XML and Gosu
file.xml
c file.xml
<> <>
© Guidewire Software, Inc. 2001-2014. All rights reserved. Do not distribute without permission. 11
XMLNode and XmlElement
XMLNode XmlElement
© Guidewire Software, Inc. 2001-2014. All rights reserved. Do not distribute without permission. 12
The XmlElement class
1 <?xml version="1.0"?>
2 <config xmlns="http://guidewire.com/config">
3 <param name="CurrencyURL" value="">http://acme.x.com</param>
4 <database name="TADatabase" dbtype="h2" autoupgrade="true">
<config…>
• Represent elements from
XML
• Import XML
<param…>
• Get information about each
element
<database…>
• Create and modify elements
• Export XML
© Guidewire Software, Inc. 2001-2014. All rights reserved. Do not distribute without permission. 13
The QName property
1 <?xml version="1.0"?>
2 <config xmlns="http://guidewire.com/config">
3 <param name="CurrencyURL" value="">http://acme.x.com</param>
4 <database name="TADatabase" dbtype="h2" autoupgrade="true">
QName: {http://guidewire.com/config}config
QName: {http://guidewire.com/config}param
QName: {http://guidewire.com/config}database
QName: {http://guidewire.com/config}config
Attributes: xmlns (http://guidewire.com/config)
Text:
QName: {http://guidewire.com/config}param
Attributes: name (CurrencyURL)
Text: http://acme.x.com
QName: {http://guidewire.com/config}database
Attributes: name (TADatabase), dbtype(h2), autoupgrade(true)
Text:
© Guidewire Software, Inc. 2001-2014. All rights reserved. Do not distribute without permission. 15
XML simple values
<param… >
value: 20141017
logic: serialization
© Guidewire Software, Inc. 2001-2014. All rights reserved. Do not distribute without permission. 16
Lesson outline
• Overview of XML
© Guidewire Software, Inc. 2001-2014. All rights reserved. Do not distribute without permission. 17
Parse XML without an XSD
6 uses gw.xml.XmlElement
7 uses java.io.File
…14 var filePathInput = "…/examples/xml/exampleUntyped.xml"
…16 var file = new File(filePathInput)
…19 var xml = XmlElement.parse(file)
…22 xml.print()
<?xml version="1.0"?>
<config xmlns="http://guidewire.com/config">
<param name="CurrencyURL" value="">http://acme.x.com</param>
<database name="TADatabase" dbtype="h2" autoupgrade="true">
<param name="jdbcURL" value="jdbc:h2:file:ta/db/ta"/>
• XmlElement.parse()
- Parse XML data into a graph of XmlElement objects without an XSD
- Input can be byte[], java.io.File, java.io.InputStream, java.io.Reader,
or String
© Guidewire Software, Inc. 2001-2014. All rights reserved. Do not distribute without permission. 18
Accessing element properties
…19 var xml = XmlElement.parse(file)
…25 print("QName: " + xml.QName)
26 print("Namespace: " + xml.QName.NamespaceURI)
27 print("Local part: " + xml.QName.LocalPart)
QName: {http://guidewire.com/config}config
Namespace: http://guidewire.com/config
Local part: config
{http://guidewire.com/config}param
{http://guidewire.com/config}database
{http://guidewire.com/config}param
© Guidewire Software, Inc. 2001-2014. All rights reserved. Do not distribute without permission. 20
Accessing children by name
…19 var xml = XmlElement.parse(file)
…36 print("Text of database element: " +
37 xml.getChild(new QName("http://guidewire.com/config",
"database")).Text)
38 print("Number of param elements in file: " +
39 xml.getChildren(new QName("http://guidewire.com/config",
"param")).Count)
[Element {http://guidewire.com/config}database]
Element {http://guidewire.com/config}database
Element {http://guidewire.com/config}database
© Guidewire Software, Inc. 2001-2014. All rights reserved. Do not distribute without permission. 22
Accessing attributes and values
…51 var dbElement = xml.Children.singleWhere(\el ->
el.QName.LocalPart == "database"))
52 print("Attributes: " + dbElement.AttributesNames)
53 print("---------------------------------------")
54 for (attrib in dbElement.AttributeNames) {
55 print(attrib + ": " + dbElement.getAttributeSimpleValue(attrib))
56 }
• AttributeNames
- property returns a set of attribute names for the element
• getAttributeSimpleValue()
- returns a simple value for an attribute
© Guidewire Software, Inc. 2001-2014. All rights reserved. Do not distribute without permission. 23
Accessing element contents
…19 var xml = XmlElement.parse(file)
…58 print(xml.Children[0].QName)
59 print(xml.Children[0].Text)
60 print(xml.Children[0].Children)
61 print("---------------------------------------")
62 print(xml.Children[1].QName)
63 print(xml.Children[1].Text)
64 print(xml.Children[1].Children)
© Guidewire Software, Inc. 2001-2014. All rights reserved. Do not distribute without permission. 24
Lesson outline
• Overview of XML
© Guidewire Software, Inc. 2001-2014. All rights reserved. Do not distribute without permission. 25
Modifying an existing XmlElement
…51 var dbElement = xml.Children.singleWhere(\el ->
el.QName.LocalPart == "database"))
…67 print("Original Value: "
+ dbElement.getAttributeSimpleValue("autoupgrade"))
68 dbElement.setAttributeSimpleValue("autoupgrade","false")
…69 print("Current Value: "
+ dbElement.getAttributeSimpleValue("autoupgrade"))
© Guidewire Software, Inc. 2001-2014. All rights reserved. Do not distribute without permission. 26
Creating new XmlElements (1)
• Syntax to create an XmlElement:
new XmlElement(QName)
• Syntax to create a QName:
new QName(namespaceURI, localPart, prefix)
new QName(namespaceURI, localPart)
new QName(localPart)
• Syntax to create an set attribute values
xmlElement.setAttributeValue("name","value"
)
• Syntax to add child XmlElement to existing XmlElement
xmlElement.addChild(aXmlElement)
© Guidewire Software, Inc. 2001-2014. All rights reserved. Do not distribute without permission. 27
Creating new XmlElements (2)
A new
• …6 element is not part of a given XML hierarchy until it
uses gw.xml.XmlElement
…9 uses javax.xml.namespace.QName
is added as a child of an element in the hierarchy
72 var claimSystem = new XmlElement(new QName(
"http://guidewire.com/config", "claimSystem"))
73 claimSystem.setAttributeValue("URL",
"http://localhost:8880/ab/ContactManager.do")
…75 var claimSystemUser = new XmlElement(new QName(
"http://guidewire.com/config", "param"))
76 claimSystemUser.setAttributeValue("name", "user")
77 claimSystemUser.setAttributeValue("value", "su")
…79 var claimSystemPassword = new XmlElement(new QName(
"http://guidewire.com/config", "param"))
80 claimSystemPassword.setAttributeValue("name", "password")
81 claimSystemPassword.setAttributeValue("value", "gw")
82
83 claimSystem.addChild(claimSystemUser)
84 claimSystem.addChild(claimSystemPassword)
85 xml.addChild(claimSystem)
© Guidewire Software, Inc. 2001-2014. All rights reserved. Do not distribute without permission. 28
Exporting XML
…6 uses gw.xml.XmlElement
7 uses java.io.File
…10 uses java.io.FileWriter
…11 uses java.io.BufferedWriter
…15 var filePathOutput = "…/examples/xml/exampleUntyped_Modified.xml"
…88 var outputWriter = new BufferedWriter(new FileWriter(
new File(filePathOutput)))
89 outputWriter.write(xml.asUTFString())
90 outputWriter.close()
• asUTFString() method
- Serializes the element to a
String object in UTF-8
• XmlSerializationOptions()
- Customize the export
© Guidewire Software, Inc. 2001-2014. All rights reserved. Do not distribute without permission. 29
Lesson outline
• Overview of XML
© Guidewire Software, Inc. 2001-2014. All rights reserved. Do not distribute without permission. 30
Strongly typed XML
XSD
file.xsd
.parse ( <>
file.xml
)
• If an existing XSD validates and informs the XML, then the
parsed XML is strongly typed
- XSD must be known to Guidewire
- XSD parses the XML document
© Guidewire Software, Inc. 2001-2014. All rights reserved. Do not distribute without permission. 32
Parsing a strongly typed XML file
8 uses gw.xml.XmlElement
9 uses java.io.File
14 uses trainingapp.demo.xsd.examplestrongtyped.ExampleStrongTyped
…17 var filePathInput = "…/examples/xml/exampleStrongTyped.xml"
…19 var file = new File(filePathInput)
…22 var xml = ExampleStrongTyped.parse(file)
…25 xml.print()
<?xml version="1.0"?>
<ExampleStrongTyped xmlns="http://www.w3.org/2001/XMLSchema">
<database name="TADatabase" dbtype="h2" autoupgrade="true">
<param name="jdbcURL" value="jdbc:h2:file:ta/db/ta"/>
<param name="jdbcURLtest" value="jdbc:h2:file:ta/db/test"/>
© Guidewire Software, Inc. 2001-2014. All rights reserved. Do not distribute without permission. 33
XSD types as Gosu types
8 uses gw.xml.XmlElement
9 uses java.io.File
14 uses trainingapp.demo.xsd.examplestrongtyped.ExampleStrongTyped
…17 var filePathInput = "…/examples/xml/exampleStrongTyped.xml"
…19 var file = new File(filePathInput)
…22 var xml = ExampleStrongTyped.parse(file)
…28 print(xml.Database[0].Name)
…29 print(xml.Param.Count)
• Declared elements
and attributes in the
XSD are available
as Gosu types
• Use dot notation
to reference the
element types and attributes
© Guidewire Software, Inc. 2001-2014. All rights reserved. Do not distribute without permission. 34
XmlElement property prefix $
…19 var xml = XmlElement.parse(file)
…49 print("QName: " + xml.$QName)
…51 var dbElement = xml.$Children.singleWhere(\el ->
el.QName.LocalPart == "database"))
52 print("Attributes: " + dbElement.AttributesNames)
QName:
{http://www.w3.org/2001/XMLSchema}ExampleStrongTyped
---------------------------------------
Attributes: [name, driver, dbtype, printcommands, autoupgrade,
checker]
© Guidewire Software, Inc. 2001-2014. All rights reserved. Do not distribute without permission. 35
Exporting XML
…6 uses gw.xml.XmlElement
7 uses java.io.File
…10 uses java.io.FileWriter
…11 uses java.io.BufferedWriter
…15 var filePathOutput = "…/xml/exampleStrongTyped_Modified.xml"
…92 var outputWriter = new BufferedWriter(new FileWriter(
new File(filePathOutput)))
93 outputWriter.write(xml.asUTFString())
94 outputWriter.close()
• asUTFString() method
- Serializes the element to a
String object in UTF-8
• XmlSerializationOptions()
- Customize the export
© Guidewire Software, Inc. 2001-2014. All rights reserved. Do not distribute without permission. 37
Automatic validation of XML
…22 var xml = ExampleStrongTyped.parse(file)
81 var claimSystem = new XmlElement(new QName(
"http://guidewire.com/config", "claimSystem"))
82 claimSystem.setAttributeValue("URL",
"http://localhost:8880/ab/ContactManager.do")
83 xml.addChild(claimSystem)
…93 var outputWriter2 = new BufferedWriter(new FileWriter(
new File(filePathOutput2)))
94 outputWriter2.write(xml.asUTFString())
95 outputWriter2.close()
© Guidewire Software, Inc. 2001-2014. All rights reserved. Do not distribute without permission. 38
Lesson outline
• Overview of XML
© Guidewire Software, Inc. 2001-2014. All rights reserved. Do not distribute without permission. 39
XMLNode versus XmlElement
XMLNode XmlElement
© Guidewire Software, Inc. 2001-2014. All rights reserved. Do not distribute without permission. 40
XML model
<ABContact>
<PublicID>ab:68
</PublicID>
</ABContact>
ABContact XML XML
68 model data
© Guidewire Software, Inc. 2001-2014. All rights reserved. Do not distribute without permission. 41
XML model editor
• XML Model editor
Fields to add creates GX
based on entity Models
- File extension
is .gx
Fields
added to Model • Steps to create
1. Create GX
model
2. Add fields to the
model
3. Use the model to
generate the
message
Sample XML payload
or Schema
© Guidewire Software, Inc. 2001-2014. All rights reserved. Do not distribute without permission. 42
Lesson objectives review
• You should now be able to:
- Describe the fundamental features of XML
- Describe the basic functionality of the Guidewire XmlElement class
- Import and export untyped XML using Gosu
- Import and export strongly typed XML using Gosu and an XSD
- Identify additional Guidewire tools for working with XML
© Guidewire Software, Inc. 2001-2014. All rights reserved. Do not distribute without permission. 43
Review questions
1. What is the difference between untyped and strongly
typed XML?
2. Imagine an XML file is parsed without an XSD into a
variable called "xml". The first child of the root object is an
element named <loadOptions> and there are no other
elements with that name. Name two Gosu expressions
that would access the <loadOptions> element.
3. How do you make an XSD known to Guidewire?
4. Name two features of strongly typed XML that are not
available with untyped XML.
© Guidewire Software, Inc. 2001-2014. All rights reserved. Do not distribute without permission. 44
Notices
Copyright © 2001-2014 Guidewire Software, Inc. All rights reserved.
This file and the contents herein are the property of Guidewire Software, Inc. Use of this course material
is restricted to students officially registered in this specific Guidewire-instructed course, or for other use
expressly authorized by Guidewire. Replication or distribution of this course material in electronic, paper,
or other format is prohibited without express permission from Guidewire.
© Guidewire Software, Inc. 2001-2014. All rights reserved. Do not distribute without permission. 45