我一直在寻找读取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>
答案 0 :(得分:2)
试一试。
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());
}
答案 1 :(得分:2)
我一直在修补同样的问题,并认为我找到了解决方案:
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中有多少标签或者它们的名字是什么。希望这会有所帮助。