我有一个 XML 格式:
<response>
<result name="response" numFound="295" start="0">
<doc>
<str name="Content">...</str>
<str name="Latitude">36.48617</str>
<str name="Longitude">-89.97065</str>
<str name="id">00000001 pages 1-249.pdf</str>
</doc>
<doc>
<str name="Content">...</str>
<str name="Latitude">33.59927</str>
<str name="Longitude">-86.69304</str>
<str name="id">100-449923 section 26 -114.pdf</str>
</doc>
<doc>
我需要在变量中找到内容、纬度和经度的值。到目前为止,我的代码是:
SAXBuilder sax=new SAXBuilder();
Document doc= (Document) sax.build(new StringReader(xmlstr));
Element rootElem=doc.getRootElement();
out.println("rootElem = " + rootElem.toString());
Element res=rootElem.getChild("result");
List docs=res.getChildren("doc");
for(int i=0;i<docs.size();i++)
{
out.println("docsSize = " + docs.size());
Element row= (Element)docs.get(i);
//List strs= docs(i).getChildren("str");
List strs=row.getChildren("str");
for(int j=0;j<strs.size();j++){
out.println("strs = " + strs.size());
//out.println(strs.get(j).getValue());
List column = (List)strs.get(j);
if(column.getAttribute("name").getValue()=='Content')
{
out.println("Hi");
out.println(column.getAttribute("name").getValue());
out.println("Bi");
}
//String Content = column.getAttribute("name").get(1).getValue();
//String value = column.getText();
//out.println("Content = " + Content);
//out.println("value = " + value);
}
//out.println(content);
break;
}
}catch(Exception e)
{
out.println(e);
}
但是我仍然无法获得纬度和经度的值,即使我得到了它们所在数组的大小。你能建议同样的吗