Java XML读取
我一直想知道如何读取XML文件,但在回答之前,请阅读整篇文章 例如,我有:Java XML读取,java,xml,Java,Xml,我一直想知道如何读取XML文件,但在回答之前,请阅读整篇文章 例如,我有: <?xml version="1.0" encoding="UTF-8"?> <messages> <incoming id="0" class="HelloIlikeyou" /> </messages> 我想要的是从标记中获取所有值。我想把它放在一个字典中,哪个键是传入/传出的,然后它将包含一个Pair列表作为value,as key是id值,as valu
<?xml version="1.0" encoding="UTF-8"?>
<messages>
<incoming id="0" class="HelloIlikeyou" />
</messages>
我想要的是从标记中获取所有值。我想把它放在一个字典中,哪个键是传入/传出的,然后它将包含一个Pair列表作为value,as key是id值,as value是class值
所以我得到了这个:
HashMap<String, List<Pair<Integer, String>>> headers = new HashMap<>();
HashMap headers=newhashmap();
然后它将存储以下内容:
HashMap.get("incoming").add(new Pair<>("0", "HelloIlikeyou"));
HashMap.get(“传入”).add(新对(“0”、“HelloIlikeyou”));
但我不知道怎么做,我已经得到了一个部分,但它不工作:
File xml = new File(file);
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(xml);
doc.getDocumentElement().normalize();
NodeList nodes = doc.getElementsByTagName("messages");
for (int i = 0; i < nodes.getLength(); i++) {
Node node = nodes.item(i);
System.out.println("Type: " + node.getNodeValue() + " packet ID " + node.getUserData("id"));
}
File xml=新文件(File);
DocumentBuilderFactory dbFactory=DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder=dbFactory.newDocumentBuilder();
documentdoc=dBuilder.parse(xml);
doc.getDocumentElement().normalize();
NodeList nodes=doc.getElementsByTagName(“消息”);
对于(int i=0;i
使用许多可用库中的一个,例如XStream:
您可以使用JAXB,我认为这是最好的方法。看看这个: 你就快到了。W3C类有点过时了。这就是您想要的:
public static void main(final String[] args)
throws ParserConfigurationException, SAXException, IOException {
File xml = new File(file);
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(xml);
doc.getDocumentElement().normalize();
NodeList nodes = doc.getElementsByTagName("messages");
for (int i = 0; i < nodes.getLength(); i++) {
Node node = nodes.item(i);
for (int j = 0; j < node.getChildNodes().getLength(); j++) {
Node child = node.getChildNodes().item(j);
if (!child.getNodeName().equals("#text")) {
NamedNodeMap attributes = child.getAttributes();
System.out.println("Type: " + child.getNodeName()
+ " packet ID " + attributes.getNamedItem("id")
+ " - class: " + attributes.getNamedItem("class"));
}
}
}
}
“它不工作”是什么意思(原文如此)?有例外吗?不返回任何数据?计算机着火了?您仍在messages节点上操作,必须在节点上迭代。getChildNodes()有人能回答这个问题吗?[在此处输入链接描述][1][1]:在所有xml序列化库中,JAXB、imo提供的api最差。XStream在api的流畅性和方便性方面远远优于它。其背后的原因可能是JAXB是一个ref实现,不能使用像XStream这样的外来工具。请提供一个令人信服的理由来使用这个工具,而不仅仅是建议它。谢谢,但我必须添加:if(attributes==null){continue;}以防止出现null错误(我的项目的工作方式是在一个bug之后停止,因为那是一个空bug,所以我得到了一个错误)。无论如何,感谢您的修复。
public static void main(final String[] args)
throws ParserConfigurationException, SAXException, IOException {
File xml = new File(file);
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(xml);
doc.getDocumentElement().normalize();
NodeList nodes = doc.getElementsByTagName("messages");
for (int i = 0; i < nodes.getLength(); i++) {
Node node = nodes.item(i);
for (int j = 0; j < node.getChildNodes().getLength(); j++) {
Node child = node.getChildNodes().item(j);
if (!child.getNodeName().equals("#text")) {
NamedNodeMap attributes = child.getAttributes();
System.out.println("Type: " + child.getNodeName()
+ " packet ID " + attributes.getNamedItem("id")
+ " - class: " + attributes.getNamedItem("class"));
}
}
}
}
Type: incoming packet ID id="0" - class: class="HelloIlikeyou"