JAXB - Проблеми с Marshall/Unmarshall

Аз съм нов в 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, всичко работи чудесно, имам достъп до LoyaltyID (636497123456678) чрез извикване на метода getTrack1(). Ако обаче получа XML в пример 2, нямам достъп до тези данни. Трябва да се отбележи, че получавам примерен 2 XML само когато атрибутът entryMethod е зададен на "ръчно". Трябва да мога не само да демаршалирам този XML, но и да го маршалирам обратно към устройството, което го е изпратило първоначално. Някой знае ли как мога да постигна това?

Всяка помощ за разрешаването на това е много ценена.

Благодаря! - Тим


person milltj    schedule 15.06.2013    source източник
comment
Предоставете своя дефиниционен файл (xsd).   -  person Rong Nguyen    schedule 15.06.2013


Отговори (1)


Можете да използвате внедряването на JAXB EclipseLink MOXy за достъп до < анотация за strong>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.

person Pragmateek    schedule 15.06.2013
comment
Да, можете, eclipselink.jar е просто още един ванилов буркан, който можете да изтеглите от тук: eclipse.org/eclipselink/downloads/index.php#2.5 просто го добавете към вашия classpath, така че анотацията на XmlPath да е достъпна. - person Pragmateek; 15.06.2013