使用 Java XPATH 解析 XML 以获取所有子节点及其数据 [英] Parsing XML to get all child nodes and their data using Java XPATH

查看:47
本文介绍了使用 Java XPATH 解析 XML 以获取所有子节点及其数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有像解析 xml 并获取所有子节点以及节点 <Employees></Employees>

Hi I have requirement like parsing xml and get all child nodes and there data between nodes <Employees> and </Employees>

我有像这样的 xml:

I have xml like:

<?xml version="1.0"?>
<Employees>
    <Employee emplid="1111" type="admin">
        <firstname>test1</firstname>
        <lastname>Watson</lastname>
        <age>30</age>
        <email>johnwatson@sh.com</email>
    </Employee>
    <Employee emplid="2222" type="admin">
        <firstname>Sherlock</firstname>
        <lastname>Homes</lastname>
        <age>32</age>
        <email>sherlock@sh.com</email>
    </Employee>
</Employees>

我需要像

<Employee emplid="1111" type="admin">
        <firstname>test1</firstname>
        <lastname>Watson</lastname>
        <age>30</age>
        <email>johnwatson@sh.com</email>
    </Employee>
    <Employee emplid="2222" type="admin">
        <firstname>Sherlock</firstname>
        <lastname>Homes</lastname>
        <age>32</age>
        <email>sherlock@sh.com</email>
    </Employee>

我试过下面的代码

 FileInputStream file = new FileInputStream(new File("E:\\test.xml"));

         DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();

         DocumentBuilder builder =  builderFactory.newDocumentBuilder();

         Document xmlDocument = builder.parse(file);

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

         System.out.println("*************************");
         String expression = "/Employees/*";
         System.out.println(expression);
         String email = xPath.compile(expression).evaluate(xmlDocument);
         System.out.println(email);

但我得到了像

  test1
        Watson
        30
        johnwatson@sh.com

我使用过像 /Employees/* 这样的表达式,但它不起作用

I have used expression like /Employees/* but its not working

有人能帮我做这件事吗?

can anyone help me in doing this?

推荐答案

首先,如果你想匹配每个 Employee,理想情况下你的 XPath 表达式应该是 Employee 而不是 <代码>/员工/*.如果你知道标签名称,你也不需要 XPath,你可以只做 xmlDocument.getElementsByTagName("Employee").

First, if you want to match each Employee, ideally your XPath expression should be Employee not /Employees/*. If you know the tag name, you also don't need XPath, you can just do xmlDocument.getElementsByTagName("Employee").

如果要将节点序列化为字符串,可以使用 Transformer,如下所示:

If you want to serialize a node to a String, you can use an Transformer, something like this:

Transformer t = TransformerFactory.newTransformer();
NodeList nodes = xmlDocument.getElementsByTagName("Employee");
for(int i = 0; i < nodes.getLength(); i++) {
    StringWriter sw = new StringWriter();
    t.transform(new DOMSource(nodes.item(i)), new StreamResult(sw));
    String serialized = sw.toString();
    System.out.println(serialized);
}

这篇关于使用 Java XPATH 解析 XML 以获取所有子节点及其数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆