简体   繁体   中英

transform result of xpath to xml format

i've xml file as bellow :

<?xml version="1.0"?>
<all>
<test1>
hajarrrr rrr rr
</test1>
<catalog>
   <book id="bk101">
      <author>
<infos>Empire Burlesque</infos> 
</author>
      <title>XML Developer's Guide</title>
      <genre>Computer</genre>
      <price>44.95</price>
      <publish_date>2000-10-01</publish_date>
      <description>An in-depth look at creating applications 
      with XML.</description>
   </book>
   <book id="bk102">
      <author>
<infos>Emhhsjshhh</infos>  
</author>
      <title>Midnight Rain</title>
      <genre>Fantasy</genre>
      <price>5.95</price>
      <publish_date>2000-12-16</publish_date>
      <description>A former architect battles corporate zombies, 
      an evil sorceress, and her own childhood to become queen 
      of the world.</description>
   </book>
</catalog>
</all>

i want to extract only the block between <catalog> and </catalog> so i wrote this java code :

public static void main(String[] args) throws XPathExpressionException, SAXException, IOException, ParserConfigurationException, TransformerException {

        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document doc = builder.parse("C:\\Users\\HC\\Desktop\\dataset\\book.xml");
        XPathFactory xPathfactory = XPathFactory.newInstance();
        XPath xpath = xPathfactory.newXPath();
        //TODO: Is this correct query?
        XPathExpression xpathExp = xpath.compile("//text()[normalize-space(.) = '']");
        NodeList emptyTextNodes = (NodeList) xpathExp.evaluate(doc, XPathConstants.NODESET);
        for (int i = 0; i < emptyTextNodes.getLength(); i++) {
            Node emptyTextNode = emptyTextNodes.item(i);
            emptyTextNode.getParentNode().removeChild(emptyTextNode);
        }

        XPathExpression expr = xpath.compile("/all/catalog/descendant::node()");
        NodeList nl = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
        for (int i = 0; i < nl.getLength(); i++) {
            Node n = nl.item(i);

          System.out.println(n.getNodeName() + "   " + n.getNodeValue());

        }
    }

i want the result be as xml format , not like this :

book   null
author   null
infos   null
#text   Empire Burlesque
title   null
#text   XML Developer's Guide
genre   null
#text   Computer
price   null
#text   44.95
publish_date   null
#text   2000-10-01
description   null
#text   An in-depth look at creating applications 
      with XML.
book   null
author   null
infos   null
#text   Emhhsjshhh
title   null
#text   Midnight Rain
genre   null
#text   Fantasy
price   null
#text   5.95
publish_date   null
#text   2000-12-16
description   null
#text   A former architect battles corporate zombies, 
      an evil sorceress, and her own childhood to become queen 
      of the world.

in fact , i want result like this :

 <book id="bk101">
      <author>
<infos>Empire Burlesque</infos> 
</author>
      <title>XML Developer's Guide</title>
      <genre>Computer</genre>
      <price>44.95</price>
      <publish_date>2000-10-01</publish_date>
      <description>An in-depth look at creating applications 
      with XML.</description>
   </book>
   <book id="bk102">
      <author>
<infos>Emhhsjshhh</infos>  
</author>
      <title>Midnight Rain</title>
      <genre>Fantasy</genre>
      <price>5.95</price>
      <publish_date>2000-12-16</publish_date>
      <description>A former architect battles corporate zombies, 
      an evil sorceress, and her own childhood to become queen 
      of the world.</description>
   </book>

please could any one help :) , and thanks in advance.

¿What about a typical file parser?

File file = new File ("xml_path");
BufferedReader br = new BufferedReader (new FileReader (file));
String info;

while (info = br.readLine() != null)
{
    if (info.contains("<catalog>"))
    {
        System.out.println(info);
        while (info = br.readLine() != null)
        {
            if (info.contains("</catalog>"))
            {
                 System.out.println(info);
                 break;
            }
            else System.out.println(info);
        }
    }
}

That's just a simple text parser. I dont know if there is any better way to do it but I hope it helps you :)

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