解析没有标记名的xml [英] Parse xml without tagname

查看:208
本文介绍了解析没有标记名的xml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个xml文件

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

我想在AccountInfo中解析我的元素,但我不知道元素标签名称。



现在我正在使用并拥有此代码进行测试,但将来我会在AccountInfo中收到更多的elemenet,我不知道有多少或有名称

  String name =; 
String balance =;
节点accountInfo = document.getElementsByTagName(AccountInfo)。item(0);

if(accountInfo.getNodeType()== Node.ELEMENT_NODE){
Element accountInfoElement =(Element)accountInfo;
name = accountInfoElement.getElementsByTagName(Name)。item(0).getTextContent();
balance = accountInfoElement.getElementsByTagName(Balance)。item(0).getTextContent();
}


解决方案

你有两种方法可以做它:

 节点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);

一旦你有 NodeList ,你就可以循环对于(int i = 0; i< children.getLength(); i ++){
if (children.item(i).getNodeType()== Node.ELEMENT_NODE){
元素elem =(元素)children.item(i);
//如果您的文档是名称空间感知的,请使用localName
String localName = elem.getLocalName();
//标记名称返回localName和名称空间前缀
String tagName = elem.getTagName();
//与孩子一起做的事情
}
}


I have a xml file

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

And I want parse my elements in AccountInfo, but I dont know elements tag names.

Now Im using and have this code for tests, but in future I will recieve more elemenets in AccountInfo and I dont know how many or there names

String name="";
String balance="";
 Node accountInfo = document.getElementsByTagName("AccountInfo").item(0);

        if (accountInfo.getNodeType() == Node.ELEMENT_NODE){
            Element accountInfoElement = (Element) accountInfo;
            name = accountInfoElement.getElementsByTagName("Name").item(0).getTextContent();
            balance = accountInfoElement.getElementsByTagName("Balance").item(0).getTextContent();
        }

解决方案

Heres 2 ways you can do it:

Node accountInfo = document.getElementsByTagName("AccountInfo").item(0);
NodeList children = accountInfo.getChildNodes();

or you can do

XPath xPath = XPathFactory.newInstance().newXPath();
NodeList children = (NodeList) xPath.evaluate("//AccountInfo/*", document.getDocumentElement(), XPathConstants.NODESET);

Once you have your NodeList you can loop through them.

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

这篇关于解析没有标记名的xml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆