简体   繁体   中英

Parsing XML File using Jdom and SAX Parser

I have a XML in the format:

<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. 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. so Latitude and Longitude won't come inside your if condition, where you print the values.

try this if(column.getAttribute("name").getValue()!='id') , it'll print Content, Latitude and Longitude.

Writing low-level navigational code like this in Java is awfully long-winded and error-prone. Why not use XPath in conjunction with your Java code, or better still, write the whole thing in XQuery or 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.

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....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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