-- 作者:piebila
-- 发布时间:7/7/2006 3:49:00 PM
-- [求助]关于利用dom解析xml的简单问题,希望高手们看看!!
本菜鸟编写了一个非常简单的解析xml的程序,但出现了一些问题,希望高手们多多帮忙指点! xml文档如下: <?xml version="1.0" ?> <customers> <customer > <id ref="pjl"/> <name dd="dd"/> <address ee="ds"/> </customer> <customer> <id ref="jjj">#002</id> <name dd="dd">Car</name> <address ee="dd">Suzhou</address> </customer> <customer> <id ref="ww">#003</id> <name dd="ee">Jimmy</name> <address ee="rr">ChengDu</address> </customer> <customer> <id ref="ww">#004</id> <name dd="r">Henry</name> <address ee="hh">Xi'an</address> </customer> </customers> 程序如下,错误出处已标在程序中: import org.w3c.dom.Document; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.NodeList; import org.w3c.dom.Node; import org.w3c.dom.Element; public class Saveadds extends Object{ public static void main(String args[]) { String addsFile="adds.xml"; Document addsDoc = null; try { DocumentBuilderFactory docbuilderfactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docbuilder = docbuilderfactory.newDocumentBuilder(); addsDoc = docbuilder.parse(addsFile); Element addsRoot=addsDoc.getDocumentElement(); NodeList Address=addsRoot.getElementsByTagName("customer"); for (int i=0;i<Address.getLength();i++){ Element cntAddress = (Element)Address.item(i); String id= cntAddress.getElementsByTagName("id").item(0).getFirstChild().getNodeValue(); System.out.println(id); String refattri=cntAddress.getAttribute("ref"); System.out.println(refattri); /**就是上面两行,其实就是抽取并输出元素中的属性。下面还有几行相似的代码。如果不写它们,程序执行没问题,但只要有它 们,就出现“Cannot read the addsfile:null”的错误**/ String name=cntAddress.getElementsByTagName("name").item(0).getFirstChild().getNodeValue(); System.out.println(name); String ddattri=cntAddress.getAttribute("dd"); System.out.println(ddattri); String address = cntAddress.getElementsByTagName("address").item(0).getFirstChild().getNodeValue(); System.out.println(address); String eeattri=cntAddress.getAttribute("ee"); System.out.println(eeattri); } } catch (Exception e) { System.out.println("Cannot read the addsfile:" + e.getMessage()); } } } 接着我又重新编写了另外的一个程序,如下: import org.w3c.dom.Document; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.NodeList; import org.w3c.dom.Node; import org.w3c.dom.Element; import org.w3c.dom.NamedNodeMap; public class Saveadds extends Object{ public static void main(String args[]) { String addsFile="adds.xml"; Document addsDoc = null; try { DocumentBuilderFactory docbuilderfactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docbuilder = docbuilderfactory.newDocumentBuilder(); addsDoc = docbuilder.parse(addsFile); addsDoc.normalize(); NodeList Address=addsDoc.getElementsByTagName("customer"); for (int i=0;i<Address.getLength();i++) { Node cntAddress = Address.item(i); NamedNodeMap nnm = cntAddress.getAttributes(); if(nnm!=null) { Node[] matchNodes = new Node[nnm.getLength()]; for (int j=0; j<nnm.getLength();j++) { matchNodes[j] = nnm.item(j); } for (int ii=0;ii<nnm.getLength();ii++) { Node node = matchNodes[i]; System.out.print(node.getNodeName() + "=" +node.getNodeValue() ); } } } } catch (Exception e) { System.out.println("Cannot read the addsfile:" + e.getMessage()); } } } 编译没问题,但是为什么执行该程序时没有任何结果,只会再次显示java文件编辑通过的结果,难道它又一次编辑了该java文件?。 这些问题看起来复杂,其实对于高手来说应该比较简单吧,急盼各位相助,小弟感激不尽!
|