简体   繁体   English

JAXB-Marshall / Unmarshall问题

[英]JAXB - Marshall/Unmarshall issues

I am new to JAXB and have what is probably a fairly easy solution, but I am not sure how to do it. 我是JAXB的新手,可能有一个相当简单的解决方案,但是我不确定该怎么做。 It is possible that I can receive the following xml from a device that I do not control. 我可能会从不受控制的设备接收以下xml。

Sample 1 样品1

 <LoyaltyID entryMethod="swipe">
      <Track1>636497123456678</Track1>
 </LoyaltyID>

Sample 2 样品2

 <LoyaltyID entryMethod="manual">636497123456678</LoyaltyID>

I used xjc to create a JAXB class that has the following definitions: 我使用xjc创建了具有以下定义的JAXB类:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "track1",
    "track2",
    "track3"
})
@XmlRootElement(name = "LoyaltyID")
public class LoyaltyID {

@XmlElement(name = "Track1")
protected String track1;
@XmlElement(name = "Track2")
protected String track2;
@XmlElement(name = "Track3")
protected String track3;
@XmlAttribute(name = "entryMethod")
protected String entryMethod;

/**
 * Gets the value of the track1 property.
 * 
 * @return
 *     possible object is
 *     {@link String }
 *     
 */
public String getTrack1() {
    return track1;
}

/**
 * Sets the value of the track1 property.
 * 
 * @param value
 *     allowed object is
 *     {@link String }
 *     
 */
public void setTrack1(String value) {
    this.track1 = value;
}

/**
 * Gets the value of the track2 property.
 * 
 * @return
 *     possible object is
 *     {@link String }
 *     
 */
public String getTrack2() {
    return track2;
}

/**
 * Sets the value of the track2 property.
 * 
 * @param value
 *     allowed object is
 *     {@link String }
 *     
 */
public void setTrack2(String value) {
    this.track2 = value;
}

/**
 * Gets the value of the track3 property.
 * 
 * @return
 *     possible object is
 *     {@link String }
 *     
 */
public String getTrack3() {
    return track3;
}

/**
 * Sets the value of the track3 property.
 * 
 * @param value
 *     allowed object is
 *     {@link String }
 *     
 */
public void setTrack3(String value) {
    this.track3 = value;
}

/**
 * Gets the value of the entryMethod property.
 * 
 * @return
 *     possible object is
 *     {@link String }
 *     
 */
public String getEntryMethod() {
    if (entryMethod == null) {
        return "swipe";
    } else {
        return entryMethod;
    }
}

/**
 * Sets the value of the entryMethod property.
 * 
 * @param value
 *     allowed object is
 *     {@link String }
 *     
 */
public void setEntryMethod(String value) {
    this.entryMethod = value;
}

When I unmarshall Sample 1 everything works great, I am able to access the LoyaltyID (636497123456678) via a call to the getTrack1() method. 当我解组样本1时,一切工作正常,我能够通过调用getTrack1()方法访问LoyaltyID(636497123456678)。 However, if I receive the XML in Sample 2 I can't access that data. 但是,如果我在示例2中收到XML,则无法访问该数据。 It should be pointed out that I only receive the Sample 2 XML when the entryMethod attribute is set to "manual". 应该指出的是,仅当entryMethod属性设置为“ manual”时,我才接收示例2 XML。 I need to be able to not only unmarshall this XML but also marshall it back to the device that sent it originally. 我不仅需要解组该XML,还需要将其编组回最初发送它的设备。 Does anyone know how I can accomplish this? 有人知道我该怎么做吗?

Any help in resolving this is greatly appreciated. 解决该问题的任何帮助将不胜感激。

Thanks! 谢谢! - Tim -蒂姆

You can use the JAXB EclipseLink MOXy implementation to access the XmlPath annotation: 您可以使用JAXB EclipseLink MOXy实现来访问XmlPath批注:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "track1",
    "track2",
    "track3",
    "value" })
@XmlRootElement(name = "LoyaltyID")
public static class LoyaltyID
{
    @XmlElement(name = "Track1")
    protected String track1;
    @XmlElement(name = "Track2")
    protected String track2;
    @XmlElement(name = "Track3")
    protected String track3;
    @XmlAttribute(name = "entryMethod")
    protected String entryMethod;
    @XmlPath("text()")
    protected String value;

    /**
     * Gets the value of the track1 property.
     * 
     * @return
     *     possible object is
     *     {@link String }
     *     
     */
    public String getTrack1() {
        return track1;
    }

    /**
     * Sets the value of the track1 property.
     * 
     * @param value
     *     allowed object is
     *     {@link String }
     *     
     */
    public void setTrack1(String value) {
        this.track1 = value;
    }

    /**
     * Gets the value of the track2 property.
     * 
     * @return
     *     possible object is
     *     {@link String }
     *     
     */
    public String getTrack2() {
        return track2;
    }

    /**
     * Sets the value of the track2 property.
     * 
     * @param value
     *     allowed object is
     *     {@link String }
     *     
     */
    public void setTrack2(String value) {
        this.track2 = value;
    }

    /**
     * Gets the value of the track3 property.
     * 
     * @return
     *     possible object is
     *     {@link String }
     *     
     */
    public String getTrack3() {
        return track3;
    }

    /**
     * Sets the value of the track3 property.
     * 
     * @param value
     *     allowed object is
     *     {@link String }
     *     
     */
    public void setTrack3(String value) {
        this.track3 = value;
    }

    /**
     * Gets the value of the entryMethod property.
     * 
     * @return
     *     possible object is
     *     {@link String }
     *     
     */
    public String getEntryMethod() {
        if (entryMethod == null) {
            return "swipe";
        } else {
            return entryMethod;
        }
    }

    /**
     * Sets the value of the entryMethod property.
     * 
     * @param value
     *     allowed object is
     *     {@link String }
     *     
     */
    public void setEntryMethod(String value) {
        this.entryMethod = value;
    }

    /**
     * Gets the value of the entryMethod property.
     * 
     * @return
     *     possible object is
     *     {@link String }
     *     
     */
    public String getValue() {
        return this.value;
    }

    /**
     * Sets the value of the entryMethod property.
     * 
     * @param value
     *     allowed object is
     *     {@link String }
     *     
     */
    public void setValue(String value) {
        this.value = value;
    }
}

public static void main(String[] args) throws Exception
{
    // Should work too but not for me: JAXBContext context = javax.xml.bind.JAXBContext.newInstance(LoyaltyID.class);
    JAXBContext context = org.eclipse.persistence.jaxb.JAXBContextFactory.createContext(new Class[]{LoyaltyID.class}, null);

    Marshaller marshaller = context.createMarshaller();
    Unmarshaller unmarshaller = context.createUnmarshaller();

    LoyaltyID li1 = (LoyaltyID)unmarshaller.unmarshal(new ByteArrayInputStream("<LoyaltyID entryMethod=\"swipe\"><Track1>636497123456678</Track1></LoyaltyID>".getBytes()));
    LoyaltyID li2 = (LoyaltyID)unmarshaller.unmarshal(new ByteArrayInputStream("<LoyaltyID entryMethod=\"manual\">636497123456678</LoyaltyID>".getBytes()));

    marshaller.marshal(li1, System.out);
    System.out.println();
    marshaller.marshal(li2, System.out);

    return;
}

Here is the result: 结果如下:

<?xml version="1.0" encoding="UTF-8"?>
<LoyaltyID entryMethod="swipe"><Track1>636497123456678</Track1></LoyaltyID>
<?xml version="1.0" encoding="UTF-8"?>
<LoyaltyID entryMethod="manual">636497123456678</LoyaltyID>

You only have to reference the eclipselink.jar archive. 您只需要引用eclipselink.jar存档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM