Java 解析不带标记名的xml

Java 解析不带标记名的xml,java,xml,document,w3c,Java,Xml,Document,W3c,我有一个xml文件 <Response> <StatusCode>0</StatusCode> <StatusDetail>OK</StatusDetail> <AccountInfo> <element1>value</element1> <element2>value</element2> <element3>value</ele

我有一个xml文件

<Response>
<StatusCode>0</StatusCode>
<StatusDetail>OK</StatusDetail>
<AccountInfo> 
    <element1>value</element1>
    <element2>value</element2>
    <element3>value</element2>
    <elementN>value</elementN>   
</AccountInfo>
</Response>

以下是两种方法:

Node accountInfo = document.getElementsByTagName("AccountInfo").item(0);
NodeList children = accountInfo.getChildNodes();
或者你可以

XPath xPath = XPathFactory.newInstance().newXPath();
NodeList children = (NodeList) xPath.evaluate("//AccountInfo/*", document.getDocumentElement(), XPathConstants.NODESET);
一旦你有了
节点列表
,你就可以在它们之间循环

for(int i=0;i<children.getLength();i++) {
    if(children.item(i).getNodeType() == Node.ELEMENT_NODE) {
        Element elem = (Element)children.item(i);
        // If your document is namespace aware use localName
        String localName = elem.getLocalName();
        // Tag name returns the localName and the namespace prefix
        String tagName= elem.getTagName();
        // do stuff with the children
    }
}

for(int i=0;i)如果您不知道可以有哪些标记(以及它们表示什么),您希望如何以有意义的方式提取信息?您确定要动态地提取信息吗?如果您有n个不同的变量以动态方式解析,则可能很难使用它们(例如,如果您使用某种算法来计算某个值)。这些值将(或多或少)在列表中表示。通常的方法是为一组特定的输入使用一个实现。或者您只是想将值转储到数据库中吗?可能会让您开始。我想将此信息发送到其他模块,此模块可以在前端打印。我的程序将与不同的提供商一起工作,他们具有不同的信息…列表of提供商非常大……超过300家
for(int i=0;i<children.getLength();i++) {
    if(children.item(i).getNodeType() == Node.ELEMENT_NODE) {
        Element elem = (Element)children.item(i);
        // If your document is namespace aware use localName
        String localName = elem.getLocalName();
        // Tag name returns the localName and the namespace prefix
        String tagName= elem.getTagName();
        // do stuff with the children
    }
}