0

我需要使用 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 "";
        //  }
    }
}

请大家尽快回复解决方案。谢谢

4

2 回答 2

0

看起来您已经注释掉了ChangepackageRequest告诉 JAXB是iepackage-list的子代的部分代码change-package-request

@XmlElement(name = "package-ref")
public PackageList getPackageList() {`

你需要做的是

  • 取消注释该代码
  • 更新它以使用PackageRef类而不是PackageList
  • 删除对PackageReffrom 的任何引用ChangePackage
于 2013-08-03T10:25:00.387 回答
0

我找到了解决方案...

在 ChangePackageRequest.java 中

"added @XmlType(propOrder = {""}))"

因此@XmlType(propOrder = {"", "date_time", "ivr_id", "sub_no"}) 它假设 package-ref 是 change-package-request 的子代。

于 2013-08-03T10:47:13.073 回答