<<persons.xml>>
<?xml version="1.0" encoding="UTF-8"?>
<persons>
<person>
<name>andy</name>
<age>45</age>
<tel>13632940025</tel>
<sex>m</sex>
</person>
</persons>
<<WriteXmlTest.xml>>
package test.xml;
import java.io.File;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
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.NodeList;
import org.w3c.dom.Text;
import org.xml.sax.SAXException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
public class WriteXmlTest {
public static void main(String[] args) {
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document dc = db.parse("persons.xml");
fang(dc);
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void fang(Document doc) {
Element eleroots = doc.getDocumentElement();
Element eleroot = doc.createElement("person");// 创建gen元素
eleroots.appendChild(eleroot);
Element elename = doc.createElement("name");
Text text1 = doc.createTextNode("andy");
elename.appendChild(text1);
eleroot.appendChild(elename);
Element eleage = doc.createElement("age");
Text text2 = doc.createTextNode("45");
eleage.appendChild(text2);
eleroot.appendChild(eleage);
Element eletel = doc.createElement("tel");
Text text3 = doc.createTextNode("13632940025");
eletel.appendChild(text3);
eleroot.appendChild(eletel);
Element elesex = doc.createElement("sex");
Text text4 = doc.createTextNode("m");
elesex.appendChild(text4);
eleroot.appendChild(elesex);
TransformerFactory tff = TransformerFactory.newInstance();
Transformer tf = null;
try {
tf = tff.newTransformer();
} catch (TransformerConfigurationException e) {
e.printStackTrace();
}
DOMSource ds = new DOMSource(doc);
StreamResult sr = new StreamResult(new File("persons.xml"));
try {
tf.transform(ds, sr);
} catch (TransformerException e) {
e.printStackTrace();
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<persons>
<person>
<name>andy</name>
<age>45</age>
<tel>13632940025</tel>
<sex>m</sex>
</person>
</persons>
<<WriteXmlTest.xml>>
package test.xml;
import java.io.File;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
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.NodeList;
import org.w3c.dom.Text;
import org.xml.sax.SAXException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
public class WriteXmlTest {
public static void main(String[] args) {
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document dc = db.parse("persons.xml");
fang(dc);
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void fang(Document doc) {
Element eleroots = doc.getDocumentElement();
Element eleroot = doc.createElement("person");// 创建gen元素
eleroots.appendChild(eleroot);
Element elename = doc.createElement("name");
Text text1 = doc.createTextNode("andy");
elename.appendChild(text1);
eleroot.appendChild(elename);
Element eleage = doc.createElement("age");
Text text2 = doc.createTextNode("45");
eleage.appendChild(text2);
eleroot.appendChild(eleage);
Element eletel = doc.createElement("tel");
Text text3 = doc.createTextNode("13632940025");
eletel.appendChild(text3);
eleroot.appendChild(eletel);
Element elesex = doc.createElement("sex");
Text text4 = doc.createTextNode("m");
elesex.appendChild(text4);
eleroot.appendChild(elesex);
TransformerFactory tff = TransformerFactory.newInstance();
Transformer tf = null;
try {
tf = tff.newTransformer();
} catch (TransformerConfigurationException e) {
e.printStackTrace();
}
DOMSource ds = new DOMSource(doc);
StreamResult sr = new StreamResult(new File("persons.xml"));
try {
tf.transform(ds, sr);
} catch (TransformerException e) {
e.printStackTrace();
}
}
}