我想从struts2中的多个动作类生成下面提到的xml文件。
我从我的Struts 2操作类执行多个下面提到的操作,并将它们添加到单个现有的xml文件中。
1)在xml文件中添加新闻
2)在xml文件中添加页脚信息
3)在xml文件中添加代理商信息。
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<agencylink>
<agency>
<title>Ministry of Games</title>
<url>http://www.abc.gov.</url>
</agency>
<agencyNews>
<date>22/Apr/2014</date>
<news>How do I clear my web browsers cache, cookies, and history?</news>
</agencyNews>
<headerLogo>
<img>images/header.jpg</img>
</headerLogo>
<footerText>
<ftxt>All rights reserved.</ftxt>
</footerText>
<aLoginLogo>
<img>images/logo1.jpg</img>
</aLoginLogo>
<aHeaderLogo>
<img>images/logo2.jpg</img>
</aHeaderLogo>
</agencylink>
我正在使用JaxB。 当我执行这个不同的不同操作时,我的xml文件附加了该信息,但是当它存储在xml文件中时,标签名称是相同的。
标记名称必须与上面提到的xml文件类似。
当我更新新闻信息时,所有标签名称都更改为<agencyNews>
。
所以请任何人帮我通过JaxB获取这个xml格式。
下面是我的Java代码
package com.ocr.nsw.xml;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlSeeAlso;
@XmlRootElement(name = "agencylink")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlSeeAlso({ Agency.class, AgencyNews.class, HeaderLogo.class,
FooterText.class, ALoginLogo.class, AHeaderLogo.class })
public class AgencyLink {
@XmlElement(name = "agencyNews")
private List<AgencyNews> adate;
@XmlElement(name = "agencyNews")
private List<AgencyNews> anews;
@XmlElement(name = "agency")
private List<Agency> atitle;
@XmlElement(name = "agency")
private List<Agency> aurl;
@XmlElement(name = "footerText")
private List<FooterText> aftxt;
@XmlElement(name = "headerLogo")
private List<HeaderLogo> headerLogoImg;
@XmlElement(name = "aLoginLogo")
private List<ALoginLogo> loginLogoImg;
@XmlElement(name = "aHeaderLogo")
private List<AHeaderLogo> aHeaderLogoImg;
}
_____
public static void xmlWrite(List list, String filePath) {
AgencyLink agencyLink = new AgencyLink();
agencyLink.setAurl(null);
agencyLink.setAnews(list);
agencyLink.setAtitle(null);
agencyLink.setAftxt(null);
agencyLink.setHeaderLogoImg(null);
agencyLink.setLoginLogoImg(null);
agencyLink.setaHeaderLogoImg(null);
if (marshalling(filePath, agencyLink)) {
System.out.println("Success");
}
}
------
@SuppressWarnings("rawtypes")
public static Object unMarshalling(String filePath, Class clazz) {
try {
File file = new File(filePath);
JAXBContext jaxbContext = JAXBContext.newInstance(clazz);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
Object obj = clazz.cast(jaxbUnmarshaller.unmarshal(file));
return obj;
} catch (JAXBException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
从这段代码中我得到了下面提到的XML文件
<?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?>
<agencylink>
<agencyNews xsi:type="agency" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<title>Ministry of Health</title>
<url>http://www.abc.gov.bn</url>
</agencyNews>
<agencyNews>
<date>22/Apr/2014</date>
<news>How do I clear my web browsers cache, cookies, and history?</news>
</agencyNews>
<agencyNews>
<date>20/10/2013</date>
<news>10</news>
</agencyNews>
<agencyNews xsi:type="headerLogo" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<img>images/header.jpg</img>
</agencyNews>
<agencyNews xsi:type="footerText" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<ftxt>All rights reserved</ftxt>
</agencyNews>
<agencyNews xsi:type="aLoginLogo" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<img>images/logo1.jpg</img>
</agencyNews>
<agencyNews xsi:type="aHeaderLogo" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<img>images/logo2.jpg</img>
</agencyNews>
</agencylink>