The easiest way to navigate an XML document is by using an XSD dropped
into a Gosu source path.
Let's say we have the following XSD:
<xs:schema xmlns:xs="
http://www.w3.org/2001/XMLSchema">
<xs:element name="root">
<xs:complexType>
<xs:sequence>
<xs:element name="child" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:attribute name="key" type="xs:int"/>
<xs:attribute name="value" type="xs:string"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
We'll call it demo.xsd and drop it into a Gosu source root. Lets say
we have the following instance document, demo.xml:
<root>
<child key="1" value="one"/>
<child key="2" value="two"/>
<child key="3" value="three"/>
<child key="4" value="four"/>
<child key="5" value="five"/>
</root>
The following Gosu code can be used to find the "value" attribute of
the "child" element where key="3":
var xml = demo.Root.parse( new java.io.File( "platform/xml/gsrc/
demo.xml" ) )
print( xml.Child.firstWhere( \ r -> r.Key == 3 ).Value )
when run, prints:
three
And if you don't care to use an XSD at all, you can achieve a similar
result with the following:
var xml = gw.xml.XmlElement.parse( new java.io.File( "platform/xml/
gsrc/demo.xml" ) )
print( xml.getChildren( "child" ).firstWhere( \ r -
>r.getAttributeValue( "key" ) == "3" ).getAttributeValue( "value" ) )
We don't have any XPath support whatsoever at this time, and while it
might be added in the future, the idea was that Gosu is powerful
enough using blocks to find what you like.
David