Java 操纵XML之修改XML文件
一、JAVA DOM PARSER
DOM interfaces
The DOM defines several Java interfaces. Here are the most common interfaces:
Node - The base datatype of the DOM.
Element - The vast majority of the objects you'll deal with are Elements.
Attr Represents an attribute of an element.
Text The actual content of an Element or Attr.
Document Represents the entire XML document. A Document object is often referred to as a DOM tree.
Common DOM methods
When you are working with the DOM, there are several methods you'll use often:
Document.getDocumentElement() - Returns the root element of the document.
Node.getFirstChild() - Returns the first child of a given Node.
Node.getLastChild() - Returns the last child of a given Node.
Node.getNextSibling() - These methods return the next sibling of a given Node.
Node.getPreviousSibling() - These methods return the previous sibling of a given Node.
Node.getAttribute(attrName) - For a given Node, returns the attribute with the requested name.
二、源代码:ModifyXmlFile.java
1 packagecn.com.zfc.lesson26.xml;2
3 importjava.io.File;4
5 importjavax.xml.parsers.DocumentBuilder;6 importjavax.xml.parsers.DocumentBuilderFactory;7 importjavax.xml.transform.Transformer;8 importjavax.xml.transform.TransformerFactory;9 importjavax.xml.transform.dom.DOMSource;10 importjavax.xml.transform.stream.StreamResult;11
12 importorg.w3c.dom.Document;13 importorg.w3c.dom.Element;14 importorg.w3c.dom.NamedNodeMap;15 importorg.w3c.dom.Node;16
17 /**
18 * 使用JAVA DOM PARSER:修改 XML 文件19 *20 *@authorzfc21 * @date 2017年12月7日 下午8:31:5522 */
23 public classModifyXmlFile {24 public static voidmain(String[] args) {25 try{26 String filePath = "I:\\code_workspace\\JavaSE_workspace\\JavaSE\\src\\cn\\com\\zfc\\lesson26\\xml\\ModifyXmlFile.xml";27 //1、创建 File 对象,映射 XML 文件
28 File file = newFile(filePath);29 //2、创建 DocumentBuilderFactory 对象,用来创建 DocumentBuilder 对象
30 DocumentBuilderFactory documentBuilderFactory =DocumentBuilderFactory.newInstance();31 //3、创建 DocumentBuilder 对象,用来将 XML 文件 转化为 Document 对象
32 DocumentBuilder documentBuilder =documentBuilderFactory.newDocumentBuilder();33 //4、创建 Document 对象,解析 XML 文件
34 Document document =documentBuilder.parse(file);35 //修改第一个 student36 //5、获取第一个 student 结点
37 Node student = document.getElementsByTagName("student").item(0);38 //获取结点 student 结点的所有属性
39 NamedNodeMap namedNodeMap =student.getAttributes();40 //获取界定 student 的 属性 id
41 Node id = namedNodeMap.getNamedItem("id");42 //给属性 id 重新设置值
43 id.setTextContent("student11");44 //6、获取根结点 students 的第一个直接子结点 student
45 student = document.getElementsByTagName("student").item(0);46 Element studentElement =(Element) student;47 //7、获取结点 student 的直接子结点 name、sex
48 Node name = studentElement.getElementsByTagName("name").item(0);49 Node sex = studentElement.getElementsByTagName("sex").item(0);50 Element nameElement =(Element) name;51 Element sexElement =(Element) sex;52 //8、给节点进行设置值
53 nameElement.setTextContent("TomTom");54 sexElement.setTextContent("FemaleFemale");55 //9、创建 TransformerFactory 对象
56 TransformerFactory transformerFactory =TransformerFactory.newInstance();57 //10、创建 Transformer 对象
58 Transformer transformer =transformerFactory.newTransformer();59 //11、创建 DOMSource 对象
60 DOMSource domSource = newDOMSource(document);61 //12、创建 StreamResult 对象
62 StreamResult reStreamResult = newStreamResult(file);63 transformer.transform(domSource, reStreamResult);64
65 //输出测试结果
66 StreamResult consoleResult = newStreamResult(System.out);67 transformer.transform(domSource, consoleResult);68
69 } catch(Exception e) {70 e.printStackTrace();71 }72 }73 }
ModifyXmlFile.xml
1 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
2
3
4 Tom
5 Female
6
7
8 Lucy
9 Male
10
11
三、运行效果:
1 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
2
3 TomTom
4 FemaleFemale
5
6
7 Lucy
8 Male
9
10