简体   繁体   中英

Getting all attributes of an xml by using java and Xpath

I have the following xml:

<?xml version="1.0" encoding="UTF-8"?>
<root xmlns="http://www.test.com/rest/v1" xmlns:xs="http://www.w3.org/2001/XMLSchema">
   <child test="folder" id="2019-05-15-04.52.05.641880A01" />
   <child test="folder" id="2019-05-15-04.52.05.901880A02" />
</root>

I want to read the above xml by using Java code and Xpath, retrieve the id's of the child nodes(ie id="2019-05-15-04.52.05.641880A01" and id="2019-05-15-04.52.05.901880A02" ) and store them into List. I tried with the following java code:

        InputSource source = new InputSource(new StringReader(xml));
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        org.w3c.dom.Document document = db.parse(source);
        XPathFactory xpathFactory = XPathFactory.newInstance();
        XPath xpath = xpathFactory.newXPath();
        return xpath.evaluate(expression, document);

I called the above the above method with the following Xpath along with the input xml:

*[local-name()='root']/*[local-name()='child']/@id

But I am getting only one id , not all the id's. Any idea on how to get all the id's?

I think your Xpath is right. You can verify it with the following test class.

package com.idsk.commons.xsl;

import java.io.IOException;

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

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

public class Test {

    public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException, XPathExpressionException {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true); // never forget this!
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document doc = builder.parse("D://NewFile.xml");

        // Create XPath
        XPathFactory xpathfactory = XPathFactory.newInstance();
        XPath xpath = xpathfactory.newXPath();

        XPathExpression expr = xpath.compile("*[local-name()='root']/*[local-name()='child']/@id"); 

        Object result = expr.evaluate(doc, XPathConstants.NODESET);
        NodeList nodes = (NodeList) result;

        List<String> ids = new ArrayList<>();
        for (int i = 0; i < nodes.getLength(); i++) {
            System.out.println(nodes.item(i).getNodeValue());
            ids.add(nodes.item(i).getNodeValue()); //store them into List
        }
    }
}

It will create following output:

2019-05-15-04.52.05.641880A01

2019-05-15-04.52.05.901880A02

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM