首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > XML SOAP >

dom4j中使用xpath解析带命名空间的xml文件,取不到节点的解决方法 (转载)

2012-07-25 
dom4j中使用xpath解析带命名空间的xml文件,取不到节点的解决办法 (转载)?xml version1.0 encodinggb

dom4j中使用xpath解析带命名空间的xml文件,取不到节点的解决办法 (转载)

<?xml version="1.0" encoding="gbk" standalone="yes"?><unit xmlns="http://www.sdml.info/srcML/src" xmlns:cpp="http://www.sdml.info/srcML/cpp">    <cpp:ifndef>        <name>test</name>    </cpp:ifndef>    <class>class</class></unit>

?转载之 http://blog.csdn.net/fm2005/article/details/4074313

?

?

?

    public static void main(String [] args){                  Document document = XmlTools.getDoc(file.getPath());                  HashMap xmlMap = new HashMap();                  xmlMap.put("src", "http://www.sdml.info/srcML/src");                  xmlMap.put("cpp", "http://www.sdml.info/srcML/cpp");                  // 以下可以访问到                  XPath x0 = document.createXPath("unit/src:class");                  XPath x1 = document.createXPath("src:unit/src:class");                  XPath x2 = document.createXPath("/unit/src:class");                  XPath x3 = document.createXPath("unit/src:class");                  XPath x4 = document.createXPath("/unit/cpp:define");                  XPath x6 = document.createXPath("unit/cpp:define");                  XPath x7 = document.createXPath("src:unit/cpp:define");                  // 以下访问不到                  XPath x8 = document.createXPath("unit/class");                  XPath x9 = document.createXPath("unit/define");                  XPath x10 = document.createXPath("/unit/class");                  XPath x11 = document.createXPath("/unit/define");                  XPath x12 = document.createXPath("src:unit/class");                  XPath x13 = document.createXPath("src:unit/define");                  XPath x14 = document.createXPath("unit/cpp:class");                  XPath x15 = document.createXPath("unit/src:define");                  test(x0, document, xmlMap);                  test(x1, document, xmlMap);                  test(x2, document, xmlMap);                  test(x3, document, xmlMap);                  test(x4, document, xmlMap);                  test(x6, document, xmlMap);                  test(x7, document, xmlMap);                  test(x8, document, xmlMap);                  test(x9, document, xmlMap);                  test(x10, document, xmlMap);                  test(x11, document, xmlMap);                  test(x12, document, xmlMap);                  test(x13, document, xmlMap);                  test(x14, document, xmlMap);                  test(x15, document, xmlMap);      }          static void test(XPath x, Document doc, Map map) {              x.setNamespaceURIs(map);              System.out.println("运行结果:"+x.selectNodes(doc).size());          }  

?

?

    运行结果:1      运行结果:1      运行结果:1      运行结果:1      运行结果:1      运行结果:1      运行结果:1      运行结果:0      运行结果:0      运行结果:0      运行结果:0      运行结果:0      运行结果:0      运行结果:0      运行结果:0  

热点排行