Java 利用dom方式读取、创建xml详解及实例代码
更新时间:2017年03月15日 15:13:07 投稿:lqh
这篇文章主要介绍了Java 利用dom方式读取、创建xml的相关资料,需要的朋友可以参考下
Java 利用dom方式读取、创建xml详解
1.创建一个接口
XmlInterface.Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | public interface XmlInterface { /** * 建立XML文档 * @param fileName 文件全路径名称 */ public void createXml(String fileName); /** * 解析XML文档 * @param fileName 文件全路径名称 */ public void parserXml(String fileName); } |
接口实现
XmlImpl.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 | package com.test.xml; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.PrintWriter; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import javax.xml.transform.OutputKeys; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerConfigurationException; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.SAXException; public class XmlImpl implements XmlInterface{ private Document document; public void init() { try { DocumentBuilderFactory factory = DocumentBuilderFactory .newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); this .document = builder.newDocument(); } catch (ParserConfigurationException e) { System.out.println(e.getMessage()); } } public void createXml(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 = new DOMSource(document); transformer.setOutputProperty(OutputKeys.ENCODING, "gb2312" ); transformer.setOutputProperty(OutputKeys.INDENT, "yes" ); PrintWriter pw = new PrintWriter( new FileOutputStream(fileName)); StreamResult result = new StreamResult(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()); } } public void parserXml(String fileName) { try { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); Document document = db.parse(fileName); NodeList employees = document.getChildNodes(); for ( int i = 0 ; i < employees.getLength(); i++) { Node employee = employees.item(i); NodeList employeeInfo = employee.getChildNodes(); for ( int j = 0 ; j < employeeInfo.getLength(); j++) { Node node = employeeInfo.item(j); NodeList employeeMeta = node.getChildNodes(); for ( int k = 0 ; k < employeeMeta.getLength(); k++) { System.out.println(employeeMeta.item(k).getNodeName() + ":" + employeeMeta.item(k).getTextContent()); } } } System.out.println( "解析完毕" ); } catch (FileNotFoundException e) { System.out.println(e.getMessage()); } catch (ParserConfigurationException e) { System.out.println(e.getMessage()); } catch (SAXException e) { System.out.println(e.getMessage()); } catch (IOException e) { System.out.println(e.getMessage()); } } } |
测试
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | public class Main { public static void main(String args[]){ XmlImpl dd= new XmlImpl(); String str= "D:/grade.xml" ; dd.init(); dd.createXml(str); //创建xml dd.parserXml(str); //读取xml } } |
结果
生成xml
1 2 3 4 5 6 7 8 | <? xml version = "1.0" encoding = "GB2312" ?> < scores > < employee > < name >wangchenyang</ name > < sex >m</ sex > < age >26</ age > </ employee > </ scores > |
读取xml
感谢阅读,希望能帮到大家,谢谢大家对本站的支持!
微信公众号搜索 “ 脚本之家 ” ,选择关注
程序猿的那些事、送书等活动等着你
相关文章
基于Java实现ssh命令登录主机执行shell命令过程解析
这篇文章主要介绍了基于Java实现ssh命令登录主机执行shell命令过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下2019-12-12
最新评论