我是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部分中的数据 所以我可以获得出厂设置以及如何单独获取图标
我该如何解决这个问题?
答案 0 :(得分:0)
在您的代码中,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);
}
获取元数据中type
元素的所有factory
属性:
//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());
}
查询工厂的子节点时,需要以下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
答案 1 :(得分:0)
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>