0

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 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.

9
  • You can also use some XML deserialization (XStream, ...) Commented Sep 7, 2011 at 6:12
  • I agree with the gentleman - but it kinda depends on your requirement of efficiency: XPath is probably not going to perform as much as SAX or your current solution. But it is probably less prone to errors and less work to maintain. Commented Sep 7, 2011 at 6:13
  • 3
    Also DON'T use == for String, use equals() Commented Sep 7, 2011 at 6:19
  • I'm surprised the code is working at all, given that you're using == to compare strings... Commented Sep 7, 2011 at 6:33
  • 1
    @chai: Something must be interning the strings, basically... but it's still not a good idea to rely on it. Commented Sep 7, 2011 at 13:41

3 Answers 3

9

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.

Sign up to request clarification or add additional context in comments.

1 Comment

JAXB is a good choice if the structure is stable, the schema is reasonably simple and not extensible, and there is no mixed content.
0

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:

Comments

0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.