如何根据 xpath 获取 xml 的一部分?

How can i get part of xml based on the xpath?

提问人:Singh 提问时间:12/7/2022 最后编辑:Singh 更新时间:12/7/2022 访问量:49

问:

我有一些这样的XML文档

<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
  <book category="cooking">
    <title lang="en">Everyday Italian</title>
    <author>Giada De Laurentiis</author>
    <year>2005</year>
    <price>30.00</price>
  </book>
  <book category="children">
    <title lang="en">Harry Potter</title>
    <author>J K. Rowling</author>
    <year>2005</year>
    <price>29.99</price>
  </book>
  <book category="web">
    <title lang="en">XQuery Kick Start</title>
    <author>James McGovern</author>
    <author>Per Bothner</author>
    <author>Kurt Cagle</author>
    <author>James Linn</author>
    <author>Vaidyanathan Nagarajan</author>
    <year>2003</year>
    <price>49.99</price>
  </book>
  <book category="web" cover="paperback">
    <title lang="en">Learning XML</title>
    <author>Erik T. Ray</author>
    <year>2003</year>
    <price>39.95</price>
  </book>
</bookstore>

我想获取此 xml 的子集,类似于字符串格式,想要获取将在另一个 xml 中使用的节点和值

<book category="cooking">
    <title lang="en">Everyday Italian</title>
    <author>Giada De Laurentiis</author>
    <year>2005</year>
    <price>30.00</price>
  </book>

我试过这个方法

public static String xmlParserUsingXpath(String response, String xpath) throws SAXException, IOException, XPathExpressionException {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = null;
        try {
            builder = factory.newDocumentBuilder();
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        }
        String xml = response.trim().replaceFirst("^([\\W]+)<", "<"); // <-- The XML SOAP response
        Document xmlDocument = builder.parse(new ByteArrayInputStream(xml.getBytes()));
        XPath xPath = XPathFactory.newInstance().newXPath();
        String record = xPath.compile(xpath).evaluate(xmlDocument);
        System.out.println("Record : " + record);
        return record;

    }

它只给我值 喜欢 日常意大利语 吉亚达·德·劳伦蒂斯(Giada De Laurentiis) 2005 29.99

Java XML 解析 DOM 文档

评论


答:

0赞 Mitchell Houston 12/7/2022 #1
try {
    File file = new File("F:\\XMLFile.xml");  
    DocumentBuilderFactory docBuilderFac = DocumentBuilderFactory.newInstance();  
    DocumentBuilder docBuilder = docBuilderFac.newDocumentBuilder(); 
    Document document = docBuilder.parse(file);  
    
    document.getDocumentElement().normalize();  
    
    NodeList nodeList = document.getElementsByTagName("book");  
    
    
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node node = nodeList.item(i);  
        
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            Element element = (Element) node;  
            System.out.println("title "+ eElement.getElementsByTagName("title").item(0).getTextContent());
            System.out.println("author "+ eElement.getElementsByTagName("author").item(0).getTextContent());
            System.out.println("year "+ eElement.getElementsByTagName("year").item(0).getTextContent());
            System.out.println("price "+ eElement.getElementsByTagName("price").item(0).getTextContent());
        }
        
    }
} catch(Exception e) {
    System.out.println(e.getMessage());
}

你需要一些 javax 的东西和一些 w3c 的东西来做到这一点。

评论

0赞 Singh 12/7/2022
我面临的问题是我不知道我正在处理的 xml 的标签,我只知道父节点的 xpath,它将是 /bookstore/book/@category=“cooking”。我需要在该 xpath 之后打印所有子节点和值
0赞 Mitchell Houston 12/8/2022
你能解释一下为什么你不知道 Node 元素是什么吗?