lundi 2 mars 2015

How to retrieve XML element attribute having namespaces using java?



I have the below xml and I am trying to retrieve the value of id under BoostBuryDimensionValue tag using the java code but returns nothing. Can some one help me on this. Thanks in advance.


Input XML



<?xml version="1.0" encoding="UTF-8"?>
<ContentItem type="OrganicZoneContent" xmlns="http://ift.tt/1LDy0Hm" >
<TemplateId>OrganicResults</TemplateId>
<Name>OrganicResults</Name>
<Property name="navigation_records">
<BoostBury rollupKey="grp_id" recspecField="grp_id" xmlns="http://ift.tt/1Bcusux">

<BoostBuryRecords>
<BoostBuryRecord recordType="CRITERIA" boostBuryType="BOOST">
<BoostBurySearch terms="null" key="null"/>
<BoostBuryDimensionValues>
<BoostBuryDimensionValue id="4294965238" name="career" dimensionName="Occasion"/>
</BoostBuryDimensionValues>
</BoostBuryRecord>

</BoostBuryRecords>
</BoostBury>
</Property>
</ContentItem>


and the java code i am using is



public static void main(String[] args) throws Exception {
InputStream xml = new FileInputStream("tempinput.xml");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(xml);
XPath xpath = XPathFactory.newInstance().newXPath();
XPathExpression expr = xpath.compile("ContentItem/Property[@name='navigation_records']/BoostBury/BoostBuryRecords/BoostBuryRecord/@id");
Object result = expr.evaluate(doc, XPathConstants.STRING);
System.out.println("BoostBuryDimensionValue id = " + result);

}



Aucun commentaire:

Enregistrer un commentaire