要用dom4j解析的带命名空间的xml文档如下:
---myXML.xml
[quote]
<?xml version="1.0" encoding="UTF-8"?>
<MyXML xmlns="http://www.ttt.com/ttt-TrdInfo-1-0" xmlns:x="http://www.ttt.com/ttt/metadata.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="res286.xsd">
<Hdr ReqId="001" Tid="1002" Cid="500" user="cuishen" Mname="supermarket" pwd="543200210"/>
<Car Flg="T" Cod="ccc" Door="kkk" mktId="b01">
<Key KeyID="t01"/>
</Car>
</MyXML>
[/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文件的问题的途径!
---myXML.xml
[quote]
<?xml version="1.0" encoding="UTF-8"?>
<MyXML xmlns="http://www.ttt.com/ttt-TrdInfo-1-0" xmlns:x="http://www.ttt.com/ttt/metadata.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="res286.xsd">
<Hdr ReqId="001" Tid="1002" Cid="500" user="cuishen" Mname="supermarket" pwd="543200210"/>
<Car Flg="T" Cod="ccc" Door="kkk" mktId="b01">
<Key KeyID="t01"/>
</Car>
</MyXML>
[/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文件的问题的途径!