解析没有标记名的xml

编程入门 行业动态 更新时间:2024-10-23 13:27:06
本文介绍了解析没有标记名的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

本文发布于:2023-11-12 12:37:16,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1581511.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:标记   xml

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!