1

私のxiスキーマのroot.classでは、要素itemオブジェクトとohterオブジェクトはitemListの一部です。

@XmlElementRef(name = "item", namespace = "xi", type = JAXBElement.class, required = false) 
//...
protected List<Object> itemList;

私はObjectFactory.classメインスキーマから次のようなJAXBElementsとしていくつかのアイテムを持っています:

@XmlElementDecl(namespace = "de-schema", name = "detailedInformation", substitutionHeadNamespace = "xi", substitutionHeadName = "item")
public JAXBElement<numItemType> createDetailedInformation(numItemType num) {
    return new JAXBElement<numItemType>(_detailedInformation_QNAME, numItemType.class, null, num);
}

したがって、numItemTypeには、のいくつかの属性とvalue(num)がありJAXBElementます。

NumItemType.class:

@XmlJavaTypeAdapter(numItemTypeAdapter.class)
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "numItemType", namespace = "xi", propOrder = {
    "num"
})

public class NumItemType {

    @XmlValue
    protected BigDecimal num;
    @XmlAttribute(name = "precision")
    protected String precision;
    @XmlAttribute(name = "decimals")
    protected String decimals;
    //... more Attributes

}

ただしJAXB、ドキュメントを非整列XML化すると、次のような要素のみが含まれます。

<detailedInformation>
    <element1>1234</element1>
    <element2>5678</element2>
    <element3>bla</element3>
</detailedInformation>

私がそれをマーシャリングすると、それは(JAXB javaコードのように)次のようになるはずです:

<detailedInformation element2="5678" element3="bla">1234</detailedInformation>

したがって、NumItemTypeAdapterを使用してnumItemTypeAdapter.classを記述し、XmlAdapterを拡張しました。

AdaptedNum.class:

public class AdaptedNum {
    @XmlElement 
    private  double element1;
    @XmlElement
    private String element2;
    @XmlElement
    private String element3;

   /** Some getter/setter methods */
}

http://blog.bdoughan.com/2012/02/xmlanyelement-and-xmladapter.html助けになると思いましたが、少し注意が必要です:-/

4

3 に答える 3

0

問題が発生している場所を正確に分類するのは少し難しいです。元のモデルが XML スキーマから生成されたと仮定していますが、これは変更なしでそのまま機能するはずです。以下に、役立つ可能性のある例の縮小版を提供しようとしました。

package forum11343610;

import java.util.*;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.annotation.*;

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Root {

    @XmlElementRef(name = "item", namespace = "xi", type = JAXBElement.class, required = false) 
    protected List<Object> itemList = new ArrayList<Object>();

    public List<Object> getItemList() {
        return itemList;
    }

    public void setItemList(List<Object> itemList) {
        this.itemList = itemList;
    }

}

NumItemType

package forum11343610;

import java.math.BigDecimal;
import javax.xml.bind.annotation.*;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "numItemType", namespace = "xi", propOrder = {
    "num"
})

public class NumItemType {

    @XmlValue
    protected BigDecimal num;
    @XmlAttribute(name = "precision")
    protected String precision;
    @XmlAttribute(name = "decimals")
    protected String decimals;
    //... more Attributes

}

ObjectFactory

package forum11343610;

import javax.xml.bind.JAXBElement;
import javax.xml.bind.annotation.*;
import javax.xml.namespace.QName;

@XmlRegistry
public class ObjectFactory {

    private static final QName _detailedInformation_QNAME = new QName("de-schema", "detailedInformation");

    @XmlElementDecl(namespace = "xi", name = "item")
    public JAXBElement<NumItemType> createItem(NumItemType num) {
        return new JAXBElement<NumItemType>(_detailedInformation_QNAME, NumItemType.class, null, num);
    }

    @XmlElementDecl(namespace = "de-schema", name = "detailedInformation", substitutionHeadNamespace = "xi", substitutionHeadName = "item")
    public JAXBElement<NumItemType> createDetailedInformation(NumItemType num) {
        return new JAXBElement<NumItemType>(_detailedInformation_QNAME, NumItemType.class, null, num);
    }

}

デモ

package forum11343610;

import java.math.BigDecimal;
import javax.xml.bind.*;

public class Demo {

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

        ObjectFactory objectFactory = new ObjectFactory();
        Root root = new Root();

        NumItemType numItemType = new NumItemType();
        numItemType.num = BigDecimal.TEN;
        numItemType.decimals = "1";
        numItemType.precision = "2";
        root.getItemList().add(objectFactory.createDetailedInformation(numItemType));

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(root, System.out);
    }

}

出力

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<root xmlns:ns2="de-schema" xmlns:ns3="xi">
    <ns2:detailedInformation precision="2" decimals="1">10</ns2:detailedInformation>
</root>
于 2012-07-05T14:23:25.290 に答える
0

コメントありがとうございます。

それがポイントです:

デモ

NumItemType numItemType = new NumItemType();
numItemType.num = BigDecimal.TEN;
numItemType.decimals = "1";
numItemType.precision = "2";
root.getItemList().add(objectFactory.createDetailedInformation(numItemType));

XML を自動的にアンマーシャリングしてマップする必要があります。

XML 入力

<detailedInformation>
     <element1>1234</element1>
     <element2>5678</element2>
     <element3>bla</element3>
</detailedInformation>

コードで:

デモ

JAXBContext jc = JAXBContext.newInstance(Root.class, ObjectFactory.class);
Unmarshaller u = jc.createUnmarshaller();
File xml = new File("D:/", "test.xml");
Root root = (Root) u.unmarshal(xml);

出力

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<root xmlns:ns2="de-schema" xmlns:ns3="xi">
    <ns2:detailedInformation precision="2" decimals="1">10</ns2:detailedInformation>
</root>

DOM を使用して XML ドキュメントをツリーに解析し、JAXB を使用してマーホール化することができました...

ありがとうございました!

于 2012-07-06T08:17:48.180 に答える
0

言い換えると:

スキーマ Java コードを変更せずにアンマーシャリングのために NumItemType.class に新しい要素を設定したい。

NumItemType.class

package forum11343610;

import java.math.BigDecimal;
import javax.xml.bind.annotation.*;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "numItemType", namespace = "xi", propOrder = {
    "num"
})

    @XmlJavaTypeAdapter(NumItemTypeAdapter.class)
    public class NumItemType {

        @XmlValue
        protected BigDecimal num;
        @XmlAttribute(name = "precision")
        protected String precision;
        @XmlAttribute(name = "decimals")
        protected String decimals;
        //... more Attributes

    }

NumItemTypeAdapter.class

public  class NumItemTypeAdapter extends XmlAdapter<AdaptedNum, NumItemType> {

    @Override
    public NumItemType unmarshal(AdaptedNum an) throws Exception {
        NumItemType nit = new NumItemType();
        nit.setNum(an.getNum);
        nit.setPrecision(an.getPrecision);
        nit.setDecimals(an.getDecimals)
        return nit;
    }

}

AdaptedNum.class

    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(name = "adaptedNum", namespace = "", propOrder = {
        "element1",
        "element2",
        "element3"
    })

    public class AdaptedNum {

        @XmlElement(name ="element1")
        protected  BigDecimal num;
        @XmlElement(name ="element2")
            private String decimals;
        @XmlElement(name ="element3")
            private String precison;
            // set/get method
}
于 2012-07-06T14:58:51.670 に答える