Class XmlSlurper

java.lang.Object
de.stefanfrings.utils.XmlSlurper

public class XmlSlurper extends Object
Parse XML documents of any structure into a flat map key/value pairs.

Example input:

 <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
 <Individuum type="human">
     <Name>Stefan</Name>
     <Job stillActive="yes">programmer</Job>
     <ns1:Hobbies xmlns:ns1="http://www.somewhere.com/">
         <Hobby>cycling</Hobby>
         <Hobby>electronics</Hobby>
         <Hobby>swimming</Hobby>
         <Hobby></Hobby>
     </ns1:Hobbies>
 </Individuum>
 

Example result:

  • Individuum = ""
  • Individuum.type = "human"
  • Individuum.Name = "Stefan"
  • Individuum.Job = "programmer"
  • Individuum.Job.stillactive = "yes"
  • Individuum.Hobbies = ""
  • Individuum.Hobbies.Hobby = "cycling"
  • Individuum.Hobbies.Hobby0 = "cycling"
  • Individuum.Hobbies.Hobby1 = "electronics"
  • Individuum.Hobbies.Hobby2 = "swimming"
  • Individuum.Hobbies.Hobby3 = ""

The output does not distinguish between attributes and child elements. XML namespaces are ignored. The elements of arrays are numbered (e.g. Hobby0, Hobby1, Hobby2, ...). In addition, the first element is also available without number suffix (e.g. Hobby).

Author:
Stefan Frings, http://stefanfrings.de/javautils