import java.io.FileOutputStream
import org.dom4j.io.OutputFormat
import org.dom4j.io.XMLWriter
import org.dom4j.DocumentHelper
import org.dom4j.Element
import org.dom4j.Document
public class Test1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Document document = DocumentHelper.createDocument()
Element root = document.addElement("EnglishWords")
// root.addComment("This is a test for dom4j ")
Element singleWord = root.addElement("EnglishWord")
Element word = singleWord.addElement("word")
Element englishMean = singleWord.addElement("englishMean")
Element chineseMean = singleWord.addElement("chineseMean")
Element attribute = singleWord.addElement("attribute")
Element example = singleWord.addElement("example")
// 添加元素内容
word.addText("1")
attribute.addText("2")
chineseMean.addText("3")
englishMean.addText("4")
example.addText("5")
// 添加属性
// singleWord.addAttribute("code", "1")
OutputFormat format = OutputFormat.createPrettyPrint()
format.setEncoding("UTF-8")
format.setIndent(true)
try {
String filename = "D:\\a.xml"
FileOutputStream out = new FileOutputStream(filename)//
XMLWriter writer = new XMLWriter(out, format)
document.setXMLEncoding("UTF-8")
writer.write(document)
out.close()
} catch (Exception e) {
e.printStackTrace()
}
}
}
换行符"\r\n",如是用dom4j之类的jar包 *** 作,dom4j换行如下
/*** 通过 org.dom4j.io.OutputFormat 来设置XML文档输出格式
*/
OutputFormat format = OutputFormat.createPrettyPrint() //设置XML文档输出格式
format.setEncoding("GB2312") //设置XML文档的编码类型
format.setSuppressDeclaration(true)
format.setIndent(true) //设置是否缩进
format.setIndent(" ") //以空格方式实现缩进
format.setNewlines(true) //设置是否换行
需要jdom.jar和dom4j.jar这两个包/**
* 将crash信息打包成xml格式
* @param hashMap 原始crash信息的hashmap形式
* @throws IOException
* @throws JDOMException
*/
public void BuildXMLDoc(HashMap<String, String>hashMap) throws IOException, JDOMException {
int size = hashMap.size()
Iterator iter = hashMap.entrySet().iterator()
Map.Entry entry
String key
String val
int id = 1
// 创建根节点 list
Element root = new Element("phoneInfo")
// 根节点添加到文档中;
Document Doc = new Document(root)
// 此处 迭代器的遍历 可替换成循环遍历 数据库表的结果集 *** 作
while (iter.hasNext()) {
entry = (Map.Entry)iter.next()
key = (String)entry.getKey()
val = hashMap.get(key)
if (val == null || val.length() == 0)
{
val = "null"
}
// 创建节点 user
Element elements = new Element("category")
// 给 user 节点添加属性 id
elements.setAttribute("id", "" + id)
id ++
// 给 user 节点添加子节点并赋值;
// new Element("name")中的 "name" 替换成表中相应字段,setText("xuehui")中 "xuehui
// 替换成表中记录值;
elements.addContent(new Element(key).setText(val))
// 给父节点list添加user子节点
root.addContent(elements)
}
//带有缩进格式的xml 更美观
Format format = Format.getPrettyFormat()
XMLOutputter XMLOut = new XMLOutputter(format)
XMLOut.output(Doc, new FileOutputStream(FileUtil.CRASHPATH))
System.out.println("完成了文件写入")
}
上面虽然是对hashmap的遍历,但是对于其他容器的遍历形式不变,只是换成你需要的信息即可
下面是其他文章找到的
生成XML的时候,处理特殊字符
element.addContent(new CDATA("<xml>content"))
生成XML的时候,设置编码
XMLOutputter XMLOut = new XMLOutputter()
XMLOut.setEncoding("gb2312")
XMLOut.output(Doc, new FileOutputStream("test1.xml"))
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)