获取 XML 中子节点的属性值
Get attribute value of childNodes in XML
我正在尝试获取 XML 文件中第二个 child 的值,但这对我来说似乎很难,所以如果你能帮助我,我将不胜感激.我需要获取名称 Rob
、Bob
和 Victor
.. 而没有 Tom
。最后,字符串表达式应该是 "//main"
<main id="Tom">
<asd name="Rob">
</asd>
<qwe name="Bob">
</qwe>
<iop name="Victor">
</iop>
</main>
这就是我目前所做的...
public void getXML(String direction)throws Exception{
String[] stops = new String[22];
InputSource inputSrc = new InputSource(getResources().openRawResource(R.raw.bus_time));
XPath xpath = XPathFactory.newInstance().newXPath();
String expression = "//main" ;
NodeList nodes = (NodeList) xpath.evaluate(expression,inputSrc,XPathConstants.NODESET);
if(nodes != null && nodes.getLength() > 0) {
for (int i = 0; i < nodes.getLength(); ++i) {
Node node = nodes.item(i);
NodeList child = node.getChildNodes();
for(int k=0; k<child.getLength();k++) {
Node asd = child.item(k);
NamedNodeMap attr = asd.getAttributes();
for (int a = 0; a < attr.getLength(); a++) {
stops[a] = attr.item(a).getNodeValue();
Toast.makeText(this, String.valueOf(stops[a]), Toast.LENGTH_LONG).show();
}
}
}
}
}
我从 Toast 收到的结果 = null
方法 node.getChildNodes() returns 一些类型等于 TEXT_NODE 的元素,这不是您要查找的类型,并且在尝试访问其属性时失败。为了让你的代码工作,你必须在尝试获取你想要的属性之前检查是否 asd.getNodeType() == Node.ELEMENT_NODE 。
因此,将此条件添加到您的以下代码段中:
if (asd.getNodeType() == Node.ELEMENT_NODE) {
NamedNodeMap attr = asd.getAttributes();
for (int a = 0; a < attr.getLength(); a++) {
stops[a] = attr.item(a).getNodeValue();
Toast.makeText(this, stops[a], Toast.LENGTH_LONG).show();
}
}
但是你可以让它更简单,你不需要遍历属性来获取特定的属性,只需将它转换为 Element 并通过属性名称访问它,如:
if (asd.getNodeType() == Node.ELEMENT_NODE) {
Toast.makeText(this, ((Element)asd).getAttribute("name"), Toast.LENGTH_LONG).show();
}
或另一种方式:
if (asd instanceof Element) {
Toast.makeText(this, ((Element)asd).getAttribute("name"), Toast.LENGTH_LONG).show();
}
我正在尝试获取 XML 文件中第二个 child 的值,但这对我来说似乎很难,所以如果你能帮助我,我将不胜感激.我需要获取名称 Rob
、Bob
和 Victor
.. 而没有 Tom
。最后,字符串表达式应该是 "//main"
<main id="Tom">
<asd name="Rob">
</asd>
<qwe name="Bob">
</qwe>
<iop name="Victor">
</iop>
</main>
这就是我目前所做的...
public void getXML(String direction)throws Exception{
String[] stops = new String[22];
InputSource inputSrc = new InputSource(getResources().openRawResource(R.raw.bus_time));
XPath xpath = XPathFactory.newInstance().newXPath();
String expression = "//main" ;
NodeList nodes = (NodeList) xpath.evaluate(expression,inputSrc,XPathConstants.NODESET);
if(nodes != null && nodes.getLength() > 0) {
for (int i = 0; i < nodes.getLength(); ++i) {
Node node = nodes.item(i);
NodeList child = node.getChildNodes();
for(int k=0; k<child.getLength();k++) {
Node asd = child.item(k);
NamedNodeMap attr = asd.getAttributes();
for (int a = 0; a < attr.getLength(); a++) {
stops[a] = attr.item(a).getNodeValue();
Toast.makeText(this, String.valueOf(stops[a]), Toast.LENGTH_LONG).show();
}
}
}
}
}
我从 Toast 收到的结果 = null
方法 node.getChildNodes() returns 一些类型等于 TEXT_NODE 的元素,这不是您要查找的类型,并且在尝试访问其属性时失败。为了让你的代码工作,你必须在尝试获取你想要的属性之前检查是否 asd.getNodeType() == Node.ELEMENT_NODE 。 因此,将此条件添加到您的以下代码段中:
if (asd.getNodeType() == Node.ELEMENT_NODE) {
NamedNodeMap attr = asd.getAttributes();
for (int a = 0; a < attr.getLength(); a++) {
stops[a] = attr.item(a).getNodeValue();
Toast.makeText(this, stops[a], Toast.LENGTH_LONG).show();
}
}
但是你可以让它更简单,你不需要遍历属性来获取特定的属性,只需将它转换为 Element 并通过属性名称访问它,如:
if (asd.getNodeType() == Node.ELEMENT_NODE) {
Toast.makeText(this, ((Element)asd).getAttribute("name"), Toast.LENGTH_LONG).show();
}
或另一种方式:
if (asd instanceof Element) {
Toast.makeText(this, ((Element)asd).getAttribute("name"), Toast.LENGTH_LONG).show();
}