从 jdom 中的文档读取文本时出错?
我来这里是想问你一个关于jdom的基本问题。我正在尝试读取 Document 对象,但总是出现错误。 我正在尝试阅读的文档是,
<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>
并且我正在尝试评估。为此,我编写了一个函数,
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;
}
但是当我调试它时,我总是可以看到该函数没有读取任何内容。 你能帮我一下吗?
谢谢大家
编辑:好吧,很抱歉问了这样一个菜鸟问题,我只是在 getchildren (); 中犯了一个错误。我应该写 oids 而不是 oid
编辑2:实际上,我在评论我的问题时更改了代码,但现在,我读到的唯一内容是“\n \n”而不是“1.3.6.1.2.1.1”。您认为问题可能是什么?
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 ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
乍一看,在我看来,它没有“oid”孩子,它有一个“oids”孩子。您尝试读取的元素位于“oids”元素内。
您可以尝试逐步调试它,看看哪些元素没有被读取。这将是我在不尝试的情况下最好的猜测。
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: