gpt4 book ai didi

java - 获取java xml中节点的节点的节点?

转载 作者:行者123 更新时间:2023-12-01 18:31:55 25 4
gpt4 key购买 nike

我没有太多的 java/xml 经验,所以这可能是微不足道的。但给定这样的 xml 结构:

<Things>
<object name="cat" id="0">
<prop id="1" name="race">ShortHair</prop>
</object>

<object name="car" id="1">
<prop id="1" name="Manufacturer">
<manufacturer id="1" name="ford">
</prop>
</object>

<object id="2" name="Window">
</object>
</Things>

是否可以获取特定节点,例如:

getObject("cat").getRace();

getObject("car").getId(1).getManufacturer();

getObject(id="2").name;

我知道,这不是有效的代码,但我没有找到似乎执行类似操作的 xml 示例。

谢谢。

最佳答案

是的,我们可以在 Java 中获取 XML 元素,并且有不同的方法。从教程的角度考虑这个示例。

XML:

<class>
<student rollno = "393">
<firstname>dinkar</firstname>
<lastname>kad</lastname>
<nickname>dinkar</nickname>
<marks>85</marks>
</student>

<student rollno = "493">
<firstname>Vaneet</firstname>
<lastname>Gupta</lastname>
<nickname>vinni</nickname>
<marks>95</marks>
</student>

<student rollno = "593">
<firstname>jasvir</firstname>
<lastname>singh</lastname>
<nickname>jazz</nickname>
<marks>90</marks>
</student>
</class>

Java:

import java.io.File;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
import org.xml.sax.SAXException;

public class XPathParserDemo {

public static void main(String[] args) {

try {
File inputFile = new File("input.txt");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder;

dBuilder = dbFactory.newDocumentBuilder();

Document doc = dBuilder.parse(inputFile);
doc.getDocumentElement().normalize();

XPath xPath = XPathFactory.newInstance().newXPath();

String expression = "/class/student";
NodeList nodeList = (NodeList) xPath.compile(expression).evaluate(
doc, XPathConstants.NODESET);

for (int i = 0; i < nodeList.getLength(); i++) {
Node nNode = nodeList.item(i);
System.out.println("\nCurrent Element :" + nNode.getNodeName());

if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
System.out.println("Student roll no :" + eElement.getAttribute("rollno"));
System.out.println("First Name : "
+ eElement
.getElementsByTagName("firstname")
.item(0)
.getTextContent());
System.out.println("Last Name : "
+ eElement
.getElementsByTagName("lastname")
.item(0)
.getTextContent());
System.out.println("Nick Name : "
+ eElement
.getElementsByTagName("nickname")
.item(0)
.getTextContent());
System.out.println("Marks : "
+ eElement
.getElementsByTagName("marks")
.item(0)
.getTextContent());
}
}
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (XPathExpressionException e) {
e.printStackTrace();
}
}
}

输出:

Current Element :student
Student roll no : 393
First Name : dinkar
Last Name : kad
Nick Name : dinkar
Marks : 85

Current Element :student
Student roll no : 493
First Name : Vaneet
Last Name : Gupta
Nick Name : vinni
Marks : 95

Current Element :student
Student roll no : 593
First Name : jasvir
Last Name : singh
Nick Name : jazz
Marks : 90

正如我所说,还有其他方法。检查this链接以获取更多此类示例。 XPath非常丰富。您可以使用 XPath 访问任何元素/属性。如果您想将 XML 消息转换为另一种 XML,您还可以使用 XSLT。

关于java - 获取java xml中节点的节点的节点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60148378/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com