java⽣成xmlcdata_java注解⽣成xml和包含CDATA问题百度java⽣成xml,有⼀⼤推的⽂章,主要的⽣成⽅式⼀种使⽤Dom4J ,还有⼀种使⽤Jdk⾃带注解类!
下⾯主要整理我注解类的使⽤,(可以参考这篇⽂章Dom4J⽣成xml和包含CDATA问题)和xml中CDATA 问题的解决⽅法!
1:要⽣成的xml原始⽂件!
35399645973
保湿>洗护]]>
310
93.8
85
35399645973_1
35399645973_2
35399645973_3
……
1
400x400
400x400
400x400
2:xml对应的model类!
(1):FanLiProductInofMsg.java
import java.util.List;
l.bind.annotation.XmlAttribute;
l.bind.annotation.XmlElement;
dom4j读取xml
l.bind.annotation.XmlElementWrapper;
l.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 subItemIds;//sub_item_ids
@XmlElement
private String detail;//detail
@XmlElement
private String status;//status
@XmlElementWrapper(name="pic_main")
@XmlElement(name="img")
private List mainImg;//pic_main
@XmlElementWrapper(name="pic_extra")
@XmlElement(name="img")
private List 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 subItemIds) {
this.subItemIds = subItemIds;
}
public void setDetail(String detail) {
this.detail = detail;
}
public void setStatus(String status) {
this.status = status;
}
public void setMainImg(List mainImg) {
this.mainImg = mainImg;
}
public void setExtraImg(List extraImg) {
}
}
(2):Img .java
l.bind.annotation.XmlElement; l.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 st.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;
l.bind.JAXBContext;
l.bind.Marshaller;
l.transform.sax.SAXResult;
import l.serialize.OutputFormat;
import l.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 list = new ArrayList();
list.add("11_11_11");
list.add("22_22_22");
list.add("33_33_33");
List imgList = new ArrayList();
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("baidu");
fps.setUrlWap("baidu.wap");
fps.setPrice("100");
fps.setPromotionPrice("111");
fps.setWapPrice("113");

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。