Reputation: 33
this is my xml to parse..
<object pk="1" model="test">
<field type="BigIntegerField" name="number">100809206</field>
<field type="CharField" name="id">11700632</field>
<field name="test123">
<object pk="1" model="employee">
<field type="DateTimeField" name="updated_on">27 Nov, 2014, 17:18 hrs</field>
<field type="CharField" name="EmployeeName">ABC</field>
</object>
<object pk="2" model="employee">
<field type="DateTimeField" name="updated_on">28 Nov, 2014, 17:18 hrs</field>
<field type="CharField" name="EmployeeName">XYZ</field>
</object>
<object pk="3" model="employee">
<field type="DateTimeField" name="updated_on">28 Nov, 2014, 17:18 hrs</field>
<field type="CharField" name="EmployeeName">XYZ</field>
</object>
</field>
</object>
My code
BufferedReader in = new BufferedReader(new InputStreamReader(httpsURLConnection.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null)
{
response.append(inputLine);
}
in.close();
if(response != null && !"".contains(response))
{
SAXBuilder saxBuilder = new SAXBuilder();
Document doc = saxBuilder.build(new StringReader(response.toString()));
Element rootEle = doc.getRootElement();
List childList = rootEle.getChildren();
System.out.println(childList.toString());
for(i=0;i<childList.size();i++)
{
Element shipment= (Element) childList.get(i);
if(shipment != null)
{
List dataList = shipment.getChildren();
if(dataList != null && dataList.size() > 0)
{
List dataList = shipment.getChildren();
if(dataList != null && dataList.size() > 0)
{
XMLOutputter xmlOutput = new XMLOutputter();
String shipmentXml = xmlOutput.outputString(shipment);
InputSource source = new InputSource(new StringReader(shipmentXml));
org.w3c.dom.Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(source);
XPath xpath = XPathFactory.newInstance().newXPath();
String number = (xpath.evaluate("/object/field[@name ='number']", document).trim());
String name = (xpath.evaluate("/object/field[@name ='id']", document).trim());
}
}
}
}
}
I'm able to get name and id
<field type="BigIntegerField" name="number">100809206</field>
<field type="CharField" name="id">11700632</field>
but not able to get below fields usimg xpath..
<field name="test123">
<object pk="1" model="employee">
<field type="DateTimeField" name="updated_on">27 Nov, 2014, 17:18 hrs</field>
<field type="CharField" name="EmployeeName">ABC</field>
</object>
<object pk="2" model="employee">
<field type="DateTimeField" name="updated_on">28 Nov, 2014, 17:18 hrs</field>
<field type="CharField" name="EmployeeName">XYZ</field>
</object>
</field>
how to get value "27 Nov, 2014, 17:18 hrs" and ABC using attribute "updated_on and EmployeeName respectively
this 2 tags are varying can change from 2 to 5 or more..I want to iterate it in for loop and put it in some bean..so want value based on names of tags in for loop
so..Basically i Want to figure out how many <object pk="1" model="employee">
tags are available in <field name="test123">
and want to iterate over each <object>
tag in for loop using fields updated_on and EmployeeName
and get their values and put in java bean.
Upvotes: 1
Views: 241
Reputation: 22617
how to get value "27 Nov, 2014, 17:18 hrs" and ABC using attribute "updated_on and EmployeeName respectively
The correct path expression to get the field[@type = 'DateTimeField']
is
/object/field/object/field[@name = 'updated_on']
and to get field[@type = 'CharField']
/object/field/object/field[@name = 'EmployeeName']
You do not say why you're unable to retrieve those nodes, but I suspect the following: Most likely, you have tried to find them with an expression like
/object/field[@name ='updated_on']
which would only work if the XML was structured as follows:
<object>
<field name="updated_on"/>
</object>
In fact, the structure is
<object>
<field>
<object>
<field name="updated_on"/>
</object>
</field>
</object>
Finding elements anywhere in a tree can only be done with //
:
//object/field[@name ='updated_on']
Upvotes: 1