Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在XML文件中找到所需标记的最佳方法是';重复了吗?_Java_Xml - Fatal编程技术网

Java 在XML文件中找到所需标记的最佳方法是';重复了吗?

Java 在XML文件中找到所需标记的最佳方法是';重复了吗?,java,xml,Java,Xml,第一个帖子在这里。我有一个多次包含标记“usine”的XML文件,我正在以一种似乎不正确的方式进行操作,我想看看是否有一种更优化的方法来进行操作。这是我第一次使用XML和Node/NodeList,所以我对它还是比较熟悉的 这是XML文件 <?xml version="1.0" encoding="UTF-8"?> <configuration> <metadonnees> <usine type="usine-matiere">

第一个帖子在这里。我有一个多次包含标记“usine”的XML文件,我正在以一种似乎不正确的方式进行操作,我想看看是否有一种更优化的方法来进行操作。这是我第一次使用XML和Node/NodeList,所以我对它还是比较熟悉的

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

<simulation>
    <usine type="usine-matiere" id="11" x="32" y="32"/>
    <usine type="usine-aile" id="21" x="320" y="32"/>
    <chemins>
        <chemin de="11" vers="21" />
        <chemin de="21" vers="41" />
    </chemins>
</simulation>

基本上,我必须创建两个节点列表,因为我想要的在标记中,而不是标记中的一个,所以第一个节点列表是在标记中定位我,然后我更深入地创建一个新的节点列表以找到我想要的。有更好的方法吗?我可能做得不对,所以我想知道你的答案。感谢

首先,按标记名获取元素(例如获取usine元素),它将返回该标记的
节点列表
。e、 g在
simulation
中,您有两个
usine
标记(长度为2的
NodeList

Second,您可以迭代此
Nodelist
并对每个节点(元素)执行任何操作,例如,您可以获取每个
usine
标记的属性(
x
y
id

总之

1-按标记名获取元素(节点列表)

2-迭代节点列表

3-处理节点(例如,在迭代过程中获取每个节点的属性(
x
y
id

我将您的场景编码如下

public static void main(String argv[]) throws ParserConfigurationException, IOException, SAXException {

        //Read xml file
        File fXmlFile = new File("/test.xml");
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document doc = dBuilder.parse(fXmlFile);

        //Get usine nodes
        NodeList nodeList = doc.getElementsByTagName("usine");

        //Iterate nodeList
        for (int temp = 0; temp < nodeList.getLength(); temp++) {

            //Get each node and process it
            Node node = nodeList.item(temp);

            if (node.getNodeType() == Node.ELEMENT_NODE) {

                //Print attributes of the node
                Element element = (Element) node;
                System.out.println("X = " + element.getAttribute("x"));
                System.out.println("Y = " + element.getAttribute("y"));
                System.out.println("ID = " + element.getAttribute("id"));

            }
        }
    }
publicstaticvoidmain(字符串argv[])抛出ParserConfigurationException、IOException、SAXException{
//读取xml文件
File fXmlFile=新文件(“/test.xml”);
DocumentBuilderFactory dbFactory=DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder=dbFactory.newDocumentBuilder();
documentdoc=dBuilder.parse(fXmlFile);
//获取usine节点
NodeList NodeList=doc.getElementsByTagName(“usine”);
//迭代节点列表
对于(int-temp=0;temp
在本文中,我们将使用
domparser
解析XML,您可以使用它来更加熟悉其他XML处理库
例如:SAX解析器、StAX解析器和JAXB,它们在速度和性能上都比
DOM解析器
好得多。

首先,通过标记名获取元素(例如获取usine的元素),它返回该标记的
节点列表
。例如在
模拟
中,您有两个
usine
标记(a
NodeList
,长度为2)

Second,您可以迭代此
Nodelist
并对每个节点(元素)执行任何操作,例如,您可以获取每个
usine
标记的属性(
x
y
id

总之

1-按标记名获取元素(节点列表)

2-迭代节点列表

3-处理节点(例如,在迭代过程中获取每个节点的属性(
x
y
id

我将您的场景编码如下

public static void main(String argv[]) throws ParserConfigurationException, IOException, SAXException {

        //Read xml file
        File fXmlFile = new File("/test.xml");
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document doc = dBuilder.parse(fXmlFile);

        //Get usine nodes
        NodeList nodeList = doc.getElementsByTagName("usine");

        //Iterate nodeList
        for (int temp = 0; temp < nodeList.getLength(); temp++) {

            //Get each node and process it
            Node node = nodeList.item(temp);

            if (node.getNodeType() == Node.ELEMENT_NODE) {

                //Print attributes of the node
                Element element = (Element) node;
                System.out.println("X = " + element.getAttribute("x"));
                System.out.println("Y = " + element.getAttribute("y"));
                System.out.println("ID = " + element.getAttribute("id"));

            }
        }
    }
publicstaticvoidmain(字符串argv[])抛出ParserConfigurationException、IOException、SAXException{
//读取xml文件
File fXmlFile=新文件(“/test.xml”);
DocumentBuilderFactory dbFactory=DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder=dbFactory.newDocumentBuilder();
documentdoc=dBuilder.parse(fXmlFile);
//获取usine节点
NodeList NodeList=doc.getElementsByTagName(“usine”);
//迭代节点列表
对于(int-temp=0;temp
在本文中,我们将使用
domparser
解析XML,您可以使用它来更加熟悉其他XML处理库 例如:SAX解析器、StAX解析器和JAXB,它们在速度和性能方面都比
DOM解析器
好得多