【问题标题】:Problem with parsing XML file for the first time第一次解析xml文件出现问题
【发布时间】:2019-09-22 18:51:40
【问题描述】:

我第一次为我的作业解析一个 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();
            }

        }

    }

现在,每当我从尝试将它们分配给字符串变量的行中删除 cmets 时,如果我检查调试器,我可以看到它们是空的。我不明白为什么,因为他们之前只打印了一行。

那么很明显,当我尝试将字符串转换为 int 时,我遇到了一个错误,因为变量是空的。

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

任何帮助将不胜感激,因为我从昨晚开始一直在解决这个特殊问题,但运气不佳。

谢谢!

【问题讨论】:

  • 请添加输入的xml和输出的结果
  • 刚刚添加了 XML 文件。
  • 还添加了输出
  • 我刚刚将我的 cmets 编辑为英文。顺便说一句,noeuds 是 Node,racine 是 Root。

标签: java xml


【解决方案1】:

您的“usine”元素出现在两个部分 - metadonnees 和模拟。完整输出如下所示:

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++) {

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-09
    • 1970-01-01
    • 1970-01-01
    • 2023-03-25
    • 1970-01-01
    • 1970-01-01
    • 2011-06-13
    相关资源
    最近更新 更多