簡體   English   中英

JAXB-Marshall / Unmarshall問題

[英]JAXB - Marshall/Unmarshall issues

我是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屬性設置為“ manual”時,我才接收示例2 XML。 我不僅需要解組該XML,還需要將其編組回最初發送它的設備。 有人知道我該怎么做嗎?

解決該問題的任何幫助將不勝感激。

謝謝! -蒂姆

您可以使用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存檔。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM