2

我是 JAXB 的新手,可能有一个相当简单的解决方案,但我不知道该怎么做。我可能会从我无法控制的设备接收到以下 xml。

样品 1

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

样品 2

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

我使用 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;
}

当我解组样本 1 一切正常时,我可以通过调用 getTrack1() 方法访问 LoyaltyID (636497123456678)。但是,如果我收到示例 2 中的 XML,我将无法访问该数据。应该指出的是,当 entryMethod 属性设置为“手动”时,我只收到示例 2 XML。我不仅需要能够解组此 XML,而且还需要将其编组回最初发送它的设备。有谁知道我怎么能做到这一点?

非常感谢解决此问题的任何帮助。

谢谢!- 蒂姆

4

1 回答 1

1

您可以使用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;
}

结果如下:

<?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>

您只需参考eclipselink.jar档案。

于 2013-06-15T15:58:04.677 回答