Java读取、创建xml(通过dom方式)

http://www.cnblogs.com/wangchenyang/archive/2011/08/23/2150530.html

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;

publicclassXmlImpl implementsXmlInterface{

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 < employees.getLength(); i++) {

Node employee = employees.item(i);

NodeList employeeInfo = employee.getChildNodes();

for(intj = 0; j < employeeInfo.getLength(); j++) {

Node node = employeeInfo.item(j);

NodeList employeeMeta = node.getChildNodes();

for(intk = 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());

}

}

}