XML is like English: complicated, ugly, confounding and ubiquitous.
Gosu attempts to minimize the pain of working with XML with a few different libraries:
If you just want to work with XML in an untyped, simple way, you can use
uses gw.xml.simple.SimpleXmlNode var xml = SimpleXmlNode.parse( "<foo attr1='doh'><bar attr2='rey'/></foo>" ) print( xml.Attributes["attr1"] ) print( xml.Children.Attributes["attr2"] ) print( xml.toXmlString() )You can use the
Descendentsproperty to get ahold of a collection of all children of a Node, which allows you to apply block-based slicing and dicing:
var allReys = xml.Descendents.where( \ n -> n.Attributes["attr2"] == "rey" )Pretty easy to deal with.
If you have an XSD file, you can drop it in your source directory and start using it immediately, thanks to the Open Type System
Let's say you have an XSD like this:
<?xml version="1.0"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="Person"> <xs:complexType> <xs:sequence> <xs:element name="firstname" type="xs:string"/> <xs:element name="lastname" type="xs:string"/> <xs:element name="age" type="xs:int"/> </xs:sequence> </xs:complexType> </xs:element> </xs:schema>And you drop it at
myapp/example.xsd. You can then start using the XSD-derived types like so:
var x = new myapp.example.Person() x.Age = 32 x.Firstname = "Joe" x.Lastname = "Blow" x.print() // print out theNo code gen, just drop in the XSD and go, through the magic of the Open Type System.
I don't understand XSD at all, and even I can use that.
Similarly, if you have a WSDL like the "simple" (*barf*) example defined here, and you drop it in your source directory at
myapp/weather.wsdl, you can use the WSDL-derived types like so:
var x = new myapp.weather() var forecast = x.GetCityForecastByZIP("95816") print( forecast.ForecastResult.Forecast.map( \ f -> f.Desciption ).join("\n") )Again, no code gen. Drop the WSDL in and go.
Almost makes you not hate XML, doesn't it?
By the way, Dana Lank, an engineer at Guidewire, is the person who just made your life so much better.