问题是这样的,这是XML<?xml version="1.0"?> 
<note 
        xmlns="http://www.w3school.com.cn" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xsi:schemaLocation="http://www.w3school.com.cn file:///D:/_dev_stu/xsdtest/src/note.xml">     <to>George</to> 
    <from>John</from> 
    <heading name="xixi">
        <subheading>hehe</subheading>
    <body>Don't forget the meeting this weekend!</body> 
</note>我有一个List,里面是to,from,heading,subheading,body
,存的都是这个xml里面可能有的值的名称。
我想写个方法,遍历这个List,将里面的值与xml里面的值一一匹配,找到他们的值,比如先是to,那希望获得的是George;比如遍历到name,那希望获得xixi;比如subheading,获得hehe。
我现在的做法是遇到to,就遍历整个xml,然后匹配,遇到from,就遍历所有xml,然后匹配。
想知道,有没有Dom4j或者其他包,有这种方法,就是给一个名称,然后自己搜索整个xml,有则返回值,没有就返回null之类的,类似于
String search(nodeName/AttributeName),返回这个节点的value或这个属性的值呢?请大家帮忙~\(≧▽≦)/~啦啦啦

解决方案 »

  1.   

    使用 xpath
    //to/text()
    //from/text()
      

  2.   

    dom4j没有现成的方法,可以自己写个,这个不难啊,先遍历一遍,然后把这些都存导map里,就可以直接get("")了。还有,就是<to> George </to>  这个节点,在xml里并不一定是唯一的一个节点啊,比如下面的xml,你要取to,是给你哪个呢?George1还是George2?所以,要一层一层的来,不能直接to节点:
    <note1 ....>   
    ....
            <to> George1 </to>    
    ......
    </note1> 
    <note2 ....>   
    ....
            <to> George2 </to>    
    ......
    </note2> 
      

  3.   

    dom4j通过xpath查询xml http://apps.hi.baidu.com/share/detail/24459960
      

  4.   

    dom4j 也支持 xpath。
    Document 不是有 selectNodes/selectSingleNode 方法吗?