怎么利用一个dom对象生成一个xml文件
发布网友
发布时间:2022-04-22 23:58
我来回答
共2个回答
热心网友
时间:2022-04-14 00:03
一个XML文档,可以先构造一个DOM,然后将DOM转化为xml序列,输出或者生成文件。package test;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
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;
public class Test {
public static void generate(){
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.newDocument();
document.setXmlVersion("1.0");
document.setXmlStandalone(true);
Element root = document.createElement_x("MobileNet"); //创建根节点
document.appendChild(root); //将根节点添加到Document对象中
Element pageElement = document.createElement_x("page"); //设置第一个page元素到
pageElement.setAttribute("name", "list.jsp"); //设置page节点的name属性
Element methodElement = document.createElement_x("method"); //设置method节点
methodElement.setTextContent("get"); //给method设置值
pageElement.appendChild(methodElement); //添加method节点到page节点内
Element displayElement = document.createElement_x("display"); //设置method节点
displayElement.setTextContent("list撒旦发放"); //给display设置值
pageElement.appendChild(displayElement); //添加display节点到page节点内
Element request_paramElement = document.createElement_x("request_param");
request_paramElement.setTextContent("request_param1|request_param2");
pageElement.appendChild(request_paramElement);
root.appendChild(pageElement);
pageElement = document.createElement_x("page"); //设置第二个page元素到
pageElement.setAttribute("name", "content.jsp"); //设置page节点的name属性
methodElement = document.createElement_x("method");
methodElement.setTextContent("post");
pageElement.appendChild(methodElement);
displayElement = document.createElement_x("display");
displayElement.setTextContent("content");
pageElement.appendChild(displayElement);
Element url_titleElement = document.createElement_x("url_title"); //设置url_title节点
url_titleElement.setTextContent("title,publisher,published_calendar"); //给url_title设置值
pageElement.appendChild(url_titleElement); //添加url_title节点到page节点内
root.appendChild(pageElement); //将page段加人根节点内
TransformerFactory transFactory = TransformerFactory.newInstance(); //开始把Document映射到文件
Transformer transFormer = transFactory.newTransformer();
DOMSource domSource = new DOMSource(document); //设置输出结果
File file = new File("MobileNetRule.xml"); //生成xml文件
if (!file.exists()) {
file.createNewFile();
}
FileOutputStream out = new FileOutputStream(file); //文件输出流
StreamResult xmlResult = new StreamResult(out); //设置输入源
transFormer.transform(domSource, xmlResult); //输出xml文件
System.out.println(file.getAbsolutePath()); //测试文件输出的路径
TransformerFactory tf = TransformerFactory.newInstance();
Transformer t = tf.newTransformer();
t.setOutputProperty("{/encoding/}","GB2312/");
ByteArrayOutputStream boc = new ByteArrayOutputStream();
t.transform(new DOMSource(document), new StreamResult(boc));
String xmlstring = boc.toString();
System.out.println(xmlstring);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args){
Test.generate();
}
}
热心网友
时间:2022-04-14 01:21
如下所示:public void transformDomToXml(Document document,String FileName)
{
try{
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer();
DOMSource source = new DOMSource(document);
StreamResult result = new StreamResult(new File(FileName));
transformer.transform(source,result);
}catch (TransformerConfigurationException tce) {
System.out.println(" " + tce.getMessage() );
}catch (TransformerException te) {
System.out.println(" " + te.getMessage() );
}
}