首页 > 解决方案 > 来自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();
        }
        
        
    }

}

我只想从我的模拟列表中获取数据,但是当我运行我的代码时,我得到了这个

就像我说的,我只想要来自“模拟”而不是“metadonnes”的数据。

标签: javaxmlxml-parsing

解决方案


目前,您正在从上到下进行迭代,您的顶级节点是配置节点。为了实现你的目标,你必须这样做:

NodeList nList = doc.getElementsByTagName("simulation"); // finds your simulation tag

然后你必须遍历它。


推荐阅读