package demo;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
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;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;

public class xmlTest {
	public static void main(String[] args) {
		//调用 DocumentBuilderFactory.newInstance() 方法得到创建 DOM 解析器的工厂 
		DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
        Element theBook=null, theElem=null, root=null;
        try {
            factory.setIgnoringElementContentWhitespace(true);//是否删除空格  false true
            DocumentBuilder db=factory.newDocumentBuilder();  //获取解析器
            //指定路径 获取xml文件的document对象
            File f = new File("src/book.xml"); 
            Document xmldoc=db.parse(f);
            root=xmldoc.getDocumentElement();
            //添加 元素(节点)
            theBook=xmldoc.createElement("book1");
            theElem=xmldoc.createElement("name");
            theElem.setTextContent("平凡的世界");
            theBook.appendChild(theElem);
            
            theElem=xmldoc.createElement("price");
            theElem.setTextContent("¥55.0");
            theBook.appendChild(theElem);

            theElem=xmldoc.createElement("conment");
            theElem.setTextContent("推荐大家看看这本书");
            theBook.appendChild(theElem);
            root.appendChild(theBook);
            
            printXML(xmldoc);				//打印至Console
            saveXml("new_book.xml", xmldoc);//保存生成 xml文件
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
	}
	
	/**
	 * 将node的XML字符串打印到控制台
	 * @param node
	 */
	public static void printXML(Node node) {
        TransformerFactory transFactory=TransformerFactory.newInstance();
        try {
            Transformer transformer = transFactory.newTransformer();
            transformer.setOutputProperty("encoding", "gb2312");//指定编码
            transformer.setOutputProperty("indent", "yes");     //是否设置缩进

            DOMSource source=new DOMSource();
            source.setNode(node);
            StreamResult result=new StreamResult();
            result.setOutputStream(System.out);
            
            transformer.transform(source, result);
        } catch (TransformerConfigurationException e) {
            e.printStackTrace();
        } catch (TransformerException e) {
            e.printStackTrace();
        }   
    }
 
	/**
	 * 保存生成xml文件
	 * @param fileName
	 * @param doc
	 */
    public static void saveXml(String fileName, Document doc) {//将Document输出到文件
        TransformerFactory transFactory=TransformerFactory.newInstance();
        try {
            Transformer transformer = transFactory.newTransformer();
            transformer.setOutputProperty("indent", "yes");//是否设置缩进

            DOMSource source=new DOMSource();
            source.setNode(doc);
            StreamResult result=new StreamResult();
            result.setOutputStream(new FileOutputStream(fileName));
            
            transformer.transform(source, result);
        } catch (TransformerConfigurationException e) {
            e.printStackTrace();
        } catch (TransformerException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }   
    }
}
最近下载更多
he88318799  LV16 2020年3月18日
最代码酒酒  LV20 2019年4月17日
07311514  LV9 2019年2月22日
ycb159856  LV12 2017年4月11日
scpcyzxb  LV16 2017年3月5日
放开那女孩  LV15 2016年11月17日
zyl  LV34 2016年9月7日
i小妖  LV11 2016年4月15日
最代码官方  LV167 2014年8月14日
最近浏览更多
wbbhappy  LV13 1月11日
1443251642  LV1 2022年12月19日
zql666  LV9 2021年9月28日
壹级天灾  LV14 2021年7月7日
Hachi6  LV13 2021年5月10日
15939671505 2020年12月18日
暂无贡献等级
djzhang  LV1 2020年11月5日
旧梦十年丶  LV2 2020年10月21日
1234567891011  LV5 2020年6月26日
guwuqifei  LV5 2020年6月19日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友