[英]reading XML file with java
我是Java的新手,我想解析一個xml文件
我有一個這樣的xml文件格式
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<metadonnees>
<factory type="factory1">
<icones>
<icone type="empty" path="src/ressources/UMP0%.png"/>
<icone type="half" path="src/ressources/UMP33%.png"/>
<icone type="full" path="src/ressources/UMP100%.png"/>
</icones>
<out type = "materiel1"/>
<interval>100</interval>
</factory>
<factory type="factory2">
<icones>
<icone type="empty" path="src/ressources/UT0%.png"/>
<icone type="half" path="src/ressources/UT33%.png"/>
<icone type="full" path="src/ressources/UT100%.png"/>
</icones>
<enter type="materiel1" quantite="2"/>
<out type="materiel2"/>
<interval> 2 </interval>
</factory>
</metadonnees>
<simulation>
<factory type="factoty1" id="11" x="32" y="32"/>
<factory type="factory2" id="21" x="320" y="32"/>
<paths>
<path de="11" vers="21" />
<path de="21" vers="41" />
<path de="41" vers="51" />
</paths>
</simulation>
</configuration>
我嘗試使用Java讀取它,但遇到了麻煩
NodeList config = document.getElementsByTagName("metadonnees");
for(int j = 0;j<config.getLength();j++) {
NodeList usineList = document.getElementsByTagName("factory");
for (int i = 0; i < usineList.getLength(); i++) {
Node usine = usineList.item(i);
if (usine.getNodeType() == Node.ELEMENT_NODE) {
Element type = (Element) usine;
String typeUsine = type.getAttribute("type");
System.out.println(typeUsine);
}
}
}
此列表僅列出我的工廠類型
我只想在metadonnee部分中列出數據,所以我可以獲取出廠設置以及如何分別獲取圖標
我該如何解決?
在您的代碼中, document.getElementsByTagName("factory");
返回所有帶有標簽<factory>
元素。 如果要限制在元數據metadonnees
部分中的選擇,則可以使用XPath(XML路徑語言)。
獲取元數據中的所有factory
元素:
//metadonnees/factory
Java代碼:
XPath xPath = XPathFactory.newInstance().newXPath();
XPathExpression expr = xPath.compile("//metadonnees/factory");
NodeList usineList = (NodeList) expr.evaluate(document, XPathConstants.NODESET);
for (int i = 0; i < usineList.getLength(); i++) {
Element usine = (Element) usineList.item(i);
String typeUsine = usine.getAttribute("type");
System.out.println(typeUsine);
}
獲取元數據中factory
元素的所有type
屬性:
//metadonnees/factory/@type
Java代碼:
XPath xPath = XPathFactory.newInstance().newXPath();
XPathExpression expr = xPath.compile("//metadonnees/factory/@type");
NodeList typeList = (NodeList) expr.evaluate(document, XPathConstants.NODESET);
for (int i = 0; i < typeList.getLength(); i++) {
Attr type = (Attr) typeList.item(i);
System.out.println(type.getValue());
}
查詢factory的子節點時,您需要以下xpath,這意味着查詢從當前節點( .
)開始,一直持續到元素<icone>
為止:
.//icone
Java代碼:
XPath xPath = XPathFactory.newInstance().newXPath();
NodeList usineList =
(NodeList) xPath.evaluate("//metadonnees/factory", document, XPathConstants.NODESET);
for (int i = 0; i < usineList.getLength(); i++) {
Element usine = (Element) usineList.item(i);
NodeList icons = (NodeList) xPath.evaluate(".//icone", usine, XPathConstants.NODESET);
System.out.println(usine.getAttribute("type"));
for (int j = 0; j < icons.getLength(); j++) {
Element icon = (Element) icons.item(j);
String type = icon.getAttribute("type");
String path = icon.getAttribute("path");
String msg = "type=" + type + ", path=" + path;
System.out.println(msg);
}
}
結果:
factory1
type=empty, path=src/ressources/UMP0%.png
type=half, path=src/ressources/UMP33%.png
type=full, path=src/ressources/UMP100%.png
factory2
type=empty, path=src/ressources/UT0%.png
type=half, path=src/ressources/UT33%.png
type=full, path=src/ressources/UT100%.png
SimpleXml可以做到這一點:
final String data = ...
final SimpleXml simple = new SimpleXml();
final Element element = simple.fromXml(data);
for (final Element factory : element.children.get(0).children) {
for (final Element icone : factory.children.get(0).children) {
System.out.println(String.format("Factory: %s, Icone: %s", factory.attributes.get("type"), icone.attributes.get("type")));
}
}
將輸出:
Factory: factory1, Icone: empty
Factory: factory1, Icone: half
Factory: factory1, Icone: full
Factory: factory2, Icone: empty
Factory: factory2, Icone: half
Factory: factory2, Icone: full
從Maven Central:
<dependency>
<groupId>com.github.codemonstur</groupId>
<artifactId>simplexml</artifactId>
<version>1.4.0</version>
</dependency>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.