@XmlPath无法正常工作

时间:2014-09-08 19:18:22

标签: spring jaxb

@XmlPath无效。

Customer.java

import org.eclipse.persistence.oxm.annotations.XmlPath;

@XmlRootElement(name= "Customer")

public class Customer {

    private String CustomerId;
    private String organizationCode;    
    private Extn extn;
    private String organizationName;
    private int reset;
    private CustomerSchedulingPreferences customerSchedulingPreferences;
    private ArrayList<RestrictedState> restrictedStateList;

    @XmlAttribute
    public String getCustomerId() {
        return CustomerId;
    }
    public void setCustomerId(String customerId) {
        CustomerId = customerId;
    }
    @XmlAttribute
    public String getOrganizationCode() {
        return organizationCode;
    }
    public void setOrganizationCode(String organizationCode) {
        this.organizationCode = organizationCode;
    }   
    @XmlElement(name="Extn")    
    public Extn getExtn() {
        return extn;
    }
    public void setExtn(Extn extn) {
        this.extn = extn;
    }       
    @XmlPath("BuyerOrganization/@OrganizationName")
    public String getOrganizationName() {
        return organizationName;
    }
    public void setOrganizationName(String organizationName) {
        this.organizationName = organizationName;
    }
    @XmlPath("BuyerOrganization/Extn/USSCORestrictedStateList")
     @XmlElement(name = "USSCORestrictedState")
    public ArrayList<RestrictedState> getRestrictedStateList() {
        return restrictedStateList;
    }
    public void setRestrictedStateList(ArrayList<RestrictedState> restrictedStateList) {
        this.restrictedStateList = restrictedStateList;
    }

    @XmlPath("BuyerOrganization/Extn/USSCORestrictedStateList/@Reset")
    public int getReset() {
        return reset;
    }
    public void setReset(int reset) {
        this.reset = reset;
    }
    @XmlElement(name="CustomerSchedulingPreferences")
    public CustomerSchedulingPreferences getCustomerSchedulingPreferences() {
        return customerSchedulingPreferences;
    }
    public void setCustomerSchedulingPreferences(
            CustomerSchedulingPreferences customerSchedulingPreferences) {
        this.customerSchedulingPreferences = customerSchedulingPreferences;
    }

}

Client.java

import javax.xml.transform.stream.StreamResult;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.oxm.Marshaller;


public class Client 
{
    public static void main(String[] args)throws IOException 
    {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        Marshaller marshaller = (Marshaller)context.getBean("jaxbMarshallerBean");

        Customer customer=new Customer();

        customer.setCustomerId("12345");
        customer.setOrganizationCode("SUPPLY");

        Extn extn = new Extn();
        extn.setExtnBillCreditCode("000");
        extn.setExtnBillSubscriptionId("132131");
        customer.setExtn(extn);



        RestrictedState resState1= new RestrictedState();
        resState1.setOrgCode("952121");
        resState1.setRestrictedStateCode("IN");

        RestrictedState resState2= new RestrictedState();
        resState2.setOrgCode("60325");
        resState2.setRestrictedStateCode("IL");

        ArrayList<RestrictedState> restrictedStateList = new ArrayList<RestrictedState>();
        restrictedStateList.add(resState1);
        restrictedStateList.add(resState2);



        CustomerSchedulingPreferences custSchedPref = new CustomerSchedulingPreferences();
        custSchedPref.setIsLineShipComplete("Y");
        custSchedPref.setIsLineShipSingleNode("N");
        custSchedPref.setOptimizationType("03");

        customer.setCustomerSchedulingPreferences(custSchedPref);
        customer.setRestrictedStateList(restrictedStateList);

        marshaller.marshal(customer, new StreamResult(new FileWriter("customer.xml")));

        System.out.println("XML Created Sucessfully");

    }
}

的applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:oxm="http://www.springframework.org/schema/oxm"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
        http://www.springframework.org/schema/oxm
        http://www.springframework.org/schema/oxm/spring-oxm-3.0.xsd">

        <oxm:jaxb2-marshaller id="jaxbMarshallerBean">
            <oxm:class-to-be-bound name="com.javatpoint.Customer"/>
        </oxm:jaxb2-marshaller>

</beans>

所需输出结构:

<Customer CustomerID="952121" OrganizationCode="SUPPLY" >
  <Extn ExtnBillCreditCode="000" ExtnBillSubscriptionID="952121" /> 
  <BuyerOrganization  OrganizationName="Buy.com1" >
     <Extn>
       <USSCORestrictedStateList Reset="Y">
         <USSCORestrictedState  OrganizationCode="952121" RestrictedStateCode="IN"/>
       </USSCORestrictedStateList>
     </Extn>
  </BuyerOrganization>
  <CustomerSchedulingPreferences IsLineShipComplete="" IsLineShipSingleNode="" /> 
</Customer>

=============================================== =================================

请帮我解决这个问题:

目前我的输出如下:

<Customer organizationCode="SUPPLY" customerId="12345">
<CustomerSchedulingPreferences IsLineShipSingleNode="N" IsLineShipComplete="Y"/>
<Extn ExtnBillSubscriptionID="132131" ExtnBillCreditCode="000"/>
<reset>0</reset>
<USSCORestrictedState restrictedStateCode="IN" OrganizationCode="952121"/>
<USSCORestrictedState restrictedStateCode="IL" OrganizationCode="60325"/>
</Customer>

1 个答案:

答案 0 :(得分:0)

要利用@XmlPath扩展,您需要使用EclipseLink MOXy作为JAXB提供程序。

以下是一个有助于设置此内容的链接: