dom方式是Java官方生成xml文件的方式。其当时如下:
首先,创建一个DocumentBuilderFactory的对象
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
接下来,创建DocumentBuilder对象
DocumentBuilder db =null;
具体实现过程参照程序:(下边的程序是一个方法,所需要的包附在最后)
public void creatXml(){
DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
try {
DocumentBuilder db=dbf.newDocumentBuilder();
Document doc=db.newDocument();
doc.setXmlStandalone(ture);
Element bookstore=doc.createElement("bookstore");
doc.appendChild(bookstore);
Element book=doc.createElement("book");
bookstore.appendChild(book);
book.setAttribute("id", "1");
Element name=doc.createElement("name");
book.appendChild(name);
name.setTextContent("小王子");
TransformerFactory tff=TransformerFactory.newInstance();
try {
Transformer tf=tff.newTransformer();
try {
tf.transform(new DOMSource(doc), new StreamResult(new File("books1.xml")));
} catch (TransformerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (TransformerConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
try {
DocumentBuilder db=dbf.newDocumentBuilder();
Document doc=db.newDocument();
doc.setXmlStandalone(ture);
Element bookstore=doc.createElement("bookstore");
doc.appendChild(bookstore);
Element book=doc.createElement("book");
bookstore.appendChild(book);
book.setAttribute("id", "1");
Element name=doc.createElement("name");
book.appendChild(name);
name.setTextContent("小王子");
TransformerFactory tff=TransformerFactory.newInstance();
try {
Transformer tf=tff.newTransformer();
try {
tf.transform(new DOMSource(doc), new StreamResult(new File("books1.xml")));
} catch (TransformerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (TransformerConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
所使用的包:
import java.io.File;
import java.io.IOException;
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 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.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;