java - 来自xml java的特定节点
问题描述
我的 xml 看起来像这样:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<metadonnees>
<usine type="usine-matiere">
<icones>
<icone type="vide" path="src/ressources/UMP0%.png"/>
<icone type="un-tiers" path="src/ressources/UMP33%.png"/>
<icone type="deux-tiers" path="src/ressources/UMP66%.png"/>
<icone type="plein" path="src/ressources/UMP100%.png"/>
</icones>
<sortie type = "metal"/>
<interval-production>100</interval-production>
</usine>
<usine type="usine-aile">
<icones>
<icone type="vide" path="src/ressources/UT0%.png"/>
<icone type="un-tiers" path="src/ressources/UT33%.png"/>
<icone type="deux-tiers" path="src/ressources/UT66%.png"/>
<icone type="plein" path="src/ressources/UT100%.png"/>
</icones>
<entree type="metal" quantite="2"/>
<sortie type="aile"/>
<interval-production>50</interval-production>
</usine>
<usine type="usine-moteur">
<icones>
<icone type="vide" path="src/ressources/UM0%.png"/>
<icone type="un-tiers" path="src/ressources/UM33%.png"/>
<icone type="deux-tiers" path="src/ressources/UM66%.png"/>
<icone type="plein" path="src/ressources/UM100%.png"/>
</icones>
<entree type="metal" quantite="4"/>
<sortie type="moteur"/>
<interval-production>75</interval-production>
</usine>
<usine type="usine-assemblage">
<icones>
<icone type="vide" path="src/ressources/UA0%.png"/>
<icone type="un-tiers" path="src/ressources/UA33%.png"/>
<icone type="deux-tiers" path="src/ressources/UA66%.png"/>
<icone type="plein" path="src/ressources/UA100%.png"/>
</icones>
<entree type="aile" quantite="2"/>
<entree type="moteur" quantite="4"/>
<sortie type="avion"/>
<interval-production>110</interval-production>
</usine>
<usine type="entrepot">
<icones>
<icone type="vide" path="src/ressources/E0%.png"/>
<icone type="un-tiers" path="src/ressources/E33%.png"/>
<icone type="deux-tiers" path="src/ressources/E66%.png"/>
<icone type="plein" path="src/ressources/E100%.png"/>
</icones>
<entree type="avion" capacite="5"/>
</usine>
</metadonnees>
<simulation>
<usine type="usine-matiere" id="11" x="32" y="32"/>
<usine type="usine-aile" id="21" x="320" y="32"/>
<usine type="usine-assemblage" id="41" x="160" y="192"/>
<usine type="entrepot" id="51" x="640" y="192"/>
<usine type="usine-matiere" id="13" x="544" y="576"/>
<usine type="usine-matiere" id="12" x="96" y="352"/>
<usine type="usine-moteur" id="31" x="320" y="352"/>
<chemins>
<chemin de="11" vers="21" />
<chemin de="21" vers="41" />
<chemin de="41" vers="51" />
<chemin de="12" vers="31" />
<chemin de="13" vers="31" />
<chemin de="31" vers="41" />
</chemins>
</simulation>
</configuration>
我的代码是这样的:
public class mainTest {
public static void main(String[] args) {
String url = "C:\\Users\\ladin\\eclipse-workspace\\TP1_LOG121\\src\\ressources\\configuration.xml";
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newDefaultInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new File(url));
doc.getDocumentElement().normalize();
System.out.println("Root element: " + doc.getDocumentElement().getNodeName());
NodeList nList = doc.getElementsByTagName("usine");
NodeList nList2 = doc.getElementsByTagName("icone");
for(int i = 0 ; i < nList.getLength(); i++) {
Node p = (Node) nList.item(i);
if(p.getNodeType()== Node.ELEMENT_NODE) {
Element usine = (Element) p;
System.out.println(usine.getAttribute("type"));
}
}
System.out.println();
System.out.println();
for(int j = 0 ; j< nList2.getLength() ; j++) {
Node p2 = nList2.item(j);
if(p2.getNodeType()== Node.ELEMENT_NODE) {
Element icone = (Element) p2;
System.out.println("état de l'usine : " + icone.getAttribute("type"));
System.out.println("chemin de la photo : " + icone.getAttribute("path"));
System.out.println();
System.out.println();
}
}
} catch(Exception ex) {
ex.printStackTrace();
}
}
}
我只想从我的模拟列表中获取数据,但是当我运行我的代码时,我得到了这个
- 根元素:配置
- usine-matiere --> 来自 metadonnees
- usine-aile --> 来自 metadonnees
- usine-moteur --> 来自 metadonnes
- usine-assemblage --> 来自 metadonnes
- entrepot --> 来自 metadonnes
- usine-matiere --> 来自模拟
- usine-aile --> 来自模拟
- usine-assemblage --> 来自模拟
- 转口->来自模拟
- usine-matiere --> 来自模拟
- usine-matiere --> 来自模拟
- usine-moteur --> 来自模拟
就像我说的,我只想要来自“模拟”而不是“metadonnes”的数据。
解决方案
目前,您正在从上到下进行迭代,您的顶级节点是配置节点。为了实现你的目标,你必须这样做:
NodeList nList = doc.getElementsByTagName("simulation"); // finds your simulation tag
然后你必须遍历它。
推荐阅读
- java - 将 JSON 键作为包含一系列数字的字符串传递
- python-3.x - 如何使用具有已定义 bin 边缘的 KBinsDiscretizer
- javascript - 无法读取 null 的属性“scrollIntoView”-VueJS
- dataframe - Pyspark 数据帧分组
- ajax - 如何检查是否使用 JSF / AjaxBehaviorEvent 检查了多个复选框
- python - TypeError:“str”和“float”的实例之间不支持“<” - Prettytable 错误
- python - 使用 BioPython 时,函数不会遍历整个列表
- azure - 在 Azure DevOps 中将依赖项复制到私有源
- node.js - Nodejs Puppeteer 没有启动
- node.js - NodeJS Loop 也不在等待 .then