fork(1) download
  1. import groovy.util.*;
  2.  
  3. public class GroovyXmlSlurperExample
  4. {
  5. public static void main(String[] args)
  6. {
  7. def xml = "<root>\n\t<item name=\"a\" value=\"1\"/>\n\t<item name=\"b\" value=\"2\"/>\n</root>";
  8. System.out.println("XML is: \n" + xml);
  9.  
  10. def document = new XmlSlurper().parseText(xml); // Convert the XML into an Object Model
  11.  
  12. System.out.println("\nElements are:");
  13. for (item in document.item)
  14. {
  15. System.out.println(item.@name.text() + "=" + item.@value.text());
  16. }
  17.  
  18. System.out.println("\nJust the value of the 'value' attributes: " + document."item".@value.text());
  19.  
  20. }
  21. }
Success #stdin #stdout 1.26s 388800KB
stdin
Standard input is empty
stdout
XML is: 
<root>
	<item name="a" value="1"/>
	<item name="b" value="2"/>
</root>

Elements are:
a=1
b=2

Just the value of the 'value' attributes: 12