简体   繁体   English

使用Jdom和SAX Parser解析XML文件

[英]Parsing XML File using Jdom and SAX Parser

I have a XML in the format: 我有一个格式的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>

I need to find the values of Content, Latitude and Longitude in variable. 我需要在变量中找到Content,Latitude和Longitude的值。 So far my code is: 到目前为止我的代码是:

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);
}   

But still i am not able to get values f Latitude and Longitude, even though i get size of the array they are in. Can you pls suggest the same 但是我仍然无法获得纬度和经度值,即使我得到它们所在阵列的大小。你能不能提出同样的建议

this if(column.getAttribute("name").getValue()=='Content') will only allow Content. 这个if(column.getAttribute(“name”)。getValue()=='Content')只允许Content。 so Latitude and Longitude won't come inside your if condition, where you print the values. 所以纬度和经度不会进入您打印值的if条件。

try this if(column.getAttribute("name").getValue()!='id') , it'll print Content, Latitude and Longitude. 尝试这个if(column.getAttribute(“name”)。getValue()!='id') ,它将打印内容,纬度和经度。

Writing low-level navigational code like this in Java is awfully long-winded and error-prone. 在Java中编写这样的低级导航代码非常冗长且容易出错。 Why not use XPath in conjunction with your Java code, or better still, write the whole thing in XQuery or XSLT? 为什么不将XPath与Java代码结合使用,或者更好的是,在XQuery或XSLT中编写整个内容?

If you want to retrieve the attributes by name: 如果要按名称检索属性:

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"));
}

If you want to iterate over the attributes: 如果要迭代属性:

for(int j=0;j<strs.size();j++){
    out.println("strs = " + strs.size());
    Element column =  (Element) strs.get(j);
    for (Attribute a : column.getAttributes())
      out.println("Name: " + a.getName() + " Value: " + a.getValue());
}

There are a few issues you have. 你有一些问题。

I think your code will do what you expect if you fix your String comparisons... 我认为如果修复字符串比较,你的代码会做你期望的...

if(column.getAttribute("name").getValue()=='Content')

will never work. 永远不会工作。 Using == operator on a String value is very seldom the 'right' thing. 在String值上使用==运算符很少是“正确”的东西。

Try: 尝试:

if("Content".equals(column.getAttribute("name").getValue()))

Oh, I see that your whole single-quote thing means that you have not given us your real Java code.... The single-quotes around 'Content' will not even compile.... 哦,我看到你的整个单引号意味着你没有给我们你真正的Java代码....围绕'内容'的单引号甚至不会编译....

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM