Java眼中的XML——文件写入

2019-06-15 00:00:00 文件 写入 眼中

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();        
    }

相关文章