开发者

Error while reading text from Document in jdom?

开发者 https://www.devze.com 2023-04-10 13:57 出处:网络
I am here to ask you a basic question about jdom. I am trying to read a Document object but I got an error all the time.

I am here to ask you a basic question about jdom. I am trying to read a Document object but I got an error all the time. The Document that I am trying to read is,

<message>
<header>
<messageType>snmp</messageType>
<sendFrom>192.168.0.16</sendFrom>
<hostName>oghmasysMehmet</hostName>
<sendTo>192.168.0.12</sendTo>
<receiverName>Mehmet</receiverName>
<date>03/10/2011</date>
</header>
<body>
<snmpType>getbulk</snmpType>
<ip>127.0.0.1</ip>
<port>161</port>
<oids>
  <oid>1.3.6.1.2.1.1</oid>
</oids>
<community>public</community>
<nR>0</nR>
<mR>5</mR>
</body>
</message>

And I am trying to value. To do it, I have written a function,

public Vector<String> getOIDs(Document document){ 

    Vector<String> oids = new Vector<String>();

    Element root = document.getRootElement();
    Element body = root.getChild("body");
    //Element element = body.getChild("oids");
    List rows = body.getChildren("oid");
    for (int i = 0; i < rows.size(); i++) {
        Element row = (Element) rows.get(i);
        String s = row.getText();
        oids.add(s);
    }
    return oids;
}

but When I debug it, I can always see that there is nothing read by the function. Can you please help me about that ?

Thank you all

EDIT :Ok sorry for asking such a noob question, I just made a mistake in getchildren (); I should have written oids instead of oid

EDIT 2: Actually ı have changed the code as I commented on my开发者_运维知识库 question but now, the only thing I read is "\n \n" not "1.3.6.1.2.1.1". What do you think the problem might be ?


At first glance, seems to me that doesn't have a "oid" child, it has a "oids" child. The element you're trying to read is inside the "oids" element.

You can try and debug it step by step, and see what is the element that isn't being read. That would be my best guess without trying it out.


Your commented out line was correct, the line below it just needs to be updated to match. Your listing should be:

 Vector<String> oids = new Vector<String>();
 Element root = document.getRootElement();
 Element body = root.getChild("body");
 Element element = body.getChild("oids");
 List rows = element.getChildren("oid");
 for (int i = 0; i < rows.size(); i++) {
      Element row = (Element) rows.get(i);
      String s = row.getText();
      oids.add(s);
 }
 return oids;   
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号