Package de.stefanfrings.utils
Class XmlSlurper
java.lang.Object
de.stefanfrings.utils.XmlSlurper
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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic ConvertingHashMap
Parse an XML document into a simple flat HashMap where the keys reflect the XML structure.
-
Constructor Details
-
XmlSlurper
public XmlSlurper()
-
-
Method Details
-
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.- Parameters:
xmlDocument
- XML document, optionally with XML version header.- Returns:
- Map with all collected data.
- Throws:
ParserConfigurationException
- Input format errorSAXException
- Input format errorIOException
- Should never happen
-