首次解析XML文件时出现问题

时间:2019-09-22 18:51:40

标签: java xml

我第一次解析XML文件是为了做家庭作业。我遇到的问题可能是愚蠢的,但是我找不到问题所在。有些作品是法语,因为那是我的主要语言。

这是我遇到麻烦的那段代码。 因此,在最后几行代码中,我能够打印需要发送给构造函数的信息。

public void ParseXML(File fichierXML){

        final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

        {
            try {
                final DocumentBuilder builder = factory.newDocumentBuilder();
                final Document document = builder.parse(fichierXML);
                final Element racine = document.getDocumentElement();
                final NodeList racineNoeuds = racine.getChildNodes();
                final int nbRacineNoeuds = racineNoeuds.getLength();

                // Adjusting XML file
                document.getDocumentElement().normalize();

                // Printing out the main node
                System.out.println("Racine (root) : " + racine.getNodeName());

                for (int i = 0; i<nbRacineNoeuds; i++)
                {
                    if(racineNoeuds.item(i).getNodeType() == Node.ELEMENT_NODE)
                    {
                        final Element sousSection = (Element) racineNoeuds.item(i);
                        System.out.println("Sous-section : " + sousSection.getNodeName());

                        final NodeList usines = sousSection.getElementsByTagName("usine");
                        final int nbUsinesElements = usines.getLength();

                        for(int j = 0; j<nbUsinesElements; j++) {

                            final Element usine = (Element) usines.item(j);
                            String type = usine.getAttribute("type");
                            System.out.println(usine.getAttribute("type"));
                            String StringId = usine.getAttribute("id");
                            System.out.println(usine.getAttribute("id"));
                            String StringX = usine.getAttribute("x");
                            System.out.println(usine.getAttribute("x"));
                            String StringY = usine.getAttribute("y");
                            System.out.println(usine.getAttribute("y"));

                            //int id = Integer.parseInt(StringId);
                            //int x = Integer.parseInt(StringX);
                            //int y = Integer.parseInt(StringY);

                            //AjouterBatiment(new Usine(1, ImageIO.read(new File(("src/ressources/UMP0%.png"))),
                            //        new Point(x,y), 100));
                        }
                    }
                }

            } catch (ParserConfigurationException | IOException | SAXException |  NumberFormatException e) {
                e.printStackTrace();
            }

        }

    }

现在,每当我从试图将其分配给String变量的行中删除注释时,如果我检查调试器,就会发现它们为空。我不明白为什么,因为他们之前只打印了一行。

因此,很明显,在此之后,当我尝试将String转换为整数时,因为变量为空,我遇到了一个错误。

这是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>

以下是印刷品的输出:

usine-assemblage
41
160
192
entrepot
51
640
192
usine-matiere
13
544
576
usine-matiere
12
96
352
usine-moteur
31
320
352

自昨晚以来,我一直在努力解决这一特殊问题,运气不佳。

谢谢!

1 个答案:

答案 0 :(得分:0)

您的“正弦”元素将出现在两个部分中-异丁烯和模拟。完整的输出如下所示:

Racine : configuration
Sous-section : metadonnees
usine-matiere



usine-aile



usine-moteur



usine-assemblage



entrepot



Sous-section : simulation
usine-matiere
11
32
32
usine-aile
21
320
32
usine-assemblage
41
160
192
entrepot
51
640
192
usine-matiere
13
544
576
usine-matiere
12
96
352
usine-moteur
31
320
352

当调试器在断点处停止代码时,每当“命中”给定的行时,它就会停止。输出结果显示,前5个匹配包含来自“ metadonnes”的元素-因此这里没有问题,因为来自“ metadonnes”的元素不包含x,y和id属性。您需要跳过前5个停靠点才能在调试器中获取所需的数据。

您需要忽略这些“空”条目的操作-只需忽略“ metadonnes”节点中的所有内容。一种方法是仅在解析“模拟”部分时进入检索x,y和id属性的循环。

for(int j = 0; "simulation".equals(sousSection.getNodeName()) && j<nbUsinesElements; j++) {

此修改将允许您跳过不在“模拟”节点中的任何内容

相关问题