这是我要解析的xml ..
<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>
我的代码
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());
}
}
}
}
}
我能够获得姓名和身份证明
<field type="BigIntegerField" name="number">100809206</field>
<field type="CharField" name="id">11700632</field>
但无法获得以下字段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>
如何获得价值“2014年11月27日17:18”和ABC使用属性“updated_on和EmployeeName分别
这2个标签变化可以从2变为5或更多..我想在for循环中迭代它 并把它放在一些bean ..所以想要基于for循环
的标签名称的值所以..基本上我想知道<object pk="1" model="employee">
中有多少<field name="test123">
个标签可供使用,并希望使用字段<object>
遍历for循环中的每个updated_on and EmployeeName
标记并获取它们的值并放入java bean。
答案 0 :(得分:1)
如何获得价值“2014年11月27日,17:18”和ABC分别使用属性“updated_on和EmployeeName”
获取field[@type = 'DateTimeField']
的正确路径表达式是
/object/field/object/field[@name = 'updated_on']
并获得field[@type = 'CharField']
/object/field/object/field[@name = 'EmployeeName']
您没有说为什么您无法检索这些节点,但我怀疑以下内容:您很可能已尝试使用类似
的表达式找到它们/object/field[@name ='updated_on']
只有在XML结构如下时才有效:
<object>
<field name="updated_on"/>
</object>
事实上,结构是
<object>
<field>
<object>
<field name="updated_on"/>
</object>
</field>
</object>
只能使用//
:
//object/field[@name ='updated_on']