百度java生成xml,有一大推的文章,主要的生成方式一種使用Dom4J ,還有一種使用Jdk自帶註解類! 下麵主要整理我註解類的使用,(可以參考這篇文章 "Dom4J生成xml和包含CDATA問題" )和xml中CDATA 問題的解決方法! 1:要生成的xml原始文件! 2:xml對應的model ...
百度java生成xml,有一大推的文章,主要的生成方式一種使用Dom4J ,還有一種使用Jdk自帶註解類!
下麵主要整理我註解類的使用,(可以參考這篇文章Dom4J生成xml和包含CDATA問題)和xml中CDATA 問題的解決方法!
1:要生成的xml原始文件!
<?xml version="1.0" encoding="utf-8"?>
<item>
<id>35399645973</id>
<title><![CDATA[補水首選水密碼水保濕美白護洗護組合三件]]> </title>
<category><![CDATA[美妝>保濕>洗護]]></category>
<url><![CDATA[http://www.example.com/detail-35399645973]]> </url>
<url_wap><![CDATA[http://m.example.com/de99645973]]> </url_wap>
<price>310</price>
<promotion_price>93.8</promotion_price>
<wap_price>85</wap_price>
<sub_item_ids>
<sub_item_id>35399645973_1</sub_item_id>
<sub_item_id>35399645973_2</sub_item_id>
<sub_item_id>35399645973_3</sub_item_id>
……
</sub_item_ids>
<detail><![CDATA[商品詳情,支持html圖文混排]]> </detail>
<status>1<status>
<pic_main>
<img>
<url><![CDATA[http://www.example.com/10777829/T_400x400.jpg]]> </url>
<size>400x400</size>
</img>
</pic_main>
<pic_extra>
<img>
<url><![CDATA[http://www.example.com/10777821_400x400.jpg]]> </url>
<size>400x400</size>
</img>
<img>
<url><![CDATA[http://www.example.com/10777822_400x400.jpg]]> </url>
<size>400x400</size>
</img>
</pic_extra >
</item>
2:xml對應的model類!
(1):FanLiProductInofMsg.java
import java.util.List;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
/**
*
* @author aflyun
* @date 2016.06.12
*
*/
@XmlRootElement(name="item")
public class FanLiProductInofMsg {
@XmlAttribute
private String version;
@XmlElement
private String id;
@XmlElement
private String title;
@XmlElement
private String category;
@XmlElement
private String url;//Pc商品的url
@XmlElement(name="url_wap")
private String urlWap;//Wap商品的url url_wap
@XmlElement
private String price;
@XmlElement(name="promotion_price")
private String promotionPrice;//promotion_price
@XmlElement(name="wap_price")
private String wapPrice;//wap_price
@XmlElementWrapper(name="sub_item_ids")
@XmlElement(name="sub_item_id")
private List<String> subItemIds;//sub_item_ids
@XmlElement
private String detail;//detail
@XmlElement
private String status;//status
@XmlElementWrapper(name="pic_main")
@XmlElement(name="img")
private List<Img> mainImg;//pic_main
@XmlElementWrapper(name="pic_extra")
@XmlElement(name="img")
private List<Img> extraImg;//pic_extra
public void setVersion(String version) {
this.version = version;
}
public void setId(String id) {
this.id = id;
}
public void setTitle(String title) {
this.title = title;
}
public void setCategory(String category) {
this.category = category;
}
public void setUrl(String url) {
this.url = url;
}
public void setUrlWap(String urlWap) {
this.urlWap = urlWap;
}
public void setPrice(String price) {
this.price = price;
}
public void setPromotionPrice(String promotionPrice) {
this.promotionPrice = promotionPrice;
}
public void setWapPrice(String wapPrice) {
this.wapPrice = wapPrice;
}
public void setSubItemIds(List<String> subItemIds) {
this.subItemIds = subItemIds;
}
public void setDetail(String detail) {
this.detail = detail;
}
public void setStatus(String status) {
this.status = status;
}
public void setMainImg(List<Img> mainImg) {
this.mainImg = mainImg;
}
public void setExtraImg(List<Img> extraImg) {
this.extraImg = extraImg;
}
}
(2):Img .java
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
/**
*
* @author aflyun
* @date 2016.06.12
*
*/
@XmlRootElement
public class Img {
@XmlElement(name="url")
private String url;
@XmlElement(name="size")
private String size;
public void setUrl(String url) {
this.url = url;
}
public void setSize(String size) {
this.size = size;
}
}
3:生成過程,已經CDATA問題處理!
package com.dufy.test.xml;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.transform.sax.SAXResult;
import org.apache.xml.serialize.OutputFormat;
import org.apache.xml.serialize.XMLSerializer;
import org.junit.Test;
import com.kuyu.b2b2c.product.fanli.vo.FanLiProductInofMsg;
import com.kuyu.b2b2c.product.fanli.vo.Img;
public class JuintXmlTest {
private static Marshaller marshal;
@Test
public void testXml() throws Exception {
List<String> list = new ArrayList<String>();
list.add("11_11_11");
list.add("22_22_22");
list.add("33_33_33");
List<Img> imgList = new ArrayList<Img>();
Img img = null;
for (int i = 0; i < 2; i++) {
img = new Img();
img.setUrl("-" + i + "-");
img.setSize("40×40");
imgList.add(img);
}
FanLiProductInofMsg fps = new FanLiProductInofMsg();
fps.setVersion("1.0");
fps.setId("110");
fps.setTitle("4K 高清");
fps.setCategory("電視>4K>高清");
fps.setUrl("http://baidu.com");
fps.setUrlWap("http://baidu.wap.com");
fps.setPrice("100");
fps.setPromotionPrice("111");
fps.setWapPrice("113");
fps.setSubItemIds(list);
fps.setDetail("wwwwwwwwwwwwwwwwwwwwwww");
fps.setStatus("1");
fps.setMainImg(imgList);
fps.setExtraImg(imgList);
PrintWriter pw = new PrintWriter(new File("D:/test.xml"));
String ojbectToXmlWithCDATA = ojbectToXmlWithCDATA(FanLiProductInofMsg.class, fps);
System.out.println(ojbectToXmlWithCDATA);
pw.println(ojbectToXmlWithCDATA);
pw.close();
}
public static String ojbectToXmlWithCDATA(Class clazz, Object obj) throws Exception {
JAXBContext context = JAXBContext.newInstance(clazz);
// configure an OutputFormat to handle CDATA
OutputFormat of = new OutputFormat();
of.setCDataElements(
new String[] { "^title", //
"^category",
"^url",
"^url_wap",
"^detail"
}); //
// set any other options you'd like
of.setPreserveSpace(true);
of.setIndenting(true);
// create the serializer
ByteArrayOutputStream op = new ByteArrayOutputStream();
XMLSerializer serializer = new XMLSerializer(op, of);
SAXResult result = new SAXResult(serializer.asContentHandler());
Marshaller mar = context.createMarshaller();
mar.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
mar.marshal(obj, result);
return op.toString("utf-8");
}
}
setCDataElements 這裡面你標註需CDATA 的欄位!
上面的這個例子可以直接拷貝運行,網上還一些其他的方法生成xml和CDATA,如果你有好的方法,歡迎分享給我,謝謝!
4:參考文章
(1):Jaxb2 轉換XML文檔
(2):Jaxb annotation初步使用
(3):三步解決JAXB生成XML包含CDATA問題—JAVA編程
歡迎訪問我的csdn博客,我們一同成長!
"不管做什麼,只要堅持下去就會看到不一樣!在路上,不卑不亢!"
http://blog.csdn.net/u010648555