// 把DOM转为String
private String otputDOMToXMLString(Document doc) throws IOException {
String XMLString = "";
//建立dom的输出格式
OutputFormat format = new OutputFormat(doc);
StringWriter stringOut = new StringWriter();
XMLSerializer serial = new XMLSerializer(stringOut, format);
serial.asDOMSerializer();
serial.serialize(doc.getDocumentElement());
XMLString = stringOut.toString();
System.out.println(XMLString);
return XMLString;
}
//把String 保存为文件
private void saveXMLString(String XMLString, String fileName)
throws IOException {
File file = new File(fileName);
if (file.exists()) {
file.delete();
}
file.createNewFile();
if (file.canWrite()) {
FileWriter fileOut = new FileWriter(file);
fileOut.write(XMLString);
fileOut.close();
}
}
解决方案 »
- 如何将String对象转换成int并取得Unicode码
- 如何设计这种表结构?
- !!!!技术求教:java 对象序列化后,可传送对象至服务端,返回数据到客户端时抛出异常。
- 估计我这个问题连hibernate他爹也回答不了了!!!喔喔喔喔喔喔
- Rose逆向生成不了与org包有关联的java类怎么办?
- 用httpclient传参,并取得网页内容的方法?
- Android开发虚拟机问题
- 本人建立的一个Webservice和LDAP开发应用技术群,欢迎大家加入
- 机会难得!!组建学习日记开源项目开发小组(jsp+javabean+struts框架)的公开信(11月11日报名截止)
- 奇怪的问题,请教各位大虾
- 一个XML的问题,在线等待,谢谢解答!
- 求救:操作数据库时遇到的问题 [在线]
www.jdom.org
import org.jdom.Element;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
public class ProduceXMl {
/**
* |-『统战部』
40 |-『干部科』
41 |-『秘书科』
42 |-『区民族宗教局』
43 |-『民族宗教科』
44 |-『佛教协会』
45 |-『联络科』
46 |-『台湾事务办公室』
47 |-『办公室』
48 |-『台胞接待站』 */
public static void main(String[] args) throws IOException {
Document doc = new Document();
Element root = new Element("根");
doc.setRootElement(root);
Element tongzhanbu = new Element("统战部");
root.addContent(tongzhanbu);
tongzhanbu.addContent(new Element("干部科").setText("干部"));
tongzhanbu.addContent(new Element("秘书科").setText("秘书"));
Element zhongjiao = new Element("区民族宗教局");
tongzhanbu.addContent(zhongjiao);
zhongjiao.addContent(new Element("民族宗教科").setText("民族宗教"));
zhongjiao.addContent(new Element("佛教协会").setText("佛教协会"));
tongzhanbu.addContent(new Element("联络科").setText("联络"));
Element taiban = new Element("台湾事务办公室");
root.addContent(taiban);
taiban.addContent(new Element("办公室").setText("办公室"));
taiban.addContent(new Element("台胞接待站").setText("台胞接待站"));
XMLOutputter outp = new XMLOutputter();
Format format=Format.getPrettyFormat();
format.setEncoding("GBK");
outp.setFormat(format);
outp.output(doc,System.out);
}
}
<?xml version="1.0" encoding="GBK"?>
<根>
<统战部>
<干部科>干部</干部科>
<秘书科>秘书</秘书科>
<区民族宗教局>
<民族宗教科>民族宗教</民族宗教科>
<佛教协会>佛教协会</佛教协会>
</区民族宗教局>
<联络科>联络</联络科>
</统战部>
<台湾事务办公室>
<办公室>办公室</办公室>
<台胞接待站>台胞接待站</台胞接待站>
</台湾事务办公室>
</根>