Dom4J生成xml和包含CDATA问题

在 java注解生成xml和包含CDATA问题里面做了介绍,这里直接贴代码。
1:生成xml的java文件

package com.dufy.test.xml;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;

/**
 * 使用DOM4J生成xml并包括CDATA的处理
 * @author aflyun
 * @date 2016.06.16
 *
 */
public class TestGenerateXml { 

    /** 
    * @param args 
    */ 
    public static void main(String[] args) { 

        loadXml(); 

    } 
    /**
     * 生成 document xml
     */
    public static void loadXml(){ 
        Document document = DocumentHelper.createDocument();//由DocumentHelper得到一个空的Document 
        document.setXMLEncoding("utf-8");//设置xml头条的Encoding 
        //addElement写入根节点 addAttribute节点属性的键和值 
        Element root = document.addElement("item");//.addAttribute("xmlns", "").addAttribute("版本号", ""); 
        //由根节点 可以得到下二级的节点 
        Element id = root.addElement("id"); 
        id.addText("35399645973"); 
        //二级节点 
        Element title = root.addElement("title"); 

        title.addText("");

        Element category = root.addElement("category"); 
        category.addText("保湿>洗护]]>");

        Element url = root.addElement("url"); 
        url.addText("");

        Element url_wap = root.addElement("url_wap"); 
        url_wap.addText("");

        Element price = root.addElement("price"); 
        price.addText("310");

        Element promotion_price = root.addElement("promotion_price"); 
        promotion_price.addText("111");

        Element wap_price = root.addElement("wap_price"); 
        wap_price.addText("80");

        Element sub_item_ids = root.addElement("sub_item_ids"); 
            Element sub_item_id = sub_item_ids.addElement("sub_item_id");
            sub_item_id.addText("35399645973_1");
            sub_item_id.addText("35399645973_2");
            sub_item_id.addText("35399645973_3");

        Element detail = root.addElement(""); 
        detail.addText("");

        Element status = root.addElement("status"); 
        status.addText("1");

        Element pic_main = root.addElement("pic_main"); 
            Element img_main = pic_main.addElement("img");
            img_main.addElement("url").addText("");
            img_main.addElement("size").addText("400x400");


        Element pic_extra = root.addElement("pic_extra"); 
            Element img_extra = pic_extra.addElement("img");
            img_extra.addElement("url").addText("");
            img_extra.addElement("size").addText("400x400");
            Element img_extr1 = pic_extra.addElement("img");
            img_extr1.addElement("url").addText("");
            img_extr1.addElement("size").addText("400x400");

        String xml1 = document.asXML();                          //默认转义 
        String xml2 = formatXml(document, "utf-8", true);        //转义 
        String xml3 = formatXml(document, "utf-8", false);       //不转义 
         System.out.println("--------1111-----------------"); 
        System.out.println(xml1); 
        System.out.println("---------2222----------------"); 
        System.out.println(xml2); 
        System.out.println("---------3333----------------"); 
        System.out.println(xml3); 

        PrintWriter pw = null;
        try {
             pw = new PrintWriter(new File("D:/test.xml"));
             pw.write(xml3);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            pw.close();
        }

    } 

     /** 
     * 格式化XML文档 
     * 
     * @param document xml文档 
     * @param charset    字符串的编码 
     * @param istrans    是否对属性和元素值进行转移 
     * @return 格式化后XML字符串 
     */ 
    public static String formatXml(Document document, String charset, boolean istrans) { 
            OutputFormat format = OutputFormat.createPrettyPrint(); 
            format.setEncoding(charset); 
            StringWriter sw = new StringWriter(); 
            XMLWriter xw = new XMLWriter(sw, format); 
            xw.setEscapeText(istrans); 
            try { 
                    xw.write(document); 
                    xw.flush(); 
                    xw.close(); 
            } catch (IOException e) { 
                    System.out.println("格式化XML文档发生异常,请检查!"); 
                    e.printStackTrace(); 
            } 
            return sw.toString(); 
    } 


} 

setEscapeText 设置为false,最为重要
2:生成后的xml,上面的第三种才能生成正确的结果!
文件:在D:/test.xml



<item>
  <id>35399645973id>
  <title>title>
  <category>保湿>洗护]]>category>
  <url>url>
  <url_wap>url_wap>
  <price>310price>
  <promotion_price>111promotion_price>
  <wap_price>80wap_price>
  <sub_item_ids>
    <sub_item_id>35399645973_135399645973_235399645973_3sub_item_id>
  sub_item_ids>
  <<detail>>detail>>
  <status>1status>
  <pic_main>
    <img>
      <url>url>
      <size>400x400size>
    img>
  pic_main>
  <pic_extra>
    <img>
      <url>url>
      <size>400x400size>
    img>
    <img>
      <url>url>
      <size>400x400size>
    img>
  pic_extra>
item>

3:参考文章
(1):Dom4j格式化转义字符问题

你可能感兴趣的:(javaweb)