java xml写入文件_Java眼中的XML——文件写入

DOM方法

public void createXML() throws Exception{

//新创建一个xml文件

DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();

DocumentBuilder db=dbf.newDocumentBuilder();

Document document = db.newDocument();

//创建根节点

Element root = document.createElement("bookstore");

//创建子节点

Element child = document.createElement("book");

//设置子节点的属性

child.setAttribute("id", "1");

//创建孙节点

Element grandChild1 = document.createElement("name");

//设置孙节点的名字

grandChild1.setTextContent("道德经");

//添加孙节点到子节点

child.appendChild(grandChild1);

//添加子节点到根节点

root.appendChild(child);

//添加根节点到document

document.appendChild(root);

TransformerFactory tff=TransformerFactory.newInstance();

Transformer tf=tff.newTransformer();

tf.setOutputProperty(OutputKeys.INDENT, "yes");//INDENT缩进,表示是否换行

tf.transform(new DOMSource(document), new StreamResult(new File("books1.xml")));

}

public static void main(String[] args) throws Exception {

new DOMTest().createXML();

}

SAX方法

//SAX

public void createXML() throws Exception{

SAXTransformerFactory tff=(SAXTransformerFactory) SAXTransformerFactory.newInstance();

//创建TransformerHandler对象

TransformerHandler handler = tff.newTransformerHandler();

//通过获取Transformer对象,设置输出文件的格式

Transformer tf=handler.getTransformer();

tf.setOutputProperty(OutputKeys.ENCODING, "utf-8");//编码

tf.setOutputProperty(OutputKeys.INDENT, "yes");//换行

//创建一个Result对象,使其与handler关联

Result result = new StreamResult(new FileOutputStream(new File("books2.xml")));

handler.setResult(result);

/***开始创建***/

//打开文档

handler.startDocument();

//创建AttributesImpl对象(implement--->工具)

AttributesImpl atts=new AttributesImpl();

//创建节点

handler.startElement("", "", "bookstore", atts);

atts.clear();

atts.addAttribute("", "", "id", "", "1");//设置属性

handler.startElement("", "", "book", atts);

atts.clear();

handler.startElement("", "", "name", atts);

handler.characters("小王子".toCharArray(), 0, "小王子".length());//写入文本

handler.endElement("", "", "name");

handler.endElement("", "", "book");

handler.endElement("", "", "bookstore");

//关闭文档

handler.endDocument();

}

public static void main(String[] args) throws Exception {

new SAXTest().createXML();

}

JDOM方法

public void createXML() throws Exception{

//创建根节点

Element rootElement = new Element("rss");

rootElement.setAttribute("version", "2.0");

//创建document对象

Document document = new Document(rootElement);

Element bookstore = new Element("bookstore");

rootElement.addContent(bookstore);

Element book = new Element("book");

book.setText("");

bookstore.addContent(book);

//设置生成xml的格式

Format format=Format.getCompactFormat();

format.setIndent("");//设置换行

format.setEncoding("gbk");

//生成xml文件

XMLOutputter outputer =new XMLOutputter(format);

outputer.output(document, new FileOutputStream(new File("books3.xml")));

}

public static void main(String[] args) throws Exception {

JDOMTest s = new JDOMTest();

s.createXML();

}

DOM4J方法

public void createXML() throws Exception{

//获取document对象

Document document = DocumentHelper.createDocument();

//添加rss节点

Element rss = document.addElement("rss");

//为rss节点添加属性

rss.addAttribute("version", "2.0");

Element bookstore = rss.addElement("bookstore");

Element book = bookstore.addElement("book");

Element name = book.addElement("name");

//为节点添加文本

name.setText("");

//设置生成xml文件的格式

OutputFormat format = OutputFormat.createPrettyPrint();

format.setEncoding("gbk");

//生成xml文件

XMLWriter writer=new XMLWriter(new FileOutputStream(new File("books4.xml")), format);

//设置是否转义,默认为true

writer.setEscapeText(false);

writer.write(document);

writer.close();

}

public static void main(String[] args) throws Exception {

new DOM4JTest().createXML();

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值