Java中解析XML的有效方法

Java中解析XML的有效方法,java,xml,xml-parsing,Java,Xml,Xml Parsing,我必须解析具有以下结构的XML文件: <root> <object_1> <pro1> abc </pro1> <pro2> pqr </pro2> <pro3> xyz </pro3> <children> <object_a> <pro1&

我必须解析具有以下结构的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>
具有各自的特性

下面的代码对我很有用,但这不是最好的方法

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);
    }
}
File File=新文件(文件名);
DocumentBuilderFactory dbFactory=DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder=dbFactory.newDocumentBuilder();
Document doc=dBuilder.parse(文件);
doc.getDocumentElement().normalize();
如果(doc==null)返回;
Node=doc.getFirstChild();
NodeList lst=node.getChildNodes();
节点子节点=null;
int len=lst.getLength();

对于(int index=0;index请考虑使用,这是自版本6以来Java的一部分。您应该能够解析(“unmarshall”)将XML文件放入自己的类中,几乎不需要任何代码,只需添加一些注释,说明对象结构和XML结构之间的映射。

StAX和/或JAXB几乎总是一条可行之路

如果XML确实是动态的(如属性指定属性名),即
,那么您将需要仅使用StAX,或者使用JAXB将其映射到的内容(具有名称和值属性的POJO)并进行后期处理

就我个人而言,我发现结合使用StAX和JAXB是最好的。我解析到我想要的元素,然后使用JAXB将元素转换成POJO

另见

  • 我自己的实用程序库将成为

虽然JAXB可能是最好的选择,但我还想提一提,它提供了类似于JQuery的API,使处理XML文档变得非常愉快。

您还可以使用一些XML反序列化(XStream,…)我同意这位先生的观点——但这有点取决于您对效率的要求:XPath的性能可能不如SAX或您当前的解决方案。但它可能不太容易出错,维护工作量也较少。另外,对于
String
,请使用
equals()
我很惊讶代码能正常工作,因为你使用
==
来比较字符串…@chai:基本上,字符串中肯定有什么东西在里面……但依赖它仍然不是一个好主意。如果结构稳定,模式相当简单且不可扩展,并且没有混合内容,JAXB是一个不错的选择。
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);
    }
}