gpt4 book ai didi

java - 无法使用 Element.getAttribute() 获取元素值

转载 作者:数据小太阳 更新时间:2023-10-29 02:51:57 25 4
gpt4 key购买 nike

我想读取一个 xml 文件。我找到了一个很好的例子,直到 xml 元素没有任何属性。当然,我已经尝试过如何读取属性,但它不起作用。

例如 XML

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<car>
<properties>
<test h="1.12" w="4.2">
<colour>red</colour>
</test>
</properties>
</car>

Java 代码:

public void readXML(String file) {
try {

File fXmlFile = new File(file);
DocumentBuilderFactory dbFactory = DocumentBuilderFactory
.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
doc = dBuilder.parse(fXmlFile);
doc.getDocumentElement().normalize();

for (int temp = 0; temp < nList.getLength(); temp++) {

Node nNode = nList.item(temp);
if (nNode.getNodeType() == Node.ELEMENT_NODE) {

Element eElement = (Element) nNode;

System.out.println("test : "
+ getTagValue("test", eElement));
System.out.println("colour : " + getTagValue("colour", eElement));

}
}

} catch (Exception e) {
e.printStackTrace();
}
}

public String getTagValue(String sTag, Element eElement) {
NodeList nlList = eElement.getElementsByTagName(sTag).item(0)
.getChildNodes();


Node nValue = (Node) nlList.item(0);
System.out.println(nValue.hasAttributes());

if (sTag.startsWith("test")) {
return eElement.getAttribute("w");

} else {
return nValue.getNodeValue();
}

}

输出:

假的

测试:

错误

颜色:红色

我的问题是,我无法打印出属性。我怎样才能得到属性?

最佳答案

你的代码有很多错误;未声明的变量和看似疯狂的算法。我重写了它并且它有效:

import java.io.File;

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;

public final class LearninXmlDoc
{
private static String getTagValue(final Element element)
{
System.out.println(element.getTagName() + " has attributes: " + element.hasAttributes());

if (element.getTagName().startsWith("test"))
{
return element.getAttribute("w");

}
else
{
return element.getNodeValue();
}
}

public static void main(String[] args)
{
final String fileName = "c:\\tmp\\test\\domXml.xml";

readXML(fileName);
}

private static void readXML(String fileName)
{
Document document;
DocumentBuilder documentBuilder;
DocumentBuilderFactory documentBuilderFactory;
NodeList nodeList;
File xmlInputFile;

try
{
xmlInputFile = new File(fileName);
documentBuilderFactory = DocumentBuilderFactory.newInstance();
documentBuilder = documentBuilderFactory.newDocumentBuilder();
document = documentBuilder.parse(xmlInputFile);
nodeList = document.getElementsByTagName("*");

document.getDocumentElement().normalize();

for (int index = 0; index < nodeList.getLength(); index++)
{
Node node = nodeList.item(index);
if (node.getNodeType() == Node.ELEMENT_NODE)
{
Element element = (Element) node;

System.out.println("\tcolour : " + getTagValue(element));
System.out.println("\ttest : " + getTagValue(element));
System.out.println("-----");
}
}
}
catch (Exception exception)
{
exception.printStackTrace();
}
}
}

关于java - 无法使用 Element.getAttribute() 获取元素值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9008864/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com