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()
}
}
如何使用dom拼接xml字符串(标准方式)如何使用dom拼接xml字符串(标准方式) .
package com.my.test
import org.dom4j.Document
import org.dom4j.DocumentHelper
import org.dom4j.Element
public class XmlGroup {
/**
* 返回applogin接口的请求xml yes
* @param guid
* @param guidpwd
* @return String xml信息
*/
public static String getRquestXml(String guid, String guidpwd)
{
Document requestDoc = DocumentHelper.createDocument()
Element root = requestDoc.addElement("message")
Element headElement = root.addElement("head")
headElement.addElement("guid").addText(guid)
Element paramsElement = root.addElement("body").addElement("params")
paramsElement.addElement("pwd").addText(guidpwd)
paramsElement.addElement("callbackurl")
return root.asXML()
}
/**
* 返回searchemployee接口的请求xml
* @param appid
* @param tag
* @param accounts
* @return String
*/
public String getRequestSearchEmployeeXml(String appid, String tag, String accounts)
{
Document requestDoc = DocumentHelper.createDocument()
Element root = requestDoc.addElement("message")
Element headElement = root.addElement("head")
headElement.addElement("appid").addText(appid)
headElement.addElement("tag").addText(tag)
headElement.addElement("accounts").setText(accounts)
Element paramsElement = root.addElement("body").addElement("params")
paramsElement.addElement("condition").setText(accounts)
paramsElement.addElement("pagecount").setText("20")
paramsElement.addElement("pagenum").setText("1")
return root.asXML()
}
public static void main(String agrs[])
{
//test
String xml=getRquestXml("zhangyi","123")
System.out.println(xml)
//test
String xml2=getRquestXml("mobile","ads324sdsa4sf3w")
System.out.println(xml2)
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)