dom4j中处理XML命名空间的问题,哪位指点下,谢谢!!!
要用dom4j解析的带命名空间的xml文档如下: ---myXML.xml [quote] [/quote] 解析上面xml文件的java文件如下: --ReadMyXML.java [quote] import java.io.File; import java.util.List; import java.util.Map; import java.util.HashMap; import org.dom4j.Document; import org.dom4j.Element; import org.dom4j.XPath; import org.dom4j.Attribute; import org.dom4j.io.SAXReader; import org.dom4j.DocumentException; public class ReadMyXML{ public static void main(String args[]){ File xmlFile = new File("c:/myXML.xml"); SAXReader xmlReader = new SAXReader(); try{ Document document = xmlReader.read(xmlFile); /*测试代码1 Attribute valueAttribute = (Attribute)document.selectSingleNode("//MyXML/Hdr/@ReqId"); System.out.println(valueAttribute.getText()); */ ///*测试代码2 xml的命名空间问题 HashMap xmlMap = new HashMap(); xmlMap.put("mo","http://www.ttt.com/ttt-TrdInfo-1-0"); xmlMap.put("x","http://www.ttt.com/ttt/metadata.xsd"); xmlMap.put("xsi","http://www.w3.org/2001/XMLSchema-instance"); XPath x = document.createXPath("//mo:MyXML/Hdr/@ReqId"); x.setNamespaceURIs(xmlMap); Attribute valueAttribute = (Attribute)x.selectSingleNode(document); System.out.println(valueAttribute.getText()); //*/ }catch(DocumentException e){ e.printStackTrace(); } } } [/quote] 经过本人测试发现,使用dom4j的测试代码1和2都无法解析xml文件,就是因为这个xml文件带有命名空间,希望大家讨论讨论,找到解决dom4j解析带命名空间的xml文件的问题的途径!