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

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]

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 锂电池供电怎么设计?
  • ¥15 Mac虚拟机安装Windows,Windows中用idea跑项目,报错process finished with exit code -1073741819(0xc0000005) 如何解决?
  • ¥15 关于python爬虫返回js数据结构的处理
  • ¥15 ubuntu所用stm32标准库开发烧录没反映
  • ¥15 运维工程师(在职人员回答)
  • ¥15 Kmeans的趋势分析有会的吗
  • ¥20 R语言单细胞测序 对seurat对象进行标准化时出现问题
  • ¥15 请问如何在matlab里使用raven工具?
  • ¥100 关于ios手游充值到一定金额,再点充值提示公众号的问题。
  • ¥15 求一个stm32串口控制程序