1 代码如下:
1 package dom.pasing; 2 3 import java.io.IOException; 4 import java.io.StringWriter; 5 6 import javax.xml.parsers.DocumentBuilder; 7 import javax.xml.parsers.DocumentBuilderFactory; 8 import javax.xml.parsers.ParserConfigurationException; 9 10 11 import javax.xml.transform.OutputKeys; 12 import javax.xml.transform.Transformer; 13 import javax.xml.transform.TransformerException; 14 import javax.xml.transform.TransformerFactory; 15 import javax.xml.transform.dom.DOMSource; 16 import javax.xml.transform.stream.StreamResult; 17 18 import org.apache.xml.serialize.OutputFormat; 19 import org.apache.xml.serialize.XMLSerializer; 20 import org.w3c.dom.Attr; 21 import org.w3c.dom.Document; 22 import org.w3c.dom.Element; 23 import org.w3c.dom.Node; 24 import org.w3c.dom.NodeList; 25 import org.w3c.dom.Text; 26 27 public class DOMTest { 28 29 /** 30 * 31 * @return 32 */ 33 public static Document createDocument() { 34 35 Document document = null; 36 37 try { 38 DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); 39 DocumentBuilder builder = builderFactory.newDocumentBuilder(); 40 document = builder.newDocument(); 41 Element root = document.createElement("students"); 42 document.appendChild(root); 43 44 /** 45 * <students> 46 * <student sn="01"> 47 * <name>张三</name> 48 * <age>19</age> 49 * </student> 50 * </students> 51 */ 52 Element stuEle = document.createElement("student"); 53 54 Attr attr = document.createAttribute("sn"); 55 attr.setValue("01"); 56 stuEle.setAttributeNode(attr); 57 58 Element nameElem = document.createElement("name"); 59 Text txtName = document.createTextNode("张三"); 60 nameElem.appendChild(txtName); 61 stuEle.appendChild(nameElem); 62 63 Element ageElem = document.createElement("age"); 64 Text txtAge = document.createTextNode("19"); 65 ageElem.appendChild(txtAge); 66 stuEle.appendChild(ageElem); 67 document.getDocumentElement().appendChild(stuEle); //在文档根元素节点studnets下,添加一个student元素节点 68 69 /** 70 * 创建节点 71 * <student> 72 * <name>李四</name> 73 * <age>24</age> 74 * </student> 75 */ 76 Element stuEle2 = document.createElement("student"); 77 78 Attr attr2 = document.createAttribute("sn"); 79 attr2.setValue("02"); 80 stuEle2.setAttributeNode(attr2); 81 82 Element nameEle2 = document.createElement("name"); 83 Text txtName2 = document.createTextNode("李四"); 84 nameEle2.appendChild(txtName2); 85 stuEle2.appendChild(nameEle2); 86 87 Element ageEle2 = document.createElement("age"); 88 Text txtAge2 = document.createTextNode("24"); 89 ageEle2.appendChild(txtAge2); 90 stuEle2.appendChild(ageEle2); 91 document.getDocumentElement().appendChild(stuEle2); //在文档根元素节点studnets下,添加一个student元素节点 92 } catch (ParserConfigurationException e) { 93 e.printStackTrace(); 94 } 95 96 return document; 97 } 98 99 public static void alterDocument(Document document) { 100 NodeList nodeList = document.getElementsByTagName("student"); 101 Node node = nodeList.item(0); 102 node.getParentNode().removeChild(node); //移除第1个student元素节点 103 } 104 105 106 107 /** 108 * 转换DOM为String 109 * @param document 110 * @return 111 * @throws TransformerException 112 */ 113 public static String documentToStr(Document document) { 114 115 String result = null; 116 117 try { 118 DOMSource source = new DOMSource(document); 119 StringWriter stringWriter = new StringWriter(); 120 StreamResult streamResult = new StreamResult(stringWriter); 121 122 TransformerFactory transformerFactory = TransformerFactory.newInstance(); 123 Transformer transformer = transformerFactory.newTransformer(); 124 transformer.setOutputProperty(OutputKeys.INDENT, "yes"); //默认的缩进量长度为0 125 transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8"); 126 transformer.transform(source, streamResult); 127 128 result = stringWriter.toString(); 129 } catch (TransformerException e) { 130 e.printStackTrace(); 131 } 132 return result; 133 } 134 135 /** 136 * 137 * @param document 138 * @return 139 */ 140 public static String documentToIndentStr(Document document) { 141 142 String result = null; 143 144 try { 145 146 StringWriter stringWriter = new StringWriter(); 147 148 OutputFormat format = new OutputFormat(document); 149 format.setLineWidth(100); 150 format.setIndenting(true); 151 format.setIndent(4); 152 153 XMLSerializer serializer = new XMLSerializer(stringWriter, format); 154 serializer.serialize(document); 155 156 result = stringWriter.toString(); 157 } catch (IOException e) { 158 // TODO Auto-generated catch block 159 e.printStackTrace(); 160 } 161 return result; 162 } 163 164 public static void main(String[] args) { 165 166 String rslt = null; 167 168 Document document = createDocument(); 169 170 rslt = documentToIndentStr(document); 171 172 System.out.println(rslt); 173 } 174 } 输出结果:
<?xml version="1.0" encoding="UTF-8"?>
<students>
<student sn="01">
<name>张三</name>
<age>19</age>
</student>
<student sn="02">
<name>李四</name>
<age>24</age>
</student>
</students>
时间: 2024-10-21 02:29:33