开发者

Efficient way of parsing XML in Java

开发者 https://www.devze.com 2023-04-03 08:08 出处:网络
I have to parse an XML file with following structure: <root> <object_1> <pro1> abc </pro1>

I have to parse an XML file with following structure:

<root>
    <object_1>
        <pro1> abc </pro1>
        <pro2> pqr </pro2>
        <pro3> xyz </pro3>

        <children>
            <object_a>
                <pro1> abc </pro1>
                <pro2> pqr </pro2>
                <pro3> xyz </pro3>

                <children>
                    .
                    .
                    .
                </children>
            </object_a>
        </children>     
    </object_1>
    <object_2> 
    .
    . 
    .
    </object_n>
</root>

Aim is to parse this multilevel nesting. A few classes are defined in Java.

Class Object_1
Class Object_2
.
.
.
Class Object_N

with their respective properties.

The following code is working for me, but then this is not the开发者_JAVA百科 best way of doing things.

File file = new File(fileName);
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(file);
doc.getDocumentElement().normalize();

if(doc ==null) return;

Node node = doc.getFirstChild();

NodeList lst = node.getChildNodes();
Node children = null ; 

int len = lst.getLength();
for(int index=0;index<len;index++)
{
    Node child = lst.item(index);
    String name = child.getNodeName();
    if(name=="Name")
        name = child.getNodeValue();
    else if(name=="Comment")
        comment = child.getNodeValue());
    else if(name=="children")
        children = child;
    }

    if(children==null) return; 

    lst = children.getChildNodes();
    len = lst.getLength();
    Class<?> obj=null;
    AbsModel model = null;
    for(int index=0;index<len;index++)
    {
        Node childNode = lst.item(index);
        String modelName = childNode.getNodeName();
        try {
            obj = Class.forName(modelName);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        if(obj!=null)
            model = (AbsModel) obj.newInstance();
        else
            model = new GenericModel();

        model.restoreDefaultPropFromXML(childNode);
        addChild(model);
    }
}

Is there a better way of parsing this XML.


Consider using JAXB, which is part of Java since version 6. You should be able to parse (“unmarshall”) your XML file into your own classes with almost no code, just adding a few annotations expliciting the mapping between your object structure and your XML structure.


StAX and or JAXB is almost always the way to go.

If the XML is really dynamic (like attributes specify the property name) ie <prop name="property" value="" /> then you will need to use StAX only or live with what JAXB will map it to (a POJO with name and value properties) and post process.

Personally I find combining StAX and JAXB the best. I parse to the elements I want and then use JAXB to turn the element into a POJO.

See Also:

  • My own utility library that will turn an XML Stream into an iterator of objects.
  • Parsing very large XML files and marshalling to Java Objects
  • http://tedone.typepad.com/blog/2011/06/unmarshalling-benchmark-in-java-jaxb-vs-stax-vs-woodstox.html


While JAXB may be the best choice I'd also like to mention jOOX which provides a JQuery-like API and makes working with XML documents really pleasant.

0

精彩评论

暂无评论...
验证码 换一张
取 消