[英]Is there a way to parse XML even though same Tag Names in diffrent Nodes
我正在用 DOM Parser/Builder 用 Java 解析我的 XML 文件。 對於我的 XML 標記名的一部分,它工作正常。 但是當我嘗試解析另一個 Tagname 時,情況變得更糟,因為 Tagname 也用於其他標簽。
XML文件:
<RootTag>
<humans>
<human>
<name>Max</name>
<age>22</age>
<friends>
<friend>
<name>Peter</name>
<adress>
<street>Way down 1</street>
</adress>
</friend>
<friend>
<name>Kevin</name>
<adress>
<street>Way left 2</street>
</adress>
</friend>
</friends>
</human>
<human>
<name>Justin</name>
<age>22</age>
<friends>
<friend>
<name>Georg</name>
<adress>
<street>Way up 1</street>
</adress>
</friend>
</friends>
</human>
</humans>
<friend>
<friends>
<name>Max</name>
<numberFriends>2</numberFriends>
</friends>
<friends>
<name>Justin</name>
<numberFriends>1</numberFriends>
</friends>
</friend>
</RootTag>
爪哇:
public static void parse() throws ParserConfigurationException, IOException, SAXException {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setValidating(true);
factory.setIgnoringElementContentWhitespace(true);
DocumentBuilder builder = factory.newDocumentBuilder();
File file = new File("humanFriends.xml");
Document doc = builder.parse(file);
NodeList humanL = doc.getElementsByTagName("human");
for (int j = 0; j < humanL.getLength(); j++) {
Node humanN = humanL.item(j);
if (humanN.getNodeType() == Node.ELEMENT_NODE) {
Element humanE = (Element) humanN;
String name = humanE.getElementsByTagName("name").item(0).getTextContent();
String vehicleId = humanE.getElementsByTagName("age").item(0).getTextContent();
...
}
NodeList friendsL = doc.getElementsByTagName("friends");
for (int j = 0; j < friendsL.getLength(); j++) {
Node friendsN = friendsL.item(j);
if (friendsN.getNodeType() == Node.ELEMENT_NODE) {
Element friendsE = (Element) friendsN;
String name = friendsE.getElementsByTagName("name").item(0).getTextContent();
String vehicleId = friendsE.getElementsByTagName("numberFriends").item(0).getTextContent();
here I'm getting error because parser take also friends from human Tag...
}
}
是否可以像分層一樣解析它或僅在特定子節點中解析標簽名? 即使在不同的節點中具有相同的標記名,是否也可以解析 XML,或者對於 XML 來說這是一個糟糕的結構嗎?
Element.getElementsByTagName("foo")
返回所有后代元素(具有給定標簽/元素名稱的當前元素)。 在您的代碼+示例中,這只會拋出一個令人討厭的 NPE,因為第一個friends
元素里面沒有numberFriends
。
現在你可以:
NullPointerException
(或以其他方式測試,您是否在正確的元素中......這不是我最喜歡的方法,不干凈,但非常務實,簡短且有效)。getElementsByTagName()
...,而是從相應的子元素中獲取。): (對於 2.)假設,您需要所有//humans/human
(<- XPATH) 元素的名稱+年齡以及所有//friend/friends
元素的名稱+numberFriends,您將執行以下操作:
import java.io.File;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class Test {
public static void main(String[] args) throws ParserConfigurationException, IOException, SAXException {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setValidating(true);
factory.setIgnoringElementContentWhitespace(true);
DocumentBuilder builder = factory.newDocumentBuilder();
File file = new File("humanFriends.xml");
Document doc = builder.parse(file);
NodeList humansL = doc.getElementsByTagName("humans");
//System.out.println(humansL.getLength());
for (int i = 0; i < humansL.getLength(); i++) {
Node humansN = humansL.item(i);
if (humansN.getNodeType() == Node.ELEMENT_NODE) {
NodeList humanL = ((Element) humansN).getElementsByTagName("human");
// System.out.println(humanL.getLength());
for (int j = 0; j < humanL.getLength(); j++) {
Node humanN = humanL.item(j);
if (humanN.getNodeType() == Node.ELEMENT_NODE) {
Element humanE = (Element) humanN;
String name = humanE.getElementsByTagName("name").item(0).getTextContent();
String age= humanE.getElementsByTagName("age").item(0).getTextContent();
System.out.println(name);
System.out.println(age);
}
}
}
}
NodeList friendsL = doc.getElementsByTagName("friend");
// System.out.println(friendsL.getLength());
for (int i = 0; i < friendsL.getLength(); i++) {
Node friendsN = friendsL.item(i);
if (friendsN.getNodeType() == Node.ELEMENT_NODE) {
NodeList friendL = ((Element) friendsN).getElementsByTagName("friends");
// System.out.println(friendL.getLength());
for (int j = 0; j < friendL.getLength(); j++) {
Node friendN = friendL.item(j);
if (friendN.getNodeType() == Node.ELEMENT_NODE) {
Element friendE = (Element) friendN;
String name = friendE.getElementsByTagName("name").item(0).getTextContent();
System.out.println(name);
String numberFriends = friendE.getElementsByTagName("numberFriends").item(0).getTextContent();
System.out.println(numberFriends);
}
}
}
}
}
}
請稍微改變您的(測試)“humanFriends.xml”中的值,尤其是要識別不明確的標簽名稱中的問題;)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.