简体   繁体   English

Java中解析XML的有效方法

[英]Efficient way of parsing XML in Java

I have to parse an XML file with following structure: 我必须解析具有以下结构的XML文件:

<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. 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. 有没有更好的方法来解析此XML。

考虑使用JAXB ,它是Java自版本6以来的一部分。您应该能够几乎没有代码地将XML文件解析(“解组”)到您自己的类中,只需添加一些注释即可明确说明对象结构与您的对象之间的映射。 XML结构。

StAX and or JAXB is almost always the way to go. StAX和/或JAXB几乎总是行之有效的方法。

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. 如果XML确实是动态的(例如属性指定属性名称),即<prop name="property" value="" />那么您将仅需要使用StAX或使用JAXB映射到的内容(名称为POJO和值属性)和后期处理。

Personally I find combining StAX and JAXB the best. 我个人认为结合StAX和JAXB是最好的。 I parse to the elements I want and then use JAXB to turn the element into a POJO. 我解析了所需的元素,然后使用JAXB将元素转换为POJO。

See Also : 另请参阅

尽管JAXB可能是最佳选择,但我还要提到jOOX ,它提供了类似JQuery的API,并且使XML文档的使用非常愉快。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM