Ways to parse an XML document for Java objects

I am looking for various ways to parse an XML document for Java objects.

I have an XML document, I need to parse this XML document to extract the elements and parse them into Java objects.

Could you recommend the following?

  • different approaches
  • productive analyzer tools for Java
+2
source share
2 answers

Personally, I use the Groovy program for a simple XML parser of an XML document using XmlSlurper and return the map as an interface.

Or just use Groovy to return the value

class XmlParseHelper {
    private def xmlNode;

    private String elementName


    public XmlParserHelper(String elementName, String xml){
        this.elementName = elementName
        xmlNode = new XmlSlurper().parseText(xml)
    }

    public int getTotalRows(){
        return xmlNode.table.find{it.@name == elementName}.row.size()
    }


    public String getValue(String columName, int index){
        return xmlNode.table.find{it.@name == elementName}.row[index].column.find{it.@name == columnName}.toString()
    }
}

//xml is the string of the xml
XmlParseHelper xmlParseHelper = new XmlParseHelper ("mytable", xml);

        for(int i = 0; i < XmlParseHelper .getTotalRows(); i++){
            String code= xmlParseHelper.getValue("code", i);
            String name= xmlParseHelper.getValue("name", i);
.....
     }

XML, : .

0

All Articles