Java 利用dom方式读取、创建xml详解及实例代码
Java利用dom方式读取、创建xml详解
1.创建一个接口
XmlInterface.Java
publicinterfaceXmlInterface { /** * 建立XML文档 * @paramfileName文件全路径名称 */ publicvoidcreateXml(String fileName); /** * 解析XML文档 * @paramfileName文件全路径名称 */ publicvoidparserXml(String fileName); }
接口实现
XmlImpl.java
packagecom.test.xml; importjava.io.FileNotFoundException; importjava.io.FileOutputStream; importjava.io.IOException; importjava.io.PrintWriter; importjavax.xml.parsers.DocumentBuilder; importjavax.xml.parsers.DocumentBuilderFactory; importjavax.xml.parsers.ParserConfigurationException; importjavax.xml.transform.OutputKeys; importjavax.xml.transform.Transformer; importjavax.xml.transform.TransformerConfigurationException; importjavax.xml.transform.TransformerException; importjavax.xml.transform.TransformerFactory; importjavax.xml.transform.dom.DOMSource; importjavax.xml.transform.stream.StreamResult; importorg.w3c.dom.Document; importorg.w3c.dom.Element; importorg.w3c.dom.Node; importorg.w3c.dom.NodeList; importorg.xml.sax.SAXException; publicclassXmlImplimplementsXmlInterface{ privateDocument document; publicvoidinit() { try{ DocumentBuilderFactory factory=DocumentBuilderFactory .newInstance(); DocumentBuilder builder=factory.newDocumentBuilder(); this.document =builder.newDocument(); }catch(ParserConfigurationException e){ System.out.println(e.getMessage()); } } publicvoidcreateXml(String fileName){ Element root=this.document.createElement("scores"); this.document.appendChild(root); Element employee=this.document.createElement("employee"); Element name=this.document.createElement("name"); name.appendChild(this.document.createTextNode("wangchenyang")); employee.appendChild(name); Element sex=this.document.createElement("sex"); sex.appendChild(this.document.createTextNode("m")); employee.appendChild(sex); Element age=this.document.createElement("age"); age.appendChild(this.document.createTextNode("26")); employee.appendChild(age); root.appendChild(employee); TransformerFactory tf=TransformerFactory.newInstance(); try{ Transformer transformer=tf.newTransformer(); DOMSource source=newDOMSource(document); transformer.setOutputProperty(OutputKeys.ENCODING,"gb2312"); transformer.setOutputProperty(OutputKeys.INDENT,"yes"); PrintWriter pw=newPrintWriter(newFileOutputStream(fileName)); StreamResult result=newStreamResult(pw); transformer.transform(source, result); System.out.println("生成XML文件成功!"); }catch(TransformerConfigurationException e){ System.out.println(e.getMessage()); }catch(IllegalArgumentException e){ System.out.println(e.getMessage()); }catch(FileNotFoundException e){ System.out.println(e.getMessage()); }catch(TransformerException e){ System.out.println(e.getMessage()); } } publicvoidparserXml(String fileName){ try{ DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance(); DocumentBuilder db=dbf.newDocumentBuilder(); Document document=db.parse(fileName); NodeList employees=document.getChildNodes(); for(inti =0; i测试
publicclassMain { publicstaticvoidmain(String args[]){ XmlImpl dd=newXmlImpl(); String str="D:/grade.xml"; dd.init(); dd.createXml(str);//创建xml dd.parserXml(str);//读取xml } }结果
生成xml
wangchenyang m 26 读取xml
生成XML文件成功! #text: name:wangchenyang #text: sex:m #text: age:26 #text: 解析完毕感谢阅读,希望能帮到大家,谢谢大家对本站的支持!