问题描述
需要解析的xml文件内容如下:<xxx><SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:m0="http://group.unicom.com/xsd"><SOAP-ENV:Body><m:syncAreaInfo xmlns:m="http://service.unicom.com"><m:arg0><m0:operate_type>I</m0:operate_type><m0:area_code>001</m0:area_code><m0:area_name>String</m0:area_name><m0:area_frame>String</m0:area_frame><m0:order_no>0</m0:order_no><m0:parent_area_code>String</m0:parent_area_code><m0:start_date>String</m0:start_date><m0:end_date>String</m0:end_date><m0:area_level>0</m0:area_level><m0:validflag>String</m0:validflag><m0:remark>String</m0:remark><m0:para>String</m0:para><m0:para_id>String</m0:para_id><m0:para_value>String</m0:para_value></m:arg0></m:syncAreaInfo></SOAP-ENV:Body></SOAP-ENV:Envelope><SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:m0="http://group.unicom.com/xsd"><SOAP-ENV:Body><m:syncAreaInfo xmlns:m="http://service.unicom.com"><m:arg0><m0:operate_type>D</m0:operate_type><m0:area_code>001</m0:area_code><m0:area_name>String</m0:area_name><m0:area_frame>String</m0:area_frame><m0:order_no>0</m0:order_no><m0:parent_area_code>String</m0:parent_area_code><m0:start_date>String</m0:start_date><m0:end_date>String</m0:end_date><m0:area_level>0</m0:area_level><m0:validflag>String</m0:validflag><m0:remark>String</m0:remark><m0:para>String</m0:para><m0:para_id>String</m0:para_id><m0:para_value>String</m0:para_value></m:arg0></m:syncAreaInfo></SOAP-ENV:Body></SOAP-ENV:Envelope></xxx>我要取到所有的<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 blocke.printStackTrace();}}但是貌似标签命中包含“:”就会出错org.dom4j.XPathException: Exception occurred evaluting XPath: //m:arg0. Exception: XPath expression uses unbound namespace prefix mat 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" 如果把“:”换成“-”就没有问题,但这个文件时别人传过来的,格式就是这样又不能改,请问怎样才能取到这些节点呢
解决方案
XML中,ooo:xxx中,ooo是命名空间。XML里有xmlns:m="http://service.unicom.com"这样的声明,但是Java程序里没有。所以,处理带命名空间的XML的时候,应该告诉Java程序,每个命名空间前缀(比如“m”)具体代表什么命名空间,(比如“http://service.unicom.com”)。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());}}}
解决方案二:
一楼正解!
解决方案三:
楼上正解。