Filter XML file by id? [duplicate] - java
I want to read XML data using XPath in Java, so for the information I have gathered I am not able to parse XML according to my requirement.
here is what I want to do:
Get XML file from online via its URL, then use XPath to parse it, I want to create two methods in it. One is in which I enter a specific node attribute id, and I get all the child nodes as result, and second is suppose I just want to get a specific child node value only
<?xml version="1.0"?>
<howto>
<topic name="Java">
<url>http://www.rgagnonjavahowto.htm</url>
<car>taxi</car>
</topic>
<topic name="PowerBuilder">
<url>http://www.rgagnon/pbhowto.htm</url>
<url>http://www.rgagnon/pbhowtonew.htm</url>
</topic>
<topic name="Javascript">
<url>http://www.rgagnon/jshowto.htm</url>
</topic>
<topic name="VBScript">
<url>http://www.rgagnon/vbshowto.htm</url>
</topic>
</howto>
In above example I want to read all the elements if I search via #name and also one function in which I just want the url from #name 'Javascript' only return one node element.
You need something along the lines of this:
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(<uri_as_string>);
XPathFactory xPathfactory = XPathFactory.newInstance();
XPath xpath = xPathfactory.newXPath();
XPathExpression expr = xpath.compile(<xpath_expression>);
Then you call expr.evaluate() passing in the document defined in that code and the return type you are expecting, and cast the result to the object type of the result.
If you need help with a specific XPath expressions, you should probably ask it as separate questions (unless that was your question in the first place here - I understood your question to be how to use the API in Java).
Edit: (Response to comment): This XPath expression will get you the text of the first URL element under PowerBuilder:
/howto/topic[#name='PowerBuilder']/url/text()
This will get you the second:
/howto/topic[#name='PowerBuilder']/url[2]/text()
You get that with this code:
expr.evaluate(doc, XPathConstants.STRING);
If you don't know how many URLs are in a given node, then you should rather do something like this:
XPathExpression expr = xpath.compile("/howto/topic[#name='PowerBuilder']/url");
NodeList nl = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
And then loop over the NodeList.
You can try this.
XML Document
Save as employees.xml.
<?xml version="1.0" encoding="UTF-8"?>
<Employees>
<Employee id="1">
<age>29</age>
<name>Pankaj</name>
<gender>Male</gender>
<role>Java Developer</role>
</Employee>
<Employee id="2">
<age>35</age>
<name>Lisa</name>
<gender>Female</gender>
<role>CEO</role>
</Employee>
<Employee id="3">
<age>40</age>
<name>Tom</name>
<gender>Male</gender>
<role>Manager</role>
</Employee>
<Employee id="4">
<age>25</age>
<name>Meghan</name>
<gender>Female</gender>
<role>Manager</role>
</Employee>
</Employees>
Parser class
The class have following methods
List item
A Method that will return the Employee Name for input ID.
A Method that will return list of Employees Name with age greater than the input age.
A Method that will return list of Female Employees Name.
Source Code
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
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 Parser {
public static void main(String[] args) {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
DocumentBuilder builder;
Document doc = null;
try {
builder = factory.newDocumentBuilder();
doc = builder.parse("employees.xml");
// Create XPathFactory object
XPathFactory xpathFactory = XPathFactory.newInstance();
// Create XPath object
XPath xpath = xpathFactory.newXPath();
String name = getEmployeeNameById(doc, xpath, 4);
System.out.println("Employee Name with ID 4: " + name);
List<String> names = getEmployeeNameWithAge(doc, xpath, 30);
System.out.println("Employees with 'age>30' are:" + Arrays.toString(names.toArray()));
List<String> femaleEmps = getFemaleEmployeesName(doc, xpath);
System.out.println("Female Employees names are:" +
Arrays.toString(femaleEmps.toArray()));
} catch (ParserConfigurationException | SAXException | IOException e) {
e.printStackTrace();
}
}
private static List<String> getFemaleEmployeesName(Document doc, XPath xpath) {
List<String> list = new ArrayList<>();
try {
//create XPathExpression object
XPathExpression expr =
xpath.compile("/Employees/Employee[gender='Female']/name/text()");
//evaluate expression result on XML document
NodeList nodes = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
for (int i = 0; i < nodes.getLength(); i++)
list.add(nodes.item(i).getNodeValue());
} catch (XPathExpressionException e) {
e.printStackTrace();
}
return list;
}
private static List<String> getEmployeeNameWithAge(Document doc, XPath xpath, int age) {
List<String> list = new ArrayList<>();
try {
XPathExpression expr =
xpath.compile("/Employees/Employee[age>" + age + "]/name/text()");
NodeList nodes = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
for (int i = 0; i < nodes.getLength(); i++)
list.add(nodes.item(i).getNodeValue());
} catch (XPathExpressionException e) {
e.printStackTrace();
}
return list;
}
private static String getEmployeeNameById(Document doc, XPath xpath, int id) {
String name = null;
try {
XPathExpression expr =
xpath.compile("/Employees/Employee[#id='" + id + "']/name/text()");
name = (String) expr.evaluate(doc, XPathConstants.STRING);
} catch (XPathExpressionException e) {
e.printStackTrace();
}
return name;
}
}
Getting started example:
xml file:
<inventory>
<book year="2000">
<title>Snow Crash</title>
<author>Neal Stephenson</author>
<publisher>Spectra</publisher>
<isbn>0553380958</isbn>
<price>14.95</price>
</book>
<book year="2005">
<title>Burning Tower</title>
<author>Larry Niven</author>
<author>Jerry Pournelle</author>
<publisher>Pocket</publisher>
<isbn>0743416910</isbn>
<price>5.99</price>
</book>
<book year="1995">
<title>Zodiac</title>
<author>Neal Stephenson</author>
<publisher>Spectra</publisher>
<isbn>0553573862</isbn>
<price>7.50</price>
</book>
<!-- more books... -->
</inventory>
Java code:
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
try {
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
Document doc = docBuilder.parse (new File("c:\\tmp\\my.xml"));
// normalize text representation
doc.getDocumentElement().normalize();
System.out.println ("Root element of the doc is " + doc.getDocumentElement().getNodeName());
NodeList listOfBooks = doc.getElementsByTagName("book");
int totalBooks = listOfBooks.getLength();
System.out.println("Total no of books : " + totalBooks);
for(int i=0; i<listOfBooks.getLength() ; i++) {
Node firstBookNode = listOfBooks.item(i);
if(firstBookNode.getNodeType() == Node.ELEMENT_NODE) {
Element firstElement = (Element)firstBookNode;
System.out.println("Year :"+firstElement.getAttribute("year"));
//-------
NodeList firstNameList = firstElement.getElementsByTagName("title");
Element firstNameElement = (Element)firstNameList.item(0);
NodeList textFNList = firstNameElement.getChildNodes();
System.out.println("title : " + ((Node)textFNList.item(0)).getNodeValue().trim());
}
}//end of for loop with s var
} catch (SAXParseException err) {
System.out.println ("** Parsing error" + ", line " + err.getLineNumber () + ", uri " + err.getSystemId ());
System.out.println(" " + err.getMessage ());
} catch (SAXException e) {
Exception x = e.getException ();
((x == null) ? e : x).printStackTrace ();
} catch (Throwable t) {
t.printStackTrace ();
}
Here is an example of processing xpath with vtd-xml... for heavy duty XML processing it is second to none. here is the a recent paper on this subject Processing XML with Java – A Performance Benchmark
import com.ximpleware.*;
public class changeAttrVal {
public static void main(String s[]) throws VTDException,java.io.UnsupportedEncodingException,java.io.IOException{
VTDGen vg = new VTDGen();
if (!vg.parseFile("input.xml", false))
return;
VTDNav vn = vg.getNav();
AutoPilot ap = new AutoPilot(vn);
XMLModifier xm = new XMLModifier(vn);
ap.selectXPath("/*/place[#id=\"p14\" and #initialMarking=\"2\"]/#initialMarking");
int i=0;
while((i=ap.evalXPath())!=-1){
xm.updateToken(i+1, "499");// change initial marking from 2 to 499
}
xm.output("new.xml");
}
}
If you have a xml like below
<e:Envelope
xmlns:d = "http://www.w3.org/2001/XMLSchema"
xmlns:e = "http://schemas.xmlsoap.org/soap/envelope/"
xmlns:wn0 = "http://systinet.com/xsd/SchemaTypes/"
xmlns:i = "http://www.w3.org/2001/XMLSchema-instance">
<e:Header>
<Friends>
<friend>
<Name>Testabc</Name>
<Age>12121</Age>
<Phone>Testpqr</Phone>
</friend>
</Friends>
</e:Header>
<e:Body>
<n0:ForAnsiHeaderOperResponse xmlns:n0 = "http://systinet.com/wsdl/com/magicsoftware/ibolt/localhost/ForAnsiHeader/ForAnsiHeaderImpl#ForAnsiHeaderOper?KExqYXZhL2xhbmcvU3RyaW5nOylMamF2YS9sYW5nL1N0cmluZzs=">
<response i:type = "d:string">12--abc--pqr</response>
</n0:ForAnsiHeaderOperResponse>
</e:Body>
</e:Envelope>
and wanted to extract the below xml
<e:Header>
<Friends>
<friend>
<Name>Testabc</Name>
<Age>12121</Age>
<Phone>Testpqr</Phone>
</friend>
</Friends>
</e:Header>
The below code helps to achieve the same
public static void main(String[] args) {
File fXmlFile = new File("C://Users//abhijitb//Desktop//Test.xml");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
Document document;
Node result = null;
try {
document = dbf.newDocumentBuilder().parse(fXmlFile);
XPath xPath = XPathFactory.newInstance().newXPath();
String xpathStr = "//Envelope//Header";
result = (Node) xPath.evaluate(xpathStr, document, XPathConstants.NODE);
System.out.println(nodeToString(result));
} catch (SAXException | IOException | ParserConfigurationException | XPathExpressionException
| TransformerException e) {
e.printStackTrace();
}
}
private static String nodeToString(Node node) throws TransformerException {
StringWriter buf = new StringWriter();
Transformer xform = TransformerFactory.newInstance().newTransformer();
xform.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
xform.transform(new DOMSource(node), new StreamResult(buf));
return (buf.toString());
}
Now if you want only the xml like below
<Friends>
<friend>
<Name>Testabc</Name>
<Age>12121</Age>
<Phone>Testpqr</Phone>
</friend>
</Friends>
You need to change the
String xpathStr = "//Envelope//Header"; to String xpathStr = "//Envelope//Header/*";
This shows you how to
Read in an XML file to a DOM
Filter out a set of Nodes with XPath
Perform a certain action on each of the extracted Nodes.
We will call the code with the following statement
processFilteredXml(xmlIn, xpathExpr,(node) -> {/*Do something...*/;});
In our case we want to print some creatorNames from a book.xml using "//book/creators/creator/creatorName" as xpath to perform a printNode action on each Node that matches the XPath.
Full code
#Test
public void printXml() {
try (InputStream in = readFile("book.xml")) {
processFilteredXml(in, "//book/creators/creator/creatorName", (node) -> {
printNode(node, System.out);
});
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private InputStream readFile(String yourSampleFile) {
return Thread.currentThread().getContextClassLoader().getResourceAsStream(yourSampleFile);
}
private void processFilteredXml(InputStream in, String xpath, Consumer<Node> process) {
Document doc = readXml(in);
NodeList list = filterNodesByXPath(doc, xpath);
for (int i = 0; i < list.getLength(); i++) {
Node node = list.item(i);
process.accept(node);
}
}
public Document readXml(InputStream xmlin) {
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
return db.parse(xmlin);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private NodeList filterNodesByXPath(Document doc, String xpathExpr) {
try {
XPathFactory xPathFactory = XPathFactory.newInstance();
XPath xpath = xPathFactory.newXPath();
XPathExpression expr = xpath.compile(xpathExpr);
Object eval = expr.evaluate(doc, XPathConstants.NODESET);
return (NodeList) eval;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private void printNode(Node node, PrintStream out) {
try {
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
StreamResult result = new StreamResult(new StringWriter());
DOMSource source = new DOMSource(node);
transformer.transform(source, result);
String xmlString = result.getWriter().toString();
out.println(xmlString);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
Prints
<creatorName>Fosmire, Michael</creatorName>
<creatorName>Wertz, Ruth</creatorName>
<creatorName>Purzer, Senay</creatorName>
For book.xml
<book>
<creators>
<creator>
<creatorName>Fosmire, Michael</creatorName>
<givenName>Michael</givenName>
<familyName>Fosmire</familyName>
</creator>
<creator>
<creatorName>Wertz, Ruth</creatorName>
<givenName>Ruth</givenName>
<familyName>Wertz</familyName>
</creator>
<creator>
<creatorName>Purzer, Senay</creatorName>
<givenName>Senay</givenName>
<familyName>Purzer</familyName>
</creator>
</creators>
<titles>
<title>Critical Engineering Literacy Test (CELT)</title>
</titles>
</book>
Expanding on the excellent answer by #bluish and #Yishai, here is how you make the NodeLists and node attributes support iterators, i.e. the for(Node n: nodelist) interface.
Use it like:
NodeList nl = ...
for(Node n : XmlUtil.asList(nl))
{...}
and
Node n = ...
for(Node attr : XmlUtil.asList(n.getAttributes())
{...}
The code:
/**
* Converts NodeList to an iterable construct.
* From: https://stackoverflow.com/a/19591302/779521
*/
public final class XmlUtil {
private XmlUtil() {}
public static List<Node> asList(NodeList n) {
return n.getLength() == 0 ? Collections.<Node>emptyList() : new NodeListWrapper(n);
}
static final class NodeListWrapper extends AbstractList<Node> implements RandomAccess {
private final NodeList list;
NodeListWrapper(NodeList l) {
this.list = l;
}
public Node get(int index) {
return this.list.item(index);
}
public int size() {
return this.list.getLength();
}
}
public static List<Node> asList(NamedNodeMap n) {
return n.getLength() == 0 ? Collections.<Node>emptyList() : new NodeMapWrapper(n);
}
static final class NodeMapWrapper extends AbstractList<Node> implements RandomAccess {
private final NamedNodeMap list;
NodeMapWrapper(NamedNodeMap l) {
this.list = l;
}
public Node get(int index) {
return this.list.item(index);
}
public int size() {
return this.list.getLength();
}
}
}
Read XML file using XPathFactory, SAXParserFactory and StAX (JSR-173).
Using XPath get node and its child data.
public static void main(String[] args) {
String xml = "<soapenv:Body xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/'>"
+ "<Yash:Data xmlns:Yash='http://Yash.stackoverflow.com/Services/Yash'>"
+ "<Yash:Tags>Java</Yash:Tags><Yash:Tags>Javascript</Yash:Tags><Yash:Tags>Selenium</Yash:Tags>"
+ "<Yash:Top>javascript</Yash:Top><Yash:User>Yash-777</Yash:User>"
+ "</Yash:Data></soapenv:Body>";
String jsonNameSpaces = "{'soapenv':'http://schemas.xmlsoap.org/soap/envelope/',"
+ "'Yash':'http://Yash.stackoverflow.com/Services/Yash'}";
String xpathExpression = "//Yash:Data";
Document doc1 = getDocument(false, "fileName", xml);
getNodesFromXpath(doc1, xpathExpression, jsonNameSpaces);
System.out.println("\n===== ***** =====");
Document doc2 = getDocument(true, "./books.xml", xml);
getNodesFromXpath(doc2, "//person", "{}");
}
static Document getDocument( boolean isFileName, String fileName, String xml ) {
Document doc = null;
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setValidating(false);
factory.setNamespaceAware(true);
factory.setIgnoringComments(true);
factory.setIgnoringElementContentWhitespace(true);
DocumentBuilder builder = factory.newDocumentBuilder();
if( isFileName ) {
File file = new File( fileName );
FileInputStream stream = new FileInputStream( file );
doc = builder.parse( stream );
} else {
doc = builder.parse( string2Source( xml ) );
}
} catch (SAXException | IOException e) {
e.printStackTrace();
} catch (ParserConfigurationException e) {
e.printStackTrace();
}
return doc;
}
/**
* ELEMENT_NODE[1],ATTRIBUTE_NODE[2],TEXT_NODE[3],CDATA_SECTION_NODE[4],
* ENTITY_REFERENCE_NODE[5],ENTITY_NODE[6],PROCESSING_INSTRUCTION_NODE[7],
* COMMENT_NODE[8],DOCUMENT_NODE[9],DOCUMENT_TYPE_NODE[10],DOCUMENT_FRAGMENT_NODE[11],NOTATION_NODE[12]
*/
public static void getNodesFromXpath( Document doc, String xpathExpression, String jsonNameSpaces ) {
try {
XPathFactory xpf = XPathFactory.newInstance();
XPath xpath = xpf.newXPath();
JSONObject namespaces = getJSONObjectNameSpaces(jsonNameSpaces);
if ( namespaces.size() > 0 ) {
NamespaceContextImpl nsContext = new NamespaceContextImpl();
Iterator<?> key = namespaces.keySet().iterator();
while (key.hasNext()) { // Apache WebServices Common Utilities
String pPrefix = key.next().toString();
String pURI = namespaces.get(pPrefix).toString();
nsContext.startPrefixMapping(pPrefix, pURI);
}
xpath.setNamespaceContext(nsContext );
}
XPathExpression compile = xpath.compile(xpathExpression);
NodeList nodeList = (NodeList) compile.evaluate(doc, XPathConstants.NODESET);
displayNodeList(nodeList);
} catch (XPathExpressionException e) {
e.printStackTrace();
}
}
static void displayNodeList( NodeList nodeList ) {
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
String NodeName = node.getNodeName();
NodeList childNodes = node.getChildNodes();
if ( childNodes.getLength() > 1 ) {
for (int j = 0; j < childNodes.getLength(); j++) {
Node child = childNodes.item(j);
short nodeType = child.getNodeType();
if ( nodeType == 1 ) {
System.out.format( "\n\t Node Name:[%s], Text[%s] ", child.getNodeName(), child.getTextContent() );
}
}
} else {
System.out.format( "\n Node Name:[%s], Text[%s] ", NodeName, node.getTextContent() );
}
}
}
static InputSource string2Source( String str ) {
InputSource inputSource = new InputSource( new StringReader( str ) );
return inputSource;
}
static JSONObject getJSONObjectNameSpaces( String jsonNameSpaces ) {
if(jsonNameSpaces.indexOf("'") > -1) jsonNameSpaces = jsonNameSpaces.replace("'", "\"");
JSONParser parser = new JSONParser();
JSONObject namespaces = null;
try {
namespaces = (JSONObject) parser.parse(jsonNameSpaces);
} catch (ParseException e) {
e.printStackTrace();
}
return namespaces;
}
XML Document
<?xml version="1.0" encoding="UTF-8"?>
<book>
<person>
<first>Yash</first>
<last>M</last>
<age>22</age>
</person>
<person>
<first>Bill</first>
<last>Gates</last>
<age>46</age>
</person>
<person>
<first>Steve</first>
<last>Jobs</last>
<age>40</age>
</person>
</book>
Out put for the given XPathExpression:
String xpathExpression = "//person/first";
/*OutPut:
Node Name:[first], Text[Yash]
Node Name:[first], Text[Bill]
Node Name:[first], Text[Steve] */
String xpathExpression = "//person";
/*OutPut:
Node Name:[first], Text[Yash]
Node Name:[last], Text[M]
Node Name:[age], Text[22]
Node Name:[first], Text[Bill]
Node Name:[last], Text[Gates]
Node Name:[age], Text[46]
Node Name:[first], Text[Steve]
Node Name:[last], Text[Jobs]
Node Name:[age], Text[40] */
String xpathExpression = "//Yash:Data";
/*OutPut:
Node Name:[Yash:Tags], Text[Java]
Node Name:[Yash:Tags], Text[Javascript]
Node Name:[Yash:Tags], Text[Selenium]
Node Name:[Yash:Top], Text[javascript]
Node Name:[Yash:User], Text[Yash-777] */
See this link for our own Implementation of NamespaceContext
Related
Store xml value as a Map using XPATH using JAVA
I am using XPATH to parse xml document,please find the xml below <?xml version="1.0" encoding="UTF-8"?> <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body> <bookEvent> <bookName>harry_potter</bookName> <bookEntity>comic</bookEntity> <bookEntityId>10987645</bookEntityId> <bookParameter> <name>Name1</name> <value>value1</value> </bookParameter> <bookParameter> <name>Name2</name> <value>value2</value> </bookParameter> <bookParameter> <name>Name3</name> <value>value3</value> </bookParameter> <bookParameter> <name>Name4</name> <value>value4</value> </bookParameter> <bookParameter> <name>Name5</name> <value>value5</value> </bookParameter> </bookEvent> </soap:Body> </soap:Envelope> Here I would like to convert BookParameters to Map like below {"Name1":"value1","Name2":"value2" etc} I have tried the below code and i can get a Map but not in the expected format, try{ Map<String,String> eventParameters = new HashMap<>(); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse("book.xml"); XPathFactory xPathfactory = XPathFactory.newInstance(); XPath xpath = xPathfactory.newXPath(); NodeList nodeList = (NodeList)xpath.compile("//bookEvent//eventParameter").evaluate(doc, XPathConstants.NODESET); for (int i = 0; i < nodeList.getLength(); i++) { Node node = nodeList.item(i); if(node.hasChildNodes()) { NodeList childNodes = node.getChildNodes(); for (int j = 0; j < childNodes.getLength(); j++) { Node childNode = childNodes.item(j); if (childNode.getNodeType() == Node.ELEMENT_NODE) { System.out.println(childNode.getNodeName()+"::"+childNode.getNodeValue()+"::"+childNode.getTextContent()); eventParameters.put(childNode.getTextContent(),childNode.getTextContent()); } } } } System.out.println("print map::"+eventParameters); } catch (Exception e) { e.printStackTrace(); } The output looks like this print map::{Name3=Name3, Name4=Name4, value5=value5, Name5=Name5, value2=value2, value1=value1, value4=value4, value3=value3, Name1=Name1, Name2=Name2} Please somebody guide me to create a below map from the xml,Any help would be appreciable. {"Name1":"value1","Name2":"value2" etc}
You can do it as a one-liner in XPath 3.1: map:merge(//bookParameter!map{string(name): string(value)}) => serialize(map{'method':'json'}) You can run XPath 3.1 from Java by installing Saxon-HE 9.8 (open source)
Use Below code : import java.io.File; import java.util.LinkedHashMap; import java.util.Map; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; public class ReadXMLFile { public static Map<String,String> hMap = new LinkedHashMap<>(); public static void main(String argv[]) { try { File fXmlFile = new File("C:\\Users\\jaikant\\Desktop\\QUESTION.xml"); DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); Document doc = dBuilder.parse(fXmlFile); doc.getDocumentElement().normalize(); NodeList nodeList = doc.getElementsByTagName("bookParameter"); for (int parameter = 0; parameter < nodeList.getLength(); parameter++) { Node node = nodeList.item(parameter); if (node.getNodeType() == Node.ELEMENT_NODE) { Element eElement = (Element) node; String name = eElement.getElementsByTagName("name").item(0).getTextContent(); String value = eElement.getElementsByTagName("value").item(0).getTextContent(); hMap.put(name, value); } } } catch (Exception e) { e.printStackTrace(); } hMap.forEach((h,k) -> { System.out.println(h + ":" + k); }); } } It will print exactly what you are looking for.
Java - Delete child node from dynamic XML
I want to delete a XML node that contains a PDF in Base64. This is an example: <?xml version="1.0" encoding="UTF-8"?> <getResult> <id>null</id> <pdf>ioje98fh23fjkiwf72322342</pdf> </getResult> First, I transform the XML in String to Document but the result is null. This is my code: DocumentBuilder dbf = null; Document doc = null; try { dbf = DocumentBuilderFactory.newInstance().newDocumentBuilder(); InputSource is = new InputSource(); is.setCharacterStream(new StringReader("<getResult><id>null</id><pdf>ioje98fh23fjkiwf72322342</pdf></getResult>")); doc = dbf.parse(is); NodeList children = doc. getChildNodes(); for (int i = 0; i < children.getLength(); i++) { Node currentChild = children.item(i); System.out.println(currentChild); } } catch (Exception e) { System.out.println(e.getMessage().toString()); } The result is always: [getResult: null] Considering that the main node can vary but the structure does not, How can I get the PDF node?
Here is the could you could use to retrieve the data. import java.io.StringReader; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.InputSource; import org.w3c.dom.CharacterData; public class LabFour { public static void main(String[] args) { DocumentBuilder dbf = null; Document doc = null; try { dbf = DocumentBuilderFactory.newInstance().newDocumentBuilder(); InputSource is = new InputSource(); is.setCharacterStream( new StringReader("<getResult><id>null</id><pdf>ioje98fh23fjkiwf72322342</pdf></getResult>")); doc = dbf.parse(is); NodeList nodes = doc.getElementsByTagName("getResult"); for (int i = 0; i < nodes.getLength(); i++) { Element element = (Element) nodes.item(i); NodeList name = element.getElementsByTagName("id"); Element line = (Element) name.item(0); System.out.println("id: " + getCharacterDataFromElement(line)); NodeList pdf = element.getElementsByTagName("pdf"); line = (Element) title.item(0); System.out.println("pdf: " + getCharacterDataFromElement(pdf)); } } catch (Exception e) { e.printStackTrace(); } } public static String getCharacterDataFromElement(Element e) { Node child = e.getFirstChild(); if (child instanceof CharacterData) { CharacterData cd = (CharacterData) child; return cd.getData(); } return "?"; } }
SimpleXml can do it: final SimpleXml simple = new SimpleXml(); final Element element = simple.fromXml(data); element.children.remove(1); System.out.println(simple.domToXml(element)); Will output: <getResult><id>null</id></getResult> From maven central: <dependency> <groupId>com.github.codemonstur</groupId> <artifactId>simplexml</artifactId> <version>1.4.0</version> </dependency>
Checking if particular node exists in Xml file using Xpath
I would like to check is code = "ABC" exists in my xml file using xPath.Can you please suggest me some methods for it? <metadata> <codes class = "class1"> <code code = "ABC"> <detail "blah blah"/> </code> </codes> <codes class = "class2"> <code code = "123"> <detail "blah blah"/> </code> </codes> </metadata> [EDIT] I did following. It reuturns null. XPath xPath = XPathFactory.newInstance().newXPath(); XPathExpression expr = xPath.compile("//codes/code[# code ='ABC']"); Object result = expr.evaluate(doc, XPathConstants.NODESET); NodeList nodes = (NodeList) result; for (int i = 0; i < nodes.getLength(); i++) { System.out.println("nodes: "+ nodes.item(i).getNodeValue()); }
I don't know how you tested your code because <detail "blah blah"/> is an incorrect xml construct it should be <detail x="blah blah"/> i.e. a name-value pair !! For the XPath Expression "//codes/code[# code ='ABC']" the nodes.item(i).getNodeValue()) is going to be null because it will return an Element. See the below Javadoc comment: A working sample: import java.io.ByteArrayInputStream; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.xpath.XPath; import javax.xml.xpath.XPathConstants; import javax.xml.xpath.XPathExpression; import javax.xml.xpath.XPathFactory; import org.w3c.dom.Document; import org.w3c.dom.NodeList; public class Test { public static void main(String[] args) throws Exception { Document doc = getDoc(); XPath xPath = XPathFactory.newInstance().newXPath(); XPathExpression expr = xPath.compile("//codes/code[#code ='ABC']"); Object result = expr.evaluate(doc, XPathConstants.NODESET); NodeList nodes = (NodeList) result; System.out.println("Have I found anything? " + (nodes.getLength() > 0 ? "Yes": "No")); for (int i = 0; i < nodes.getLength(); i++) { System.out.println("nodes: "+ nodes.item(i).getNodeValue()); } } private static Document getDoc() { String xml = "<metadata>"+ "<codes class = 'class1'>"+ "<code code='ABC'>"+ "<detail x='blah blah'/>"+ "</code>"+ "</codes>"+ "<codes class = 'class2'>"+ "<code code = '123'>"+ "<detail x='blah blah'/>"+ "</code>"+ "</codes>"+ "</metadata>"; DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); try { DocumentBuilder db = dbf.newDocumentBuilder(); Document dom = db.parse(new ByteArrayInputStream(xml.getBytes())); return dom; }catch(Exception pce) { pce.printStackTrace(); } return null; } }
How to append xml file in java
I've been using xml files to save data from my java program. I'm using the java DOM api. I want to add to the document by adding an element and then adding children to that element. I tried doing it using this code but when i run it it does nothing. Is there another way of doing it that would be simple and work better? is there a way i can get this code working? File file = new File("C:/users/peter/desktop/newxml.xml"); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); Document document = db.parse(file); Element newB = document.createElement("B"); Element newC = document.createElement("c"); newC.setTextContent("11"); Element newD = document.createElement("d"); newD.setTextContent("21"); Element newE = document.createElement("e"); newE.setTextContent("31"); newB.appendChild(newC); newB.appendChild(newD); newB.appendChild(newE); document.getDocumentElement().appendChild(newB);
This java code works to append new node to the xml file......it is based on DOM import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import java.io.FileOutputStream; public class writexml1 { public static void main (String args[]) { File docFile = new File("..\\jquery\\WebContent\\demo\\testing.xml"); Document doc = null; try { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); doc = db.parse(docFile); } catch (java.io.IOException e) { System.out.println("Can't find the file"); } catch (Exception e) { System.out.print("Problem parsing the file."); } Element root = doc.getDocumentElement(); System.out.println("The root element is " + root.getNodeName() + ".\n"); NodeList children = root.getChildNodes(); System.out.print("There are "+children.getLength()+" child elements.\n"); System.out.print("They are: \n"); //Print the file for (Node child = root.getFirstChild();child != null;child = child.getNextSibling()) { if (child.getNodeType() == child.TEXT_NODE) { System.out.println("Text: "+child.getNodeValue()); } else if (child.getNodeType() == child.ELEMENT_NODE) { System.out.println(child.getNodeName()+" = "+child.getFirstChild().getNodeValue()); } } //NodeList deleteElement = root.getElementsByTagName("staff"); //Node deleteNode= deleteElement.item(0); //root.removeChild(deleteNode); Element staffElement = doc.createElement("staff"); Node updateText = doc.createTextNode(""); staffElement.appendChild(updateText); // Element firstName = doc.createElement("firstname"); String str_firstName="added firstname"; Node firstNameNode = doc.createTextNode(str_firstName); firstName.appendChild(firstNameNode); staffElement.appendChild(firstName); // Element lastName = doc.createElement("lastname"); String str_lastName="added lastname"; Node lastNameNode = doc.createTextNode(str_lastName); lastName.appendChild(lastNameNode); staffElement.appendChild(lastName); // Element nickName = doc.createElement("nickname"); String str_nickName="added nickname"; Node nickNameNode = doc.createTextNode(str_nickName); nickName.appendChild(nickNameNode); staffElement.appendChild(nickName); // Element salary = doc.createElement("salary"); String str_salary="$10,000"; Node salaryNode = doc.createTextNode(str_salary); salary.appendChild(salaryNode); staffElement.appendChild(salary); // root.appendChild(staffElement); //Node StaffNode=(Node)updateElement; try{ String outputURL = "..\\jquery\\WebContent\\demo\\testing.xml"; DOMSource source = new DOMSource(doc); StreamResult result = new StreamResult(new FileOutputStream(outputURL)); TransformerFactory transFactory = TransformerFactory.newInstance(); Transformer transformer = transFactory.newTransformer(); transformer.transform(source, result); } catch (Exception e) { e.printStackTrace(); } } }
you should check out the JAXB API. If I understand right, you're xml looks like this: <B> <C>11</C> <D>21</D> <E>31</E> </B> So code would be: import javax.xml.bind.JAXBContext; import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlAccessType; #XmlRootElement #XmlAccessorType(XmlAccessType.FIELD) public class B { #XmlElement public String C; // sloppy, probably should be type Integer or something #XmlElement public String D; #XmlElement public String E; } // then, somewhere else in your code you want to serialize... B b = new B(); b.C = "11"; b.D = "21"; b.E = "31"; JAXBContext c = JAXBContext.newInstance(B.class); // where w is a Writer instance c.createMarshaller().marshal(b, w);
How to read XML using XPath in Java
I want to read XML data using XPath in Java, so for the information I have gathered I am not able to parse XML according to my requirement. here is what I want to do: Get XML file from online via its URL, then use XPath to parse it, I want to create two methods in it. One is in which I enter a specific node attribute id, and I get all the child nodes as result, and second is suppose I just want to get a specific child node value only <?xml version="1.0"?> <howto> <topic name="Java"> <url>http://www.rgagnonjavahowto.htm</url> <car>taxi</car> </topic> <topic name="PowerBuilder"> <url>http://www.rgagnon/pbhowto.htm</url> <url>http://www.rgagnon/pbhowtonew.htm</url> </topic> <topic name="Javascript"> <url>http://www.rgagnon/jshowto.htm</url> </topic> <topic name="VBScript"> <url>http://www.rgagnon/vbshowto.htm</url> </topic> </howto> In above example I want to read all the elements if I search via #name and also one function in which I just want the url from #name 'Javascript' only return one node element.
You need something along the lines of this: DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse(<uri_as_string>); XPathFactory xPathfactory = XPathFactory.newInstance(); XPath xpath = xPathfactory.newXPath(); XPathExpression expr = xpath.compile(<xpath_expression>); Then you call expr.evaluate() passing in the document defined in that code and the return type you are expecting, and cast the result to the object type of the result. If you need help with a specific XPath expressions, you should probably ask it as separate questions (unless that was your question in the first place here - I understood your question to be how to use the API in Java). Edit: (Response to comment): This XPath expression will get you the text of the first URL element under PowerBuilder: /howto/topic[#name='PowerBuilder']/url/text() This will get you the second: /howto/topic[#name='PowerBuilder']/url[2]/text() You get that with this code: expr.evaluate(doc, XPathConstants.STRING); If you don't know how many URLs are in a given node, then you should rather do something like this: XPathExpression expr = xpath.compile("/howto/topic[#name='PowerBuilder']/url"); NodeList nl = (NodeList) expr.evaluate(doc, XPathConstants.NODESET); And then loop over the NodeList.
You can try this. XML Document Save as employees.xml. <?xml version="1.0" encoding="UTF-8"?> <Employees> <Employee id="1"> <age>29</age> <name>Pankaj</name> <gender>Male</gender> <role>Java Developer</role> </Employee> <Employee id="2"> <age>35</age> <name>Lisa</name> <gender>Female</gender> <role>CEO</role> </Employee> <Employee id="3"> <age>40</age> <name>Tom</name> <gender>Male</gender> <role>Manager</role> </Employee> <Employee id="4"> <age>25</age> <name>Meghan</name> <gender>Female</gender> <role>Manager</role> </Employee> </Employees> Parser class The class have following methods List item A Method that will return the Employee Name for input ID. A Method that will return list of Employees Name with age greater than the input age. A Method that will return list of Female Employees Name. Source Code import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import java.util.List; 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 Parser { public static void main(String[] args) { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); DocumentBuilder builder; Document doc = null; try { builder = factory.newDocumentBuilder(); doc = builder.parse("employees.xml"); // Create XPathFactory object XPathFactory xpathFactory = XPathFactory.newInstance(); // Create XPath object XPath xpath = xpathFactory.newXPath(); String name = getEmployeeNameById(doc, xpath, 4); System.out.println("Employee Name with ID 4: " + name); List<String> names = getEmployeeNameWithAge(doc, xpath, 30); System.out.println("Employees with 'age>30' are:" + Arrays.toString(names.toArray())); List<String> femaleEmps = getFemaleEmployeesName(doc, xpath); System.out.println("Female Employees names are:" + Arrays.toString(femaleEmps.toArray())); } catch (ParserConfigurationException | SAXException | IOException e) { e.printStackTrace(); } } private static List<String> getFemaleEmployeesName(Document doc, XPath xpath) { List<String> list = new ArrayList<>(); try { //create XPathExpression object XPathExpression expr = xpath.compile("/Employees/Employee[gender='Female']/name/text()"); //evaluate expression result on XML document NodeList nodes = (NodeList) expr.evaluate(doc, XPathConstants.NODESET); for (int i = 0; i < nodes.getLength(); i++) list.add(nodes.item(i).getNodeValue()); } catch (XPathExpressionException e) { e.printStackTrace(); } return list; } private static List<String> getEmployeeNameWithAge(Document doc, XPath xpath, int age) { List<String> list = new ArrayList<>(); try { XPathExpression expr = xpath.compile("/Employees/Employee[age>" + age + "]/name/text()"); NodeList nodes = (NodeList) expr.evaluate(doc, XPathConstants.NODESET); for (int i = 0; i < nodes.getLength(); i++) list.add(nodes.item(i).getNodeValue()); } catch (XPathExpressionException e) { e.printStackTrace(); } return list; } private static String getEmployeeNameById(Document doc, XPath xpath, int id) { String name = null; try { XPathExpression expr = xpath.compile("/Employees/Employee[#id='" + id + "']/name/text()"); name = (String) expr.evaluate(doc, XPathConstants.STRING); } catch (XPathExpressionException e) { e.printStackTrace(); } return name; } }
Getting started example: xml file: <inventory> <book year="2000"> <title>Snow Crash</title> <author>Neal Stephenson</author> <publisher>Spectra</publisher> <isbn>0553380958</isbn> <price>14.95</price> </book> <book year="2005"> <title>Burning Tower</title> <author>Larry Niven</author> <author>Jerry Pournelle</author> <publisher>Pocket</publisher> <isbn>0743416910</isbn> <price>5.99</price> </book> <book year="1995"> <title>Zodiac</title> <author>Neal Stephenson</author> <publisher>Spectra</publisher> <isbn>0553573862</isbn> <price>7.50</price> </book> <!-- more books... --> </inventory> Java code: import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.testng.annotations.DataProvider; import org.testng.annotations.Test; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.SAXException; import org.xml.sax.SAXParseException; try { DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder(); Document doc = docBuilder.parse (new File("c:\\tmp\\my.xml")); // normalize text representation doc.getDocumentElement().normalize(); System.out.println ("Root element of the doc is " + doc.getDocumentElement().getNodeName()); NodeList listOfBooks = doc.getElementsByTagName("book"); int totalBooks = listOfBooks.getLength(); System.out.println("Total no of books : " + totalBooks); for(int i=0; i<listOfBooks.getLength() ; i++) { Node firstBookNode = listOfBooks.item(i); if(firstBookNode.getNodeType() == Node.ELEMENT_NODE) { Element firstElement = (Element)firstBookNode; System.out.println("Year :"+firstElement.getAttribute("year")); //------- NodeList firstNameList = firstElement.getElementsByTagName("title"); Element firstNameElement = (Element)firstNameList.item(0); NodeList textFNList = firstNameElement.getChildNodes(); System.out.println("title : " + ((Node)textFNList.item(0)).getNodeValue().trim()); } }//end of for loop with s var } catch (SAXParseException err) { System.out.println ("** Parsing error" + ", line " + err.getLineNumber () + ", uri " + err.getSystemId ()); System.out.println(" " + err.getMessage ()); } catch (SAXException e) { Exception x = e.getException (); ((x == null) ? e : x).printStackTrace (); } catch (Throwable t) { t.printStackTrace (); }
Here is an example of processing xpath with vtd-xml... for heavy duty XML processing it is second to none. here is the a recent paper on this subject Processing XML with Java – A Performance Benchmark import com.ximpleware.*; public class changeAttrVal { public static void main(String s[]) throws VTDException,java.io.UnsupportedEncodingException,java.io.IOException{ VTDGen vg = new VTDGen(); if (!vg.parseFile("input.xml", false)) return; VTDNav vn = vg.getNav(); AutoPilot ap = new AutoPilot(vn); XMLModifier xm = new XMLModifier(vn); ap.selectXPath("/*/place[#id=\"p14\" and #initialMarking=\"2\"]/#initialMarking"); int i=0; while((i=ap.evalXPath())!=-1){ xm.updateToken(i+1, "499");// change initial marking from 2 to 499 } xm.output("new.xml"); } }
If you have a xml like below <e:Envelope xmlns:d = "http://www.w3.org/2001/XMLSchema" xmlns:e = "http://schemas.xmlsoap.org/soap/envelope/" xmlns:wn0 = "http://systinet.com/xsd/SchemaTypes/" xmlns:i = "http://www.w3.org/2001/XMLSchema-instance"> <e:Header> <Friends> <friend> <Name>Testabc</Name> <Age>12121</Age> <Phone>Testpqr</Phone> </friend> </Friends> </e:Header> <e:Body> <n0:ForAnsiHeaderOperResponse xmlns:n0 = "http://systinet.com/wsdl/com/magicsoftware/ibolt/localhost/ForAnsiHeader/ForAnsiHeaderImpl#ForAnsiHeaderOper?KExqYXZhL2xhbmcvU3RyaW5nOylMamF2YS9sYW5nL1N0cmluZzs="> <response i:type = "d:string">12--abc--pqr</response> </n0:ForAnsiHeaderOperResponse> </e:Body> </e:Envelope> and wanted to extract the below xml <e:Header> <Friends> <friend> <Name>Testabc</Name> <Age>12121</Age> <Phone>Testpqr</Phone> </friend> </Friends> </e:Header> The below code helps to achieve the same public static void main(String[] args) { File fXmlFile = new File("C://Users//abhijitb//Desktop//Test.xml"); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); Document document; Node result = null; try { document = dbf.newDocumentBuilder().parse(fXmlFile); XPath xPath = XPathFactory.newInstance().newXPath(); String xpathStr = "//Envelope//Header"; result = (Node) xPath.evaluate(xpathStr, document, XPathConstants.NODE); System.out.println(nodeToString(result)); } catch (SAXException | IOException | ParserConfigurationException | XPathExpressionException | TransformerException e) { e.printStackTrace(); } } private static String nodeToString(Node node) throws TransformerException { StringWriter buf = new StringWriter(); Transformer xform = TransformerFactory.newInstance().newTransformer(); xform.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); xform.transform(new DOMSource(node), new StreamResult(buf)); return (buf.toString()); } Now if you want only the xml like below <Friends> <friend> <Name>Testabc</Name> <Age>12121</Age> <Phone>Testpqr</Phone> </friend> </Friends> You need to change the String xpathStr = "//Envelope//Header"; to String xpathStr = "//Envelope//Header/*";
This shows you how to Read in an XML file to a DOM Filter out a set of Nodes with XPath Perform a certain action on each of the extracted Nodes. We will call the code with the following statement processFilteredXml(xmlIn, xpathExpr,(node) -> {/*Do something...*/;}); In our case we want to print some creatorNames from a book.xml using "//book/creators/creator/creatorName" as xpath to perform a printNode action on each Node that matches the XPath. Full code #Test public void printXml() { try (InputStream in = readFile("book.xml")) { processFilteredXml(in, "//book/creators/creator/creatorName", (node) -> { printNode(node, System.out); }); } catch (Exception e) { throw new RuntimeException(e); } } private InputStream readFile(String yourSampleFile) { return Thread.currentThread().getContextClassLoader().getResourceAsStream(yourSampleFile); } private void processFilteredXml(InputStream in, String xpath, Consumer<Node> process) { Document doc = readXml(in); NodeList list = filterNodesByXPath(doc, xpath); for (int i = 0; i < list.getLength(); i++) { Node node = list.item(i); process.accept(node); } } public Document readXml(InputStream xmlin) { try { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); return db.parse(xmlin); } catch (Exception e) { throw new RuntimeException(e); } } private NodeList filterNodesByXPath(Document doc, String xpathExpr) { try { XPathFactory xPathFactory = XPathFactory.newInstance(); XPath xpath = xPathFactory.newXPath(); XPathExpression expr = xpath.compile(xpathExpr); Object eval = expr.evaluate(doc, XPathConstants.NODESET); return (NodeList) eval; } catch (Exception e) { throw new RuntimeException(e); } } private void printNode(Node node, PrintStream out) { try { Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); StreamResult result = new StreamResult(new StringWriter()); DOMSource source = new DOMSource(node); transformer.transform(source, result); String xmlString = result.getWriter().toString(); out.println(xmlString); } catch (Exception e) { throw new RuntimeException(e); } } Prints <creatorName>Fosmire, Michael</creatorName> <creatorName>Wertz, Ruth</creatorName> <creatorName>Purzer, Senay</creatorName> For book.xml <book> <creators> <creator> <creatorName>Fosmire, Michael</creatorName> <givenName>Michael</givenName> <familyName>Fosmire</familyName> </creator> <creator> <creatorName>Wertz, Ruth</creatorName> <givenName>Ruth</givenName> <familyName>Wertz</familyName> </creator> <creator> <creatorName>Purzer, Senay</creatorName> <givenName>Senay</givenName> <familyName>Purzer</familyName> </creator> </creators> <titles> <title>Critical Engineering Literacy Test (CELT)</title> </titles> </book>
Expanding on the excellent answer by #bluish and #Yishai, here is how you make the NodeLists and node attributes support iterators, i.e. the for(Node n: nodelist) interface. Use it like: NodeList nl = ... for(Node n : XmlUtil.asList(nl)) {...} and Node n = ... for(Node attr : XmlUtil.asList(n.getAttributes()) {...} The code: /** * Converts NodeList to an iterable construct. * From: https://stackoverflow.com/a/19591302/779521 */ public final class XmlUtil { private XmlUtil() {} public static List<Node> asList(NodeList n) { return n.getLength() == 0 ? Collections.<Node>emptyList() : new NodeListWrapper(n); } static final class NodeListWrapper extends AbstractList<Node> implements RandomAccess { private final NodeList list; NodeListWrapper(NodeList l) { this.list = l; } public Node get(int index) { return this.list.item(index); } public int size() { return this.list.getLength(); } } public static List<Node> asList(NamedNodeMap n) { return n.getLength() == 0 ? Collections.<Node>emptyList() : new NodeMapWrapper(n); } static final class NodeMapWrapper extends AbstractList<Node> implements RandomAccess { private final NamedNodeMap list; NodeMapWrapper(NamedNodeMap l) { this.list = l; } public Node get(int index) { return this.list.item(index); } public int size() { return this.list.getLength(); } } }
Read XML file using XPathFactory, SAXParserFactory and StAX (JSR-173). Using XPath get node and its child data. public static void main(String[] args) { String xml = "<soapenv:Body xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/'>" + "<Yash:Data xmlns:Yash='http://Yash.stackoverflow.com/Services/Yash'>" + "<Yash:Tags>Java</Yash:Tags><Yash:Tags>Javascript</Yash:Tags><Yash:Tags>Selenium</Yash:Tags>" + "<Yash:Top>javascript</Yash:Top><Yash:User>Yash-777</Yash:User>" + "</Yash:Data></soapenv:Body>"; String jsonNameSpaces = "{'soapenv':'http://schemas.xmlsoap.org/soap/envelope/'," + "'Yash':'http://Yash.stackoverflow.com/Services/Yash'}"; String xpathExpression = "//Yash:Data"; Document doc1 = getDocument(false, "fileName", xml); getNodesFromXpath(doc1, xpathExpression, jsonNameSpaces); System.out.println("\n===== ***** ====="); Document doc2 = getDocument(true, "./books.xml", xml); getNodesFromXpath(doc2, "//person", "{}"); } static Document getDocument( boolean isFileName, String fileName, String xml ) { Document doc = null; try { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(false); factory.setNamespaceAware(true); factory.setIgnoringComments(true); factory.setIgnoringElementContentWhitespace(true); DocumentBuilder builder = factory.newDocumentBuilder(); if( isFileName ) { File file = new File( fileName ); FileInputStream stream = new FileInputStream( file ); doc = builder.parse( stream ); } else { doc = builder.parse( string2Source( xml ) ); } } catch (SAXException | IOException e) { e.printStackTrace(); } catch (ParserConfigurationException e) { e.printStackTrace(); } return doc; } /** * ELEMENT_NODE[1],ATTRIBUTE_NODE[2],TEXT_NODE[3],CDATA_SECTION_NODE[4], * ENTITY_REFERENCE_NODE[5],ENTITY_NODE[6],PROCESSING_INSTRUCTION_NODE[7], * COMMENT_NODE[8],DOCUMENT_NODE[9],DOCUMENT_TYPE_NODE[10],DOCUMENT_FRAGMENT_NODE[11],NOTATION_NODE[12] */ public static void getNodesFromXpath( Document doc, String xpathExpression, String jsonNameSpaces ) { try { XPathFactory xpf = XPathFactory.newInstance(); XPath xpath = xpf.newXPath(); JSONObject namespaces = getJSONObjectNameSpaces(jsonNameSpaces); if ( namespaces.size() > 0 ) { NamespaceContextImpl nsContext = new NamespaceContextImpl(); Iterator<?> key = namespaces.keySet().iterator(); while (key.hasNext()) { // Apache WebServices Common Utilities String pPrefix = key.next().toString(); String pURI = namespaces.get(pPrefix).toString(); nsContext.startPrefixMapping(pPrefix, pURI); } xpath.setNamespaceContext(nsContext ); } XPathExpression compile = xpath.compile(xpathExpression); NodeList nodeList = (NodeList) compile.evaluate(doc, XPathConstants.NODESET); displayNodeList(nodeList); } catch (XPathExpressionException e) { e.printStackTrace(); } } static void displayNodeList( NodeList nodeList ) { for (int i = 0; i < nodeList.getLength(); i++) { Node node = nodeList.item(i); String NodeName = node.getNodeName(); NodeList childNodes = node.getChildNodes(); if ( childNodes.getLength() > 1 ) { for (int j = 0; j < childNodes.getLength(); j++) { Node child = childNodes.item(j); short nodeType = child.getNodeType(); if ( nodeType == 1 ) { System.out.format( "\n\t Node Name:[%s], Text[%s] ", child.getNodeName(), child.getTextContent() ); } } } else { System.out.format( "\n Node Name:[%s], Text[%s] ", NodeName, node.getTextContent() ); } } } static InputSource string2Source( String str ) { InputSource inputSource = new InputSource( new StringReader( str ) ); return inputSource; } static JSONObject getJSONObjectNameSpaces( String jsonNameSpaces ) { if(jsonNameSpaces.indexOf("'") > -1) jsonNameSpaces = jsonNameSpaces.replace("'", "\""); JSONParser parser = new JSONParser(); JSONObject namespaces = null; try { namespaces = (JSONObject) parser.parse(jsonNameSpaces); } catch (ParseException e) { e.printStackTrace(); } return namespaces; } XML Document <?xml version="1.0" encoding="UTF-8"?> <book> <person> <first>Yash</first> <last>M</last> <age>22</age> </person> <person> <first>Bill</first> <last>Gates</last> <age>46</age> </person> <person> <first>Steve</first> <last>Jobs</last> <age>40</age> </person> </book> Out put for the given XPathExpression: String xpathExpression = "//person/first"; /*OutPut: Node Name:[first], Text[Yash] Node Name:[first], Text[Bill] Node Name:[first], Text[Steve] */ String xpathExpression = "//person"; /*OutPut: Node Name:[first], Text[Yash] Node Name:[last], Text[M] Node Name:[age], Text[22] Node Name:[first], Text[Bill] Node Name:[last], Text[Gates] Node Name:[age], Text[46] Node Name:[first], Text[Steve] Node Name:[last], Text[Jobs] Node Name:[age], Text[40] */ String xpathExpression = "//Yash:Data"; /*OutPut: Node Name:[Yash:Tags], Text[Java] Node Name:[Yash:Tags], Text[Javascript] Node Name:[Yash:Tags], Text[Selenium] Node Name:[Yash:Top], Text[javascript] Node Name:[Yash:User], Text[Yash-777] */ See this link for our own Implementation of NamespaceContext