1

在我的 xi-schema 的 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 extends XmlAdapter 编写了一个 numItemTypeAdapter.class

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

感谢您的评论。

这才是重点:

演示

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

换句话说:

我想为 NumItemType.class 设置新元素以进行解组,而无需更改架构 java 代码。

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 回答
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;
    }

}

项目类型

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

}

对象工厂

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 回答