如何从org.jdom.Document中的xml对象获取数据?
我正在尝试从 xml 文档对象获取一些数据。我想象中的xml文件是这样的;
<root>
<body>
<oids>
<oid> </oid>
<oid> </oid>
<oid> </oid>
<oid> </oid>
</oids>
</body>
</root>
为此,我正在为此编写一个函数;
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 = (List) element.getChildren("oid");
/*
List rows = root.getChildren("oids");
for (int i = 0; i < rows.size(); i++) {
}
*/
return oids;
}
当我从互联网上阅读时,我明白我应该使用 List 类来获取 s 但当我尝试它时,我总是遇到错误。你能帮我拿到s吗?
谢谢大家。
I am trying to get some datas from xml document object. My imaginery xml file is like that;
<root>
<body>
<oids>
<oid> </oid>
<oid> </oid>
<oid> </oid>
<oid> </oid>
</oids>
</body>
</root>
And to do that I am writing a function for that ;
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 = (List) element.getChildren("oid");
/*
List rows = root.getChildren("oids");
for (int i = 0; i < rows.size(); i++) {
}
*/
return oids;
}
As I read from the Internet , I undeerstood that I should use List class to get the s but when I try it, I always get errors. Can you please help me to get the s.
Thank you all.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我看不出代码中有什么问题。唯一看起来可疑的是对 List 的显式转换。这是为什么?
我猜您导入了错误的 List 实现。确保您已导入 java.util.List。
I can't see what is wrong in the code. The only thing that looks fishy is the explicit conversion to List. Why is that?
I'm guessing that you have imported the wrong List implementation. Make sure you have imported java.util.List.
在您的 XML 中,;和是兄弟姐妹,即他们有同一个父母。您的代码假设是的孩子。希望这能让你再次前进。
In your XML, <body> and <oids> are siblings, i.e. they have the same parent. Your code assumes that <oids> is a child of <body>. That should hopefully get you going again.