duruilin43946516
2011-08-11 17:46
浏览 510
已采纳

dom4j解析xml文件的问题

需要解析的xml文件内容如下:


SOAP-ENV:Body


m0:operate_typeI/m0:operate_type
m0:area_code001/m0:area_code
m0:area_nameString/m0:area_name
m0:area_frameString/m0:area_frame
m0:order_no0/m0:order_no
m0:parent_area_codeString/m0:parent_area_code
m0:start_dateString/m0:start_date
m0:end_dateString/m0:end_date
m0:area_level0/m0:area_level
m0:validflagString/m0:validflag
m0:remarkString/m0:remark
m0:paraString/m0:para
m0:para_idString/m0:para_id
m0:para_valueString/m0:para_value
/m:arg0
/m:syncAreaInfo
/SOAP-ENV:Body
/SOAP-ENV:Envelope


SOAP-ENV:Body


m0:operate_typeD/m0:operate_type
m0:area_code001/m0:area_code
m0:area_nameString/m0:area_name
m0:area_frameString/m0:area_frame
m0:order_no0/m0:order_no
m0:parent_area_codeString/m0:parent_area_code
m0:start_dateString/m0:start_date
m0:end_dateString/m0:end_date
m0:area_level0/m0:area_level
m0:validflagString/m0:validflag
m0:remarkString/m0:remark
m0:paraString/m0:para
m0:para_idString/m0:para_id
m0:para_valueString/m0:para_value
/m:arg0
/m:syncAreaInfo
/SOAP-ENV:Body
/SOAP-ENV:Envelope

我要取到所有的SOAP-ENV:Body元素
java 代码如下:
public static void main(String[] args) {
SAXReader saxReader = new SAXReader();
try {
Document document = saxReader.read("D:/syncAreaInfo.xml");
List list=document.selectNodes("//m:arg0");
Iterator iterator=list.iterator();
while(iterator.hasNext()){
Element node=(Element)iterator.next();
System.out.println("node is "+node.getName());
}
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
但是貌似标签命中包含“:”就会出错
org.dom4j.XPathException: Exception occurred evaluting XPath: //m:arg0. Exception: XPath expression uses unbound namespace prefix m
at org.dom4j.xpath.DefaultXPath.handleJaxenException(DefaultXPath.java:374)
at org.dom4j.xpath.DefaultXPath.selectNodes(DefaultXPath.java:134)
at org.dom4j.tree.AbstractNode.selectNodes(AbstractNode.java:166)
at com.unicom.service.XmlParser.main(XmlParser.java:20)
Exception in thread "main"

如果把“:”换成“-”就没有问题,但这个文件时别人传过来的,格式就是这样又不能改,请问怎样才能取到这些节点呢

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • wks1986 2011-08-11 18:05
    已采纳

    XML中,ooo:xxx中,ooo是命名空间。

    XML里有xmlns:m="http://service.unicom.com"这样的声明,但是Java程序里没有。所以,处理带命名空间的XML的时候,应该告诉Java程序,每个命名空间前缀(比如“m”)具体代表什么命名空间,(比如“http://service.unicom.com”)。

    [code="java"]package dom4jnstest;

    import java.util.Collections;
    import java.util.Iterator;
    import java.util.List;

    import org.dom4j.Document;
    import org.dom4j.Element;
    import org.dom4j.io.SAXReader;
    import org.dom4j.xpath.DefaultXPath;

    public class Dom4jNsTest {

    public static void main(String[] args) throws Exception {
        SAXReader saxReader = new SAXReader();
        Document document = saxReader.read(Dom4jNsTest.class
                .getResourceAsStream("x.xml"));
    
        DefaultXPath xpath = new DefaultXPath("//m:arg0");
        xpath.setNamespaceURIs(Collections.singletonMap("m",
                "http://service.unicom.com"));
    
        List list = xpath.selectNodes(document);
        Iterator iterator = list.iterator();
        while (iterator.hasNext()) {
            Element node = (Element) iterator.next();
            System.out.println("node is " + node.getName());
        }
    }
    

    }
    [/code]

    点赞 评论
  • myali88 2011-08-11 18:14

    楼上正解。

    点赞 评论
  • iteye_6273 2011-08-12 10:46

    一楼正解! :)

    点赞 评论

相关推荐 更多相似问题