2011-10-03 25 views
1

我想从xml文档对象中获取一些数据。我的幻想XML文件就是这样;如何从org.jdom.Document中的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; 
} 

当我从网上看,我undeerstood,我应该使用List类来获得信,但是当我尝试它,我总是错误。你能帮我解决问题吗?

谢谢大家。

+1

什么错误?编译错误?运行时错误?发布它们。帮助我们帮助你。读这可能会有所帮助:http://www.javaworld.com/javaworld/jw-05-2000/jw-0518-jdom.html – duffymo

回答

1

我看不出代码中有什么问题。唯一看起来很腥的是显式转换为List。这是为什么?

我猜你已经导入了错误的List实现。确保你已经导入了java.util.List。

+0

非常感谢。现在,如果我使用 List rows = body.getChildren(“oid”); (int i = 0; i s? – Ozer

+0

对不起......我不明白这个问题。你是什​​么意思“ s”? –

+0

我在Document对象中的之间的值。 – Ozer

0

在您的XML中,< body>和< oids>是兄弟姐妹,即它们具有相同的父亲。您的代码假设< oids>是< body>的子项。这应该有希望让你再次去。

+0

对不起,我写错了,现在正在改变它。 – Ozer

+0

@Ozer。然后它看起来很好。如果您向我们展示您遇到的错误,我们将有更好的机会帮助您! –

+0

我认为我正在使用的列表不是java.util.List,所以这就是为什么我得到了错误。 – Ozer