Class 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
    • Constructor Detail

      • XmlSlurper

        public XmlSlurper()
    • Method Detail

      • parse

        public static ConvertingHashMap parse​(String xmlDocument)
                                       throws ParserConfigurationException,
                                              SAXException,
                                              IOException
        Parse an XML document into a simple flat HashMap where the keys reflect the XML structure. The output does not distinguish between attributes and child elements. XML namespaces are ignored.

        If a child element occurs multiple times (as a list), then they will be numbered. In addition, the first item will be avaulable without number suffix.

        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 = ""
        Parameters:
        xmlDocument - XML document, optionally with XML version header.
        Returns:
        Map with all collected data.
        Throws:
        ParserConfigurationException
        SAXException
        IOException