package dom;
import java.io.File;
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 DomDemo implements XmlDocument {
private Document document;
private String fileName;
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 = document.createElement("employees");
document.appendChild(root);
Element employee = document.createElement("employee");
Element name = document.createElement("name");
name.appendChild(document.createTextNode("张三"));
employee.appendChild(name);
Element sex = document.createElement("sex");
sex.appendChild(document.createTextNode("medol"));
Element age = document.createElement("age");
age.appendChild(document.createTextNode("18"));
employee.appendChild(sex);
employee.appendChild(age);
root.appendChild(employee);
TransformerFactory trf = TransformerFactory.newInstance();
try{
Transformer tf = trf.newTransformer();
DOMSource source = new DOMSource(document);
tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
tf.setOutputProperty(OutputKeys.INDENT,"yes");
PrintWriter pw = new PrintWriter(new FileOutputStream(new File(fileName)));
StreamResult result = new StreamResult(pw);
tf.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 parseXML (String fileName){
try{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder bf = factory.newDocumentBuilder();
Document document = bf.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());
System.out.println(e);
e.printStackTrace();
}catch(ParserConfigurationException e){
System.out.println(e.getMessage());
System.out.println(e);
e.printStackTrace();
}catch(IOException e){
System.out.println(e.getMessage());
System.out.println(e);
e.printStackTrace();
}catch(SAXException e){
System.out.println(e.getMessage());
e.printStackTrace();
}
}
public static void main(String[]args){
DomDemo dom = new DomDemo();
dom.init();
dom.createXML("./atest.xml");
dom.parseXML("./atest.xml");
}
}
谈不上原创,只是将ddviplinux 的例子给自己琢磨了一下,不过改变了一点,在程序运行时出现了MalformedByteSequenceException : Invalid byte 2 of 2-byte UTF-8 sequence
的异常,找了n多地方,终于发现了程序本身没有错误,只要把编译器的项目编码属性设置为UTF-8就可以了!!!