具有类型为List< Element>的属性的JAXB编组对象. (@XmlAnyElement)不输出节点值 [英] JAXB marshalling object having property of type List<Element> (@XmlAnyElement) doesnt output node value
问题描述
以下是我从XSD文件生成的文件TPAExtensionsType.java.
Following file TPAExtensionsType.java I have generated from a XSD file.
TPAExtensionsType.java
/*
* <p>The following schema fragment specifies the expected content contained within this class.
*
* <pre>
* <complexType name="TPA_Extensions_Type">
* <complexContent>
* <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
* <sequence>
* <any processContents='skip' maxOccurs="unbounded" minOccurs="0"/>
* </sequence>
* </restriction>
* </complexContent>
* </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的独立应用程序.
Following is the standalone application to marshall the above object to XML.
TestUtil.java
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中它的值不可见.这个问题吗?
The node ConsumerInfo has been created but its value is not visible in the generated XML though I have set its value in my standalone application above.Can anybody please help me getting this fixed and what is causing this problem?
推荐答案
引用
此节点的值,取决于其类型; 请参见上表. 如果将其定义为null,则设置无效.
如果向上滚动一点,将会看到一个表,其中提到 If you scroll up a bit, you'll see a table where it mentions that 也许您可以使用 Maybe you could use 输出 这篇关于具有类型为List< Element>的属性的JAXB编组对象. (@XmlAnyElement)不输出节点值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!Element
类型的节点是用null
nodeValue定义的.我猜这就是为什么它不显示在您的XML中的原因,因为设置它无效.Element
type nodes are defined with a null
nodeValue. I guess that's why it doesn't show up in your XML, because setting it has no effect.Node.setTextContent(String textContent)
?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>