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.