如何遍历节点并获取其属性名称?我使用的DOMParser
问题描述:
的xml文件看起来是这样的:如何遍历节点并获取其属性名称?我使用的DOMParser
<?xml version="1.0" encoding="utf-8"?>
<parent>
<child ID="1" Name="CHILD" Order="1">
<child ID="1" Name="SUB_CHILD" Order="1">
</child>
</child>
<child ID="2" Name="CHILD2" Order="1">
<child ID="1" Name="SUB_CHILD" Order="1">
</child>
</child>
</parent>
CODE(新):
void listNodes(NodeList list) {
if (list.getLength() > 0) {
for (int i = 0; i < list.getLength(); i++) {
System.out.println("-------------------");
if (list.item(i).hasAttributes()) {
NamedNodeMap attrs = list.item(i).getAttributes();
for (int index = 0; index < attrs.getLength(); index++) {
Attr attribute = (Attr) attrs.item(index);
if(attribute.getName().equals("Name")){
names[index] = ????
}
}
}else{
System.out.println(list.item(i).getNodeName()+ " has no attributes");
}
System.out.println("-------------------");
}
}
}
我已编辑的代码。现在我知道attribute
有属性。如何提取属性Name
并将其放入字符串数组中。
答
还有,我发现@http://www.java2s.com/Code/JavaAPI/org.w3c.dom/NodegetAttributes.htm
我想你的XML,以及一个通用的解决方案,它工作正常;只是填充你的POJO而不是Sysout,你很好。
你需要的方法是
static void listNodes(Node node, String indent) {
String nodeName = node.getNodeName();
System.out.println(indent + " Node: " + nodeName);
short type = node.getNodeType();
//System.out.println(indent + " Node Type: " + nodeType(type));
if (type == TEXT_NODE) {
System.out.println(indent + " Content is: "
+ ((Text) node).getWholeText());
} else if (node.hasAttributes()) {
System.out.println(indent + " Element Attributes are:");
NamedNodeMap attrs = node.getAttributes();
for (int i = 0; i < attrs.getLength(); i++) {
Attr attribute = (Attr) attrs.item(i);
System.out.println(indent + " " + attribute.getName() + " = "
+ attribute.getValue());
}
}
NodeList list = node.getChildNodes();
if (list.getLength() > 0) {
System.out
.println(indent + " Child Nodes of " + nodeName + " are:");
for (int i = 0; i < list.getLength(); i++) {
listNodes(list.item(i), indent + " ");
}
}
}
static String nodeType(short type) {
switch (type) {
case ELEMENT_NODE:
return "Element";
case DOCUMENT_TYPE_NODE:
return "Document type";
case ENTITY_NODE:
return "Entity";
case ENTITY_REFERENCE_NODE:
return "Entity reference";
case NOTATION_NODE:
return "Notation";
case TEXT_NODE:
return "Text";
case COMMENT_NODE:
return "Comment";
case CDATA_SECTION_NODE:
return "CDATA Section";
case ATTRIBUTE_NODE:
return "Attribute";
case PROCESSING_INSTRUCTION_NODE:
return "Attribute";
}
return "Unidentified";
}
编辑
按照您的要求我修改一下代码,你只是想直接子节点,而不是子儿。这里你去:
void listNodes(NodeList list) {
if (list.getLength() > 0) {
for (int i = 0; i < list.getLength(); i++) {
System.out.println("-------------------");
if (list.item(i).hasAttributes()) {
NamedNodeMap attrs = list.item(i).getAttributes();
for (int index = 0; index < attrs.getLength(); index++) {
Attr attribute = (Attr) attrs.item(index);
System.out.println(" " + attribute.getName() + " = "+ attribute.getValue());
}
}else{
System.out.println(list.item(i).getNodeName()+ " has no attributes");
}
System.out.println("-------------------");
}
}
}
调用这个方法listNodes(document.getDocumentElement().getChildNodes());
它适用于我。
答
代码会是这样的。
private void usingDOMParser() {
try {
DocumentBuilderFactory mDocumentBuilderFactory = DocumentBuilderFactory
.newInstance();
DocumentBuilder mDocumentBuilder = mDocumentBuilderFactory
.newDocumentBuilder();
Document mDocument = mDocumentBuilder.parse(new InputSource(
getAssets().open("example.xml")));
mDocument.getDocumentElement().normalize();
NodeList mNodeList = mDocument.getElementsByTagName("child");
for (int i = 0; i < mNodeList.getLength(); i++) {
Node mNode = mNodeList.item(i);
Element mElement = (Element) mNode;
NodeList nameList = mElement.getElementsByTagName("child");
Element nameElement = (Element) nameList.item(0);
nameList = nameElement.getChildNodes();
Log.i("TAG", "ID: " + nameElement.getAttribute("ID"));
Log.i("TAG", "Name: " + nameElement.getAttribute("Name"));
Log.i("TAG", "Order: " + nameElement.getAttribute("Order"));
}
}
catch (Exception e) {
Log.e("TAG", "Exception: " + e.toString());
}
}
在这里,我把你的XML
文件Assets folder
,但如果你想从互联网访问它,你可以做到这一点。
登录
03-14 17:58:15.845: I/AllTestActivity(624): ID: 1
03-14 17:58:15.845: I/AllTestActivity(624): Name: CHILD
03-14 17:58:15.845: I/AllTestActivity(624): Order: 1
03-14 17:58:15.845: I/AllTestActivity(624): ID: 1
03-14 17:58:15.845: I/AllTestActivity(624): Name: SUB_CHILD
03-14 17:58:15.845: I/AllTestActivity(624): Order: 1
03-14 17:58:15.845: I/AllTestActivity(624): ID: 1
03-14 17:58:15.845: I/AllTestActivity(624): Name: SUB_CHILD_NODE1
03-14 17:58:15.845: I/AllTestActivity(624): Order: 01
03-14 17:58:15.845: I/AllTestActivity(624): ID: 2
03-14 17:58:15.845: I/AllTestActivity(624): Name: SUB_CHILD_NODE2
03-14 17:58:15.845: I/AllTestActivity(624): Order: 02
03-14 17:58:15.854: I/AllTestActivity(624): ID: 2
03-14 17:58:15.854: I/AllTestActivity(624): Name: CHILD2
03-14 17:58:15.854: I/AllTestActivity(624): Order: 1
03-14 17:58:15.854: I/AllTestActivity(624): ID: 1
03-14 17:58:15.854: I/AllTestActivity(624): Name: SUB_CHILD
03-14 17:58:15.854: I/AllTestActivity(624): Order: 1
03-14 17:58:15.854: I/AllTestActivity(624): ID: 1
03-14 17:58:15.854: I/AllTestActivity(624): Name: SUB_CHILD_NODE1
03-14 17:58:15.854: I/AllTestActivity(624): Order: 01
03-14 17:58:15.854: I/AllTestActivity(624): ID: 2
03-14 17:58:15.854: I/AllTestActivity(624): Name: SUB_CHILD_NODE2
03-14 17:58:15.854: I/AllTestActivity(624): Order: 02
进口:
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
我希望这能帮助你。
在我看到您的答案之前,我已经解决了我的问题的一部分。我提供了上面的代码。谢谢回答。如果你的代码可以解决当前的问题,我会很乐意使用它。 – droidH 2013-03-14 06:11:49
通过上面的代码,您可以获得所有节点以及与其关联的所有元素,您可以修改代码以在您想要的位置中断。 – Atrix1987 2013-03-14 06:39:08
我很抱歉,但我不知道该怎么做先生。我几乎在我的智慧结束。 – droidH 2013-03-14 06:48:45