Get Parent attribute value based XML search using xpath java - java

I want get RECORD number based on contract id passed to java method. can any one help on this as i am new to XML parsing?
sample xml file:
<?xml version="1.0"?><FILE>
<Document RECORD="1"><Contract-Id>234</Contract-Id><Client-Id>232</Client-Id></Document>
<Document RECORD="2"><Contract-Id>235</Contract-Id><Client-Id>334</Client-Id></Document>
</FILE?
Java code:
File fXmlFile = new File(inputFile);
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document xmlDocument = dBuilder.parse(fXmlFile);
xmlDocument.getDocumentElement().normalize();
XPath xPath = XPathFactory.newInstance().newXPath();
XPathExpression xPathExpr = xPath.compile("//Document/Contract-Id[text()='"+ContractNumber+"']");
//Object result = xPathExpr.evaluate(xmlDocument,XPathConstants.NODESET);
Node nl = (Node)xPathExpr.evaluate(xmlDocument.getParentNode(), XPathConstants.NODESET);
nl.getTextContent();
nl.getAttributes();

Please try the following XPath expression:
/FILE/Document[Contract-Id="235"]/#RECORD

Related

xml parse - xpath clarification in Java

How should i get the Link value from the below xml
XML Content
<document-instance system="abc.org" number-of-pages="6" desc="Drawing" link="www.google.com">
<document-format-options>
<document-format>application/pdf</document-format>
<document-format>application/tiff</document-format>
</document-format-options>
<document-section name="DRAWINGS" start-page="1" />
</document-instance>
i traverse update desc attribute after that i'm struggle
XPathExpression firstPageUrl = xPath.compile("//document-instance/#desc=\"Drawing\"]");
Expected output : retrieve the Link value
www.google.com
File file = new File("path to file");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(file);
XPath xPath = XPathFactory.newInstance().newXPath();
String expression = "//document-instance/#link";
Node node = (Node) xPath.compile(expression).evaluate(doc, XPathConstants.NODE);
String url= node.getTextContent();

Java and XML: Read XML tags with or without namespace

We are trying to build a service that receive some XML files. But people send it, sometimes use namespaces, sometimes not. For example:
<?xml version="1.0" encoding="UTF-8"?>
<ds:EnvioDoc version="1.0" xmlns:ds="http://my.domain">
<ds:Cabecera version="1.0">
<ds:IdRec>215217190015</ds:IdRec>
<ds:IdEm>211003420017</ds:IdEm>
<ds:IdDoc>2995019</ds:IdDoc>
</ds:Cabecera>
<Cuerpo>
<CorpDoc version="1.0" xmlns="http://my.domain" xmlns:xd="http://www.w3.org/2000/09/xmldsig#">
<body>
<Fecha>2016-08-12T00:11:50-03:00</Fecha>
[..]
or can come like:
<?xml version="1.0" encoding="UTF-8"?>
<EnvioDoc version="1.0" xmlns="http://my.domain">
<Cabecera version="1.0">
<IdRec>215217190015</IdRec>
<IdEm>211003420017</IdEm>
<IdDoc>2995019</IdDoc>
</Cabecera>
<Cuerpo>
<CorpDoc version="1.0" xmlns="http://my.domain" xmlns:xd="http://www.w3.org/2000/09/xmldsig#">
<body>
<Fecha>2016-08-12T00:11:50-03:00</Fecha>
[..]
We have tried to use this code to read this files:
File edocFile = new File(fileName);
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(edocFile);
//recomendado http://stackoverflow.com/questions/13786607/normalization-in-dom-parsing-with-java-how-does-it-work
doc.getDocumentElement().normalize();
NodeList nlCabecera = doc.getElementsByTagNameNS("*","Cabecera");
Node cabeceraNode = nlCabecera .item(0);
if (cabeceraNode.getNodeType() == Node.ELEMENT_NODE) {
But getting a NullPointerExsception in the if line.
Any suggest?
TIA
To use namespace aware DOM methods like getElementsByTagNameNS you need to make sure you use a namespace aware DOM by setting dbFactory.setNamespaceAware(true); on your factory used to create the DOM parser/DocumentBuilder.
One way you can get it to work with or without namespaces is as follows. Note that this solution uses XPATH , Although this does not solve the NPE you are getting the solution will work for the case with or without namespaces.
public static void main(String[] args) throws Exception {
File fXmlFile = new File("C:\\DevelopmentTools\\3.CODE\\XMLWithNS.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
doc.getDocumentElement().normalize();
XPath xpath = XPathFactory.newInstance().newXPath();
NodeList list = (NodeList) xpath.evaluate("//*[local-name()='Cabecera']", doc, XPathConstants.NODESET);
System.out.println("Size of the list is " + list.getLength());
}

Extract a node with its entire content from a namespaced xml

Given the following namespaced xml file:
<ptk:PrintTalk xmlns:ptk="http://linkToNameSpace"> xmlns:xjdf="http://linkToNamespace"
<ptk:Request>
<ptk:PurchaseOrder Currency="EUR">
<xjdf:XJDF name="someName" version="2.0">
<xjdf:ProductList>
<xjdf:Product>
...
</xjdf:Product>
<xjdf:OtherProduct>
...
</xjdf:OtherProduct>
and many other products
</xjdf:ProductList>
<xjdf:ParameterSet>
<xjdf:Parameter>
...
</xjdf:Parameter> and so on until
</xjdf:XJDF>
</ptk:PurchaseOrder>
</ptk:Request>
</ptk:PrintTalk>
how would I extract following using XPath:
<xjdf:XJDF name="someName" version="2.0">
<xjdf:ProductList>
<xjdf:Product>
...
</xjdf:Product>
<xjdf:OtherProduct>
...
</xjdf:OtherProduct>
and many other products
</xjdf:ProductList>
<xjdf:ParameterSet>
<xjdf:Parameter>
...
</xjdf:Parameter> and so on until
</xjdf:XJDF>
I already tried something like:
/ptk:PrintTalk/ptk:Request/ptk:PurchaseOrder/*
or
//xjdf:XJDF
but these expressions give me not the result I am looking for. I use IntellijIdea's built in xpath expression evaluator, programming language is java. No libraries for xpath - just java.xml.*
UPDATE
using
//ptk:PurchaseOrder//*
I get every node as a single node without any child nodes inside, e. g. would
<xjdf:ProductList>
<xjdf:Product>
...
</xjdf:Product>
</xjdf:ProductList> (here the product tag is a child of product list tag)
result in
<xjdf:ProuctList>
<xjdf:Product>
The java code I use to do the operation:
#Override
public XJDF readFrom(
final Class<XJDF> type, final Type genericType, final Annotation[] annotations, final MediaType mediaType,
final MultivaluedMap<String, String> multivaluedMap, final InputStream inputStream
) throws IOException {
try {
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
Document documentPtk = documentBuilder.parse(new InputSource(inputStream));
XPathFactory xPathFactory = XPathFactory.newInstance();
XPath xPath = xPathFactory.newXPath();
XPathExpression xPathExpression = xPath.compile("//ptk:PurchaseOrder//*");
Document documentXjdf = (Document) xPathExpression.evaluate(documentPtk, XPathConstants.NODE);
} catch (Exception e) {
throw new WebApplicationException("PrintTalk document could not be deserialized.", e);
}
}
Three main points to make here:
DocumentBuilderFactory is not namespace-aware by default, you must explicitly switch on namespaces before you create the DocumentBuilder
XPath doesn't use the namespace prefix mappings from the XML document, it uses its own NamespaceContext instead
The Node returned by this query won't be a Document, it'll be an Element.
Annoyingly there's no default implementation of NamespaceContext in the Java core class library so you have to either use a third party one (I usually use the SimpleNamespaceContext from Spring) or write your own implementation of the interface.
Here's an example using SimpleNamespaceContext:
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
documentBuilderFactory.setNamespaceAware(true);
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
Document documentPtk = documentBuilder.parse(new InputSource(inputStream));
XPathFactory xPathFactory = XPathFactory.newInstance();
XPath xPath = xPathFactory.newXPath();
SimpleNamespaceContext nsCtx = new SimpleNamespaceContext();
nsCtx.bindNamespaceUri("p", "http://linkToNameSpace");
xPath.setNamespaceContext(nsCtx);
XPathExpression xPathExpression = xPath.compile("/p:PrintTalk/p:Request/p:PurchaseOrder/*");
Element documentXjdf = (Element) xPathExpression.evaluate(documentPtk, XPathConstants.NODE);

Getting value of child node from XML in java

My xml file looks like this
<InNetworkCostSharing>
<FamilyAnnualDeductibleAmount>
<Amount>6000</Amount>
</FamilyAnnualDeductibleAmount>
<IndividualAnnualDeductibleAmount>
<NotApplicable>Not Applicable</NotApplicable>
</IndividualAnnualDeductibleAmount>
<PCPCopayAmount>
<CoveredAmount>0</CoveredAmount>
</PCPCopayAmount>
<CoinsuranceRate>
<CoveredPercent>0</CoveredPercent>
</CoinsuranceRate>
<FamilyAnnualOOPLimitAmount>
<Amount>6000</Amount>
</FamilyAnnualOOPLimitAmount>
<IndividualAnnualOOPLimitAmount>
<NotApplicable>Not Applicable</NotApplicable>
</IndividualAnnualOOPLimitAmount>
</InNetworkCostSharing>
I am trying to get Amount value from <FamilyAnnualDeductibleAmount> and also from <FamilyAnnualOOPLimitAmount>. How do i get those values in java?
You may use two XPath queries /InNetworkCostSharing/FamilyAnnualDeductibleAmount and InNetworkCostSharing/FamilyAnnualOOPLimitAmount or just get the node InNetworkCostSharing and retrieve the values of its two direct children.
Solution using XPath:
// load the XML as String into a DOM Document object
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
ByteArrayInputStream bis = new ByteArrayInputStream("YOUR XML".getBytes());
Document doc = docBuilder.parse(bis);
// XPath to retrieve the content of the <FamilyAnnualDeductibleAmount> tag
XPath xpath = XPathFactory.newInstance().newXPath();
XPathExpression expr = xpath.compile("/InNetworkCostSharing/FamilyAnnualDeductibleAmount/text()");
String familyAnnualDeductibleAmount = (String)expr.evaluate(doc, XPathConstants.STRING);
StAX based solution:
XMLInputFactory f = XMLInputFactory.newInstance();
XMLStreamReader rdr = f.createXMLStreamReader(new FileReader("test.xml"));
while (rdr.hasNext()) {
if (rdr.next() == XMLStreamConstants.START_ELEMENT) {
if (rdr.getLocalName().equals("FamilyAnnualDeductibleAmount")) {
rdr.nextTag();
int familyAnnualDeductibleAmount = Integer.parseInt(rdr.getElementText());
System.out.println("familyAnnualDeductibleAmount = " + familyAnnualDeductibleAmount);
} else if (rdr.getLocalName().equals("FamilyAnnualOOPLimitAmount")) {
rdr.nextTag();
int familyAnnualOOPLimitAmount = Integer.parseInt(rdr.getElementText());
System.out.println("FamilyAnnualOOPLimitAmount = " + familyAnnualOOPLimitAmount);
}
}
}
rdr.close();
Note that StAX is especially good for cases like yours, it skips all unnecessary elements reading only the ones you need
Try something like this(use getElementsByTagName to get the parent nodes and then get the value be reaching out to child node):
File xmlFile = new File("NetworkCost.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(xmlFile );
doc.getDocumentElement().normalize();
NodeList nList = doc.getElementsByTagName("FamilyAnnualDeductibleAmount");
String familyDedAmount = nList.item(0).getChildNodes().item(0).getTextContent();
nList = doc.getElementsByTagName("FamilyAnnualOOPLimitAmount");
String familyAnnualAmount =
nList.item(0).getChildNodes().item(0).getTextContent();
I think I found the solution with this question from stackoverflow
Getting XML Node text value with Java DOM

How to get namespace of xml

I have this xml:
InputStream is = new ByteArrayInputStream(
"<data:RobCtiAifoData xmlns:data=\"urn:cz:isvs:rob:schemas:RobDotazyData:v1\" xmlns:reg=\"urn:cz:isvs:reg:schemas:RegTypy:v1\" xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:urn=\"urn:cz:isvs:rob:schemas:RobUnivDotazy:v1\"><data:Aifo a=\"b\">1</data:Aifo><data:VyuzitiPoskytnuti>vyuziti</data:VyuzitiPoskytnuti> </data:RobCtiAifoData>"
.getBytes());
// InputStream is = new ByteArrayInputStream(xml.getBytes());
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.parse(is);
Node node = document.getDocumentElement();
when wanna get name of element without namespace so I wanna call substring of name of element withou prefix
node.getNodeName() gives me data:VyuzitiPoskytnuti
and node.getNamespaceURI() or node.getPrefix() gives me just null. So how I can get prefix of node ?
Try enabling namespace support:
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
DocumentBuilder db = dbf.newDocumentBuilder();
From the JavaDoc for setNamespaceAware:
Specifies that the parser produced by this code will provide support for XML namespaces. By default the value of this is set to false

Categories

Resources