浏览 5075 次
该帖已经被评为新手帖
|
|
---|---|
作者 | 正文 |
发表时间:2006-11-21
引用 <?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</ReqId> <Tid>1002</Tid> <Cid>500</Cid> <user>cuishen</user> <Mname>supermarket</Mname> <pwd>543200210</pwd> </Hdr> <Car> <Flg>T</Flg> <Cod>ccc</Cod> <Door>kkk</Door> <mktId>b01</mktId> <Key> <KeyID>t01</KeyID> </Key> </Car> </MyXML> 要读取的xml文件2----myXML2.xml,放在c盘根目录下,原文件如下: 引用 <?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> 下面是用来读取上面两个xml文件的java文件-----ReadMyXML.java 引用 import java.io.File; import java.util.List; import org.dom4j.Document; import org.dom4j.Element; 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:/myXML2.xml"); SAXReader xmlReader = new SAXReader(); try{ Document document = xmlReader.read(xmlFile); ///*测试代码1 List nodeList = document.selectNodes("//MyXML/Hdr/ReqId"); Element valueElement = (Element)nodeList.get(0); System.out.println(valueElement.getText()); //*/ /*测试代码2 Element valueElement = (Element)document.selectSingleNode("//MyXML/Hdr/ReqId"); System.out.println(valueElement.getText()); */ /*测试代码3 Attribute valueAttribute = (Attribute)document.selectSingleNode("//MyXML/Hdr/@ReqId"); System.out.println(valueAttribute.getText()); */ }catch(DocumentException e){ e.printStackTrace(); } } } 我在JCreator中导入包jaxen-1.1-beta-6.jar或者jaxen-1.1-beta-9.jar,还有dom4j-1.6.1.jar,然后在ReadMyXML.java文件中换用三段测试代码中的任意一段,都无法读出myXML.xml或myXML2.xml,这个问题已经困扰我几个礼拜了,一直无法解决,恳请哪位高人不吝指教,不胜感激! 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2006-11-22
问题可能出在你的xml上,
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" 你定义的xsd文件有没有问题,如果去除这些,下面的代码能够正常执行: File xmlFile = new File("c:/myXML2.xml"); SAXReader xmlReader = new SAXReader(); Document document = xmlReader.read(xmlFile); // /*测试代码1 List nodeList = document.selectNodes("//MyXML/Hdr/@ReqId"); Attribute valueElement = (Attribute) nodeList.get(0); System.out.println(valueElement.getText()); |
|
返回顶楼 | |
发表时间:2007-05-08
|
|
返回顶楼 | |