I have an XML likewise
<?xml version="1.0" encoding="UTF-8"?>
<QDTM_IN300301QD ITSVersion="XML_1.0" xmlns="urn:hl7-org:v3"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="urn:hl7-org:v3 QDTM_IN300401QD.xsd ">
<controlActEvent classCode="CACT" moodCode="EVN">
<code code="QDTM_TE300401QD">
</code>
<statusCode code="Active" />
<subject contextConductionInd="true" contextControlCode="ON"
typeCode="SUBJ">
<registrationEvent classCode="REG" moodCode="EVN">
<statusCode code="token" />
<subject contextControlCode="AN" typeCode="SBJ">
<testCodeIdentifier classCode="ROL">
<playingTestCodeDetails classCode="ENT"
determinerCode="INSTANCE">
<code code="6399Z" codeSystemName="QTIM" codeSystemVersion="Updated">
<originalText><![CDATA[CBC (includes Differential and Platelets)]]></originalText>
<translation codeSystemName="DOSCATALOGNAMEHTMLENABLED">
<originalText><![CDATA[CBC (includes Differential and Platelets)]]></originalText>
</translation>
</code>
</playingTestCodeDetails>
</testCodeIdentifier>
</subject>
</registrationEvent>
</subject>
</controlActEvent>
</QDTM_IN300301QD>
JAVA CODE:
package com.parse;
import java.io.IOException;
import java.util.Iterator;
import javax.xml.namespace.NamespaceContext;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;
public class ParseXPath {
public String parseXML(String fileName) {
fileName = "D://projects//Draft.xml";
DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
domFactory.setNamespaceAware(true);
DocumentBuilder builder;
Document doc;
try {
builder = domFactory.newDocumentBuilder();
doc = builder.parse(fileName);
XPath xpath = XPathFactory.newInstance().newXPath();
xpath.setNamespaceContext(new NamespaceContext(){public String getNamespaceURI(String prefix) {
return "urn:hl7-org:v3";
}
public String getPrefix(String namespaceURI) {
return null; // we are not using this.
}
public Iterator getPrefixes(String namespaceURI) {
return null; // we are not using this.
}
});
String expr="//QDTM_IN300401QD/controlActEvent/subject/registrationEvent/subject/testCodeIdentifier/playingTestCodeDetails/code/translation[@codeSystemName='DOSCATALOGNAMEHTMLENABLED']/originalText/text()";
String result = xpath.evaluate(expr, doc);
System.out.println("Result --> "+result);
return result;
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (XPathExpressionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return fileName;
public static void main(String[] args)
throws ParserConfigurationException, SAXException,
IOException, XPathExpressionException {
ParseBUXpath p = new ParseBUXpath();
p.parseRelatedTestXML("test");
}
}
I am facing this issue in Namespace thing in XML. When the xml is having "xmlns="urn:hl7-org:v3" then the xpath query doesnt get me the data. For supressing that i have written the code in java and removed the line from XML.
I need to parse the XML and get the data without deleting the namespace part from the XML. Is this a problem related to xsd or it is not getting the xsd mentioned?