![](/img/trans.png)
[英]How to read the values from a XML file using Java when all the tags are same..?
[英]Read all tags and values from XML android java
我一直在尋找讀取XML標記及其值,我可以找到的是使用標記名稱讀取值,但是不知道標記名稱是否可以讀取所有值,如果可以,請向我解釋
樣本XML
<menu>
<item>
<id>1</id>
<name>test1</name>
<cost>155</cost>
<description>Single</description>
</item>
<item>
<id>2</id>
<name>test2</name>
<cost>225</cost>
<description>double</description>
</item>
<item>
<id>3</id>
<name>test3</name>
<cost>110</cost>
<description>long</description>
</item>
<item>
<id>4</id>
<name>test4</name>
<cost>155</cost>
<description>float</description>
</item>
<item>
<id>5</id>
<name>test5</name>
<cost>445</cost>
<description>integer</description>
</item>
</menu>
試試看
DocumentBuilderFactory a = DocumentBuilderFactory.newInstance();
Document doc = dbf.newDocumentBuilder().parse(file);
doc.getDocumentElement().normalize(); // Not Mandatory
NodeList elements=doc.getElementsByTagName("*");
for (int i=0; i<elements.getLength(); i++)
{
// Get element
Element elem = (Element)elements.item(i);
System.out.println(elem.getNodeName());
}
我一直在修補相同的問題,並認為我找到了解決方案:
private void listNodes(NodeList nodeList){
// Loop through the node list
for (int temp = 0; temp < (nodeList != null ? nodeList.getLength() : 0); temp++) {
Node node = nodeList.item(temp);
// if node is element
if (node.getNodeType() == Node.ELEMENT_NODE) {
// create element from node
Element element = (Element) node;
// get tag name from element
Log.d("tag", "tag/node: " + element.getTagName());
// if node has children repeat the loop until last child
if (node.hasChildNodes()) {
listNodes(node.getChildNodes());
}
}
}
}
這將為您提供所有節點的標簽,現在您只需將其實現到所需的xml級別即可。 一旦有了標簽,就將它們放入數組並從中獲取元素。
編輯:
好的,所以我玩了一段時間,並得到了一些有趣的結果。 首先,我設置了xmls,以便每一行都位於<item>
標記中,因此不會造成混亂。 這是我用來獲取節點列表的方法:
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder builder = dbf.newDocumentBuilder();
// input stream of any kind, can be local or from URL
Document doc = builder.parse(new InputSource(inputStream));
doc.getDocumentElement().normalize();
// rowNodeName in my case is "item" so you can change this to anything suitable
nodeList = doc.getElementsByTagName(rowNodeName);
} catch (ParserConfigurationException | IOException | SAXException e) {
e.printStackTrace();
}
現在,您有了節點列表,只需執行以下操作:
// a global object for tag strings
nodeNames = new ArrayList<>();
// populate tag list by reading them from first element
listNodes(nodeList.item(0).getChildNodes(), false);
// list nodes, notice the second parameter is boolean
listNodes(nodeList,true);
最后的listNodes
方法如下:
private void listNodes(NodeList list, boolean getValues) {
for (int temp = 0; temp < (list != null ? list.getLength() : 0); temp++) {
Node node = list.item(temp);
if (node.getNodeType() == Node.ELEMENT_NODE) {
Element element = (Element) node;
// if you want values then you loop through the tag names and get node values for those tags
if (getValues) {
Bundle bundle = new Bundle();
for(int i = 0; i< nodeNames.size(); i++){
bundle.putString(nodeNames.get(i), getNode(nodeNames.get(i),element));
}
xmlTableRows.add(bundle);
} else {
/// if you want tags just read them from node
nodeNames.add(element.getTagName());
}
}
}
}
getNode
方法:
private static String getNode(String tag, Element element) {
NodeList nlList = element.getElementsByTagName(tag).item(0)
.getChildNodes();
Node nValue = (Node) nlList.item(0);
return nValue.getNodeValue();
}
因此,現在您不必依賴於知道xml中有多少個標簽或其名稱是什么。 希望這可以幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.