创建XML文档,添加DOM节点
package topicclassifier;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
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.Text;
public class XMLTool {
DocumentBuilderFactory factory;
DocumentBuilder builder;
Document doc;
public XMLTool(String filePath)throws Exception {
factory = DocumentBuilderFactory.newInstance();
builder = factory.newDocumentBuilder();
doc = builder.parse(filePath);
}
public static void callWriteXmlFile(Document doc, Writer w,
String encoding) {
try {
Source source = new DOMSource(doc);
Result result = new StreamResult(w);
Transformer xformer = TransformerFactory.newInstance().newTransformer();
xformer.setOutputProperty(OutputKeys.ENCODING, encoding);
xformer.transform(source, result);
}
catch (TransformerConfigurationException e) {
e.printStackTrace();
}
catch (TransformerException e) {
e.printStackTrace();
}
}
public static boolean createXML(String filePath)
{
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder builder=null;
try {
builder = dbf.newDocumentBuilder();
} catch(Exception e){
e.printStackTrace();
return false;
}
Document doc = builder.newDocument();
Element root = doc.createElement_x("info");
doc.appendChild(root); //将根元素添加到文档上
try {
FileOutputStream fos = new FileOutputStream(filePath);
OutputStreamWriter outwriter = new OutputStreamWriter(fos);
callWriteXmlFile(doc,outwriter,"gb2312");
outwriter.close();
fos.close();
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
public void addForm(FormWraper fw){
Element form = doc.createElement_x("form");
form.setAttribute("ID", "1");
form.setAttribute("name", "form1");
Element text = doc.createElement_x("text");
form.appendChild(text);
Element attr1 = doc.createElement_x("attr");
attr1.setAttribute("label", "书名");
attr1.setAttribute("name", "key");
text.appendChild(attr1);
Element root = doc.getDocumentElement();
root.normalize();
root.appendChild(form);
try {
FileOutputStream fos = new FileOutputStream("D:/forminfo.txt");
OutputStreamWriter outwriter = new OutputStreamWriter(fos);
//((XmlDocument)doc).write(outwriter); //出错!
callWriteXmlFile(doc,outwriter,"gb2312");
outwriter.close();
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
String filePath = "D:/forminfo.txt";
XMLTool.createXML(filePath);
try {
XMLTool xt = new XMLTool(filePath);
xt.addForm(new FormWraper());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
文章转载自http://blog.sina.com.cn/s/blog_49b05ad00100gcok.html