Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用Jdom和SAX解析器解析XML文件_Java_Xml_Sax_Jdom - Fatal编程技术网

Java 使用Jdom和SAX解析器解析XML文件

Java 使用Jdom和SAX解析器解析XML文件,java,xml,sax,jdom,Java,Xml,Sax,Jdom,我有一个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=

我有一个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>

...
36.48617
-89.97065
00000001第1-249页.pdf
...
33.59927
-86.69304
100-449923第26-114.pdf节
我需要在变量中找到内容、纬度和经度的值。到目前为止,我的代码是:

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);
}   
SAXBuilder sax=new SAXBuilder();
documentdoc=(Document)sax.build(新的StringReader(xmlstr));
Element rootElem=doc.getRootElement();
out.println(“rootElem=“+rootElem.toString());
元素res=rootElem.getChild(“结果”);
列表文档=res.getChildren(“文档”);
对于(int i=0;i此if(column.getAttribute(“name”).getValue()=“Content”)将只允许内容。
所以纬度和经度不会进入if条件,在这里打印值


尝试此如果(column.getAttribute(“name”).getValue()!=“id”),它将打印内容、纬度和经度。

如果要按名称检索属性:

for(int j=0;j<strs.size();j++){
    out.println("strs = " + strs.size());
    Element column =  (Element) strs.get(j);
    out.println("Content: " + column.getAttributeValue("Content"));
    out.println("Latitude: " + column.getAttributeValue("Latitude"));
    out.println("Longitude: " + column.getAttributeValue("Longitude"));
    out.println("id: " + column.getAttributeValue("id"));
}

for(int j=0;j用Java编写这样的低级导航代码是非常冗长且容易出错的。为什么不将XPath与Java代码结合使用,或者更好的是,用XQuery或XSLT编写整个代码?

您有一些问题

我认为如果您修复字符串比较,您的代码将达到预期效果

if(column.getAttribute("name").getValue()=='Content')
对字符串值使用==运算符很少是正确的

尝试:


哦,我知道你的单引号的事情意味着你没有给我们你真正的Java代码。……围绕“内容”的单引号甚至不会编译……

它没有帮助。我需要该标记的内容值,即33.59927。谢谢你的回复。当你执行时,你得到了什么输出?
if(column.getAttribute("name").getValue()=='Content')
if("Content".equals(column.getAttribute("name").getValue()))