Java 在XML文件中找到所需标记的最佳方法是';重复了吗?
第一个帖子在这里。我有一个多次包含标记“usine”的XML文件,我正在以一种似乎不正确的方式进行操作,我想看看是否有一种更优化的方法来进行操作。这是我第一次使用XML和Node/NodeList,所以我对它还是比较熟悉的 这是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">
<?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
标记(aNodeList
,长度为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解析器
好得多