我需要使用 JAXB 的以下格式输出
<?xml version="1.0" encoding="UTF-8"?>
<tata-sky>
<change-package-request>
<sub-no>1064781105</sub-no>
<ivr-id>ubona_7301341344310891411101430133</ivr-id>
<date-time>05/29/2013 00:16:13</date-time>
<package-list>
<package-ref>Punjabi Regional Free Pack</package-ref>
<action-code>Delete</action-code>
</package-list>
</change-package-request>
</tata-sky>
我得到的输出
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<tata-sky>
<change-package-request>
<sub-no>1064781105</sub-no>
<ivr-id>ubona_7301341344310891411101430133</ivr-id>
<date-time>05/29/2013 00:16:13</date-time>
</change-package-request>
<package-ref>
<package-ref>Punjabi Regional Free Pack</package-ref>
<action-code>Delete</action-code>
</package-ref>
</tata-sky>
('package-ref' 应该是 'change-package-request' 的子级。下面是我的代码)
更改包.java
package com.tsg.flow;
/**
*
* @author Shafiq
*/
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.XmlType;
@XmlRootElement(name = "tata-sky")
public class ChangePackage {
//@XmlElement(name = "change-package-request", required = true)
private ChangepackageRequest changePReq;
public void setChangePReq(ChangepackageRequest changePReq) {
this.changePReq = changePReq;
}
@XmlElement(name = "change-package-request")
public ChangepackageRequest getChangePReq() {
return changePReq;
}
private PackageRef packageList;
public void setPackageList(PackageRef packageList) {
this.packageList = packageList;
}
@XmlElement(name = "package-ref")
public PackageRef getPackageList() {
return packageList;
}
public ChangePackage() {
}
}
更改packageRequest.java
package com.tsg.flow;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
/**
*
* @author Shafiq
*/
@XmlRootElement(name = "change-package-request")
@XmlType(propOrder = {"sub_no", "ivr_id", "date_time"})
public class ChangepackageRequest {
private String sub_no;
private String ivr_id;
private String date_time;
/* private PackageList packageList;
public void setPackageList(PackageList packageList) {
this.packageList = packageList;
}
@XmlElement(name = "package-ref")
public PackageList getPackageList() {
return packageList;
}*/
//@XmlElementWrapper(name = "change-package-request")
@XmlElement(name = "sub-no")
public String getSub_no() {
return sub_no;
}
public void setSub_no(String sub_no) {
this.sub_no = sub_no;
}
@XmlElement(name = "ivr-id")
public String getIvr_id() {
return ivr_id;
}
public void setIvr_id(String ivr_id) {
this.ivr_id = ivr_id;
}
@XmlElement(name = "date-time")
public String getDate_time() {
return date_time;
}
public void setDate_time(String date_time) {
this.date_time = date_time;
}
}
PackageRef.java
package com.tsg.flow;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
/**
*
* @author Shafiq
*/
@XmlRootElement(name = "package-list")
@XmlType(propOrder = {"package_ref", "action_code"})
public class PackageRef {
private String package_ref;
private String action_code;
// @XmlElementWrapper(name = "package-list")
@XmlElement(name = "package-ref")
public String getPackage_ref() {
return package_ref;
}
public void setPackage_ref(String package_ref) {
this.package_ref = package_ref;
}
//@XmlElementWrapper(name = "package-list")
@XmlElement(name = "action-code")
public String getAction_code() {
return action_code;
}
public void setAction_code(String action_code) {
this.action_code = action_code;
}
}
主要方法:ChangePackageService.java
package com.tsg.flow;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
/**
*
* @author Shafiq
*/
public class ChangePackageService {
public static void main(String... args) {
// public String createXMLString() {
ChangePackage objCP = new ChangePackage();
ChangepackageRequest cr = new ChangepackageRequest();
cr.setSub_no("1064781105");
cr.setIvr_id("ubona_7301341344310891411101430133");
cr.setDate_time("05/29/2013 00:16:13");
PackageRef pl = new PackageRef();
pl.setPackage_ref("Punjabi Regional Free Pack");
pl.setAction_code("Delete");
objCP.setPackageList(pl);
objCP.setChangePReq(cr);
// create JAXB context and instantiate marshaller
JAXBContext context = null;
Marshaller m = null;
try {
context = JAXBContext.newInstance(ChangePackage.class);
m = context.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
// Write to System.out
//m.marshal(cs, System.out);
// Write to File
m.marshal(objCP, System.out);
} catch (JAXBException ex) {
System.out.println("Error XML Generation" + ex.getMessage());
// System.out.println("Exception Occured during marshalling: " + ex.getMessage());
}
// return "";
// }
}
}
请大家尽快回复解决方案。谢谢