开发者

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

I have some XML document like this

<?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>

I want to ge开发者_开发知识库t subset of this xml , something like this in string format, want to get nodes and values which I will use in another xml

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

I tried this method

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;

    }

it gives me values only like Everyday Italian Giada De Laurentiis 2005 29.99


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());
}

You'll need some javax stuff and some w3c stuff to do this.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜