具有List <element>类型属性的JAXB编组对象(@XmlAnyElement)不输出节点值</element>

时间:2011-09-16 14:11:06

标签: java jaxb marshalling

以下文件TPAExtensionsType.java我是从XSD文件生成的。

TPAExtensionsType.java

/* 
 * <p>The following schema fragment specifies the expected content contained within this class.
 * 
 * <pre>
 * &lt;complexType name="TPA_Extensions_Type">
 *   &lt;complexContent>
 *     &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
 *       &lt;sequence>
 *         &lt;any processContents='skip' maxOccurs="unbounded" minOccurs="0"/>
 *       &lt;/sequence>
 *     &lt;/restriction>
 *   &lt;/complexContent>
 * &lt;/complexType>
 * </pre>
 * 
 * 
 */ 
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "TPA_Extensions_Type", propOrder = {
    "any"
})
@XmlRootElement 
public class TPAExtensionsType {

    @XmlAnyElement
    protected List<Element> any;

    /**
     * Gets the value of the any property.
     * 
     * <p>
     * This accessor method returns a reference to the live list,
     * not a snapshot. Therefore any modification you make to the
     * returned list will be present inside the JAXB object.
     * This is why there is not a <CODE>set</CODE> method for the any property.
     * 
     * <p>
     * For example, to add a new item, do as follows:
     * <pre>
     *    getAny().add(newItem);
     * </pre>
     * 
     * 
     * <p>
     * Objects of the following type(s) are allowed in the list
     * {@link Element }
     * 
     * 
     */
    public List<Element> getAny() {
    if (any == null) {
        any = new ArrayList<Element>();
    }
    return this.any;
    }

}

以下是将上述对象编组为XML的独立应用程序。

TestUtil.java

public class TestUtil {
    public static void main(String[] args) {
        TPAExtensionsType tpaExtensions = new TPAExtensionsType();
        Element consumerInfo = new DOMElement("ConsumerInfo");
        consumerInfo.setNodeValue("Some Info");
        tpaExtensions.getAny().add(consumerInfo);

        StringWriter sw  = new StringWriter();
        JAXBContext context;
        try {
            context = JAXBContext.newInstance(TPAExtensionsType.class);
            Marshaller marshaller = context.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
            marshaller.marshal(tpaExtensions, sw);
            System.out.println(sw.toString());
        } catch (JAXBException e) {
            e.printStackTrace();
        }

    }
}

以下是输出:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<tpaExtensionsType xmlns="SOME_NAMESPACE_ HERE">
    <ConsumerInfo xmlns="" xmlns:ns2="SOME_NAMESPACE_ HERE"/>
</tpaExtensionsType>

我面临的问题:

节点 ConsumerInfo 已经创建,但是它的值在生成的XML中不可见,虽然我已在上面的独立应用程序中设置了它的值。任何人都可以帮我解决这个问题,导致什么原因造成的这个问题?

1 个答案:

答案 0 :(得分:3)

引用DOM spec on nodeValue强调我的):

  

此节点的值,具体取决于其类型; 见上表如果将其定义为null,则将其设置为无效。

如果向上滚动一下,您会看到一个表格,其中提到用Element nodeValue定义null类型节点。我想这就是为什么它不会出现在你的XML中,因为设置它没有效果

也许您可以使用Node.setTextContent(String textContent)

Document doc = DocumentBuilderFactory.newInstance()
                  .newDocumentBuilder().newDocument();
Element consumerInfo = doc.createElement("consumerInfo");
consumerInfo.setTextContent("some info");
doc.appendChild(consumerInfo);
TPAExtensionsType tp = new TPAExtensionsType();
tp.getAny().add((Element) doc.getFirstChild());

JAXBContext jc = JAXBContext.newInstance(TPAExtensionsType.class);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(tp, System.out); 

输出

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<tpaExtensionsType>
    <consumerInfo>some info</consumerInfo>
</tpaExtensionsType>