gpt4 book ai didi

java - 具有 List (@XmlAnyElement) 类型属性的 JAXB 编码对象不输出节点值

转载 作者:行者123 更新时间:2023-12-04 06:14:19 27 4
gpt4 key购买 nike

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

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 的独立应用程序。

测试工具
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 中不可见,尽管我在上面的独立应用程序中设置了它的值。有人可以帮我解决这个问题吗?是什么导致了这个问题?

最佳答案

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

The value of this node, depending on its type; see the table above. When it is defined to be null, setting it has no effect.



如果向上滚动一点,您会看到一个表格,其中提到了 Element类型节点定义为 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>

关于java - 具有 List<Element> (@XmlAnyElement) 类型属性的 JAXB 编码对象不输出节点值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7445996/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com