1

<Pty ID="ID1" Src="6" R="1">を使用してタグを生成するのを手伝ってくれる人がいますか

@XmlPath単一注釈のEclipseLink MOXy 。

よろしくお願いします。

4

1 に答える 1

0

あなたの要件が正しいかどうかはわかりませんが、あなたがやろうとしていることに対する答えは次のとおりです。

IdAdapter

を記述しXmlAdapterて、単一の id 値を複数のプロパティを持つオブジェクトに変換できます。これらの他のプロパティには、デフォルト値が設定されます。

package forum11965153;

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.adapters.XmlAdapter;

public class IdAdapter extends XmlAdapter<IdAdapter.AdaptedId, String> {

    public static class AdaptedId {
        @XmlAttribute(name="ID") public String id;
        @XmlAttribute(name="Src") public String src = "6";
        @XmlAttribute(name="R") public String r = "1";
    }

    @Override
    public AdaptedId marshal(String string) throws Exception {
        AdaptedId adaptedId = new AdaptedId();
        adaptedId.id = string;
        return adaptedId;
    }

    @Override
    public String unmarshal(AdaptedId adaptedId) throws Exception {
        return adaptedId.id;
    }
}

Pty

@XmlJavaTypeAdapter注釈は、を指定するために使用されますXmlAdapter。値をルート要素に折りたたむために、MOXy の@XmlPath(".")注釈が使用されます ( http://blog.bdoughan.com/2010/07/xpath-based-mapping.htmlを参照)。

package forum11965153;

import javax.xml.bind.annotation.*;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;

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

@XmlRootElement(name="Pty")
@XmlAccessorType(XmlAccessType.FIELD)
public class Pty {

    @XmlJavaTypeAdapter(IdAdapter.class)
    @XmlPath(".")
    String id;

}

jaxb.properties

ご存知のように、MOXy を JAXB プロバイダーとして指定するにはjaxb.properties、次のエントリを使用して、ドメイン モデルと同じパッケージで呼び出されるファイルを含める必要があります ( http://blog.bdoughan.com/search/label/jaxb.propertiesを参照)。 )

javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory

デモ

次のデモ コードを使用して、すべてが機能することを証明できます。

package forum11965153;

import java.io.StringReader;
import javax.xml.bind.*;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Pty.class);

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        StringReader xml = new StringReader("<Pty ID='ID1' Src='6' R='1'/>");
        Pty pty = (Pty) unmarshaller.unmarshal(xml);

        Marshaller marshaller = jc.createMarshaller();
        marshaller.marshal(pty, System.out);
    }

}

出力

以下は、デモ コードを実行した結果の出力です。

<?xml version="1.0" encoding="UTF-8"?>
<Pty ID="ID1" Src="6" R="1"/>
于 2012-08-16T14:08:47.647 に答える