英文:
JAXB Generating unexpected tag name, returns type name
问题
以下是翻译好的内容:
我遇到了一个问题,我生成的 XML 与我预期的不符。
我有以下的 XSD...
<xs:element name="DOSLog" type="DOSLogType"/>
<xs:complexType name="DOSLogType">
<xs:sequence>
<xs:element name="Transaction" type="DOSLogTransaction"/>
</xs:sequence>
</xs:complexType>
当我从这些 JAXB 对象生成 XML 时,我得到了以下的 XML...
<DOSLogType>
<Transaction/>
</DOSLogType>
我没有预期输出的标签是 <DOSLogType>
,而是 <DOSLog>
。
代码返回的对象类型是 DOSLogType.java
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(
name = "DOSLogType",
propOrder = {"transaction"}
)
@XmlRootElement(
name = "DOSLogType"
)
public class DOSLogType implements Cloneable, CopyTo, Equals, HashCode, ToString {
我希望我能够通过 bindings.xjc
控制生成的对象和 XML 标签名称,但似乎这并不起作用。
<jaxb:bindings scd="~arts:DOSLogType"><jaxb:class ref="org.doslog.bean.DOSLogType" /></jaxb:bindings>
有人能否建议如何控制生成的 XML?
===================================
对此有两个解决方案,第一个是使用 XSD 方法,如下所示。
第二个是在 bindings.xjc
中进行修复。
<jaxb:bindings xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:inheritance="http://jaxb2-commons.dev.java.net/basic/inheritance"
xmlns:annox="http://annox.dev.java.net"
jaxb:extensionBindingPrefixes="inheritance annox"
jaxb:version="2.1">
<jaxb:bindings schemaLocation="DOSLog.xsd" node="/xs:schema">
<jaxb:bindings node="xs:complexType[@name='DOSLogType']">
<annox:annotate>
<annox:annotate annox:class="javax.xml.bind.annotation.XmlRootElement" name="DOSLog"/>
</annox:annotate>
</jaxb:bindings>
</jaxb:bindings>
</jaxb:bindings>
英文:
I have the issue that the XML I'm generating does not match what I would expect.
I have the following XSD...
<xs:element name="DOSLog" type="DOSLogType"/>
<xs:complexType name="DOSLogType">
<xs:sequence>
<xs:element name="Transaction" type="DOSLogTransaction"/>
</xs:sequence>
</xs:complexType>
When I generate the XML from these JAXB objects I get the following XML...
<DOSLogType>
<Transaction/>
</DOSLogType>
I was not expecting the outputted tag to be <DOSLogType>, but to be <DOSLog>.
The code is returning an object of type DOSLogType.java
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(
name = "DOSLogType",
propOrder = {"transaction"}
)
@XmlRootElement(
name = "DOSLogType"
)
public class DOSLogType implements Cloneable, CopyTo, Equals, HashCode, ToString {
I was hoping that I could control the generated Object and XML tag name with the bindings.xjc
This does not seem to work though.
<jaxb:bindings scd="~arts:DOSLogType"><jaxb:class ref="org.doslog.bean.DOSLogType" /></jaxb:bindings>
Can anyone suggest how I can control XML that is generated?
===================================
There were two solutions to this, the XSD approach shown below.
The second is to fix it in the bindings.xjc
<jaxb:bindings xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:inheritance="http://jaxb2-commons.dev.java.net/basic/inheritance"
xmlns:annox="http://annox.dev.java.net"
jaxb:extensionBindingPrefixes="inheritance annox"
jaxb:version="2.1">
<jaxb:bindings schemaLocation="DOSLog.xsd" node="/xs:schema">
<jaxb:bindings node="xs:complexType[@name='DOSLogType']">
<annox:annotate>
<annox:annotate annox:class="javax.xml.bind.annotation.XmlRootElement" name="DOSLog"/>
</annox:annotate>
</jaxb:bindings>
答案1
得分: 1
您可以在XSD本身中使用内联绑定。
<xs:element name="DOSLog" type="DOSLogType"/>
<xs:complexType name="DOSLogType">
<xs:annotation>
<xs:documentation>一些DOSLog类型的文档。</xs:documentation>
<xs:appinfo>
<annox:annotate>
<annox:annotate annox:class="javax.xml.bind.annotation.XmlRootElement" name="DOSLog"/>
</annox:annotate>
<jaxb:class name="DOSLogType"/>
</xs:appinfo>
</xs:annotation>
<xs:sequence>
<xs:element name="Transaction" type="DOSLogTransaction"/>
</xs:sequence>
</xs:complexType>
注意:这里的内容是XML模式定义(XSD),其中包含了一些关于DOSLog类型的信息。
英文:
You can use inline binding in xsd itself
<xs:element name="DOSLog" type="DOSLogType"/>
<xs:complexType name="DOSLogType">
<xs:annotation>
<xs:documentation>Some DOSLog type documentatioms.</xs:documentation>
<xs:appinfo>
<annox:annotate>
<annox:annotate annox:class="javax.xml.bind.annotation.XmlRootElement" name="DOSLog"/>
</annox:annotate>
<jaxb:class name="DOSLogType"/>
</xs:appinfo>
</xs:annotation>
<xs:sequence>
<xs:element name="Transaction" type="DOSLogTransaction"/>
</xs:sequence>
</xs:complexType>
答案2
得分: 0
你可以在使用编组器生成XML字符串时设置根元素名称。
要做到这一点,为你的DOCLogType类型的对象创建一个JAXBElement。
像这样:
protected Marshaller marshaller;
protected <T> String marshal(T xmlBean, Class<T> clazz, String rootElementNamespace, String rootElementName) {
StringWriter stringWriter = new StringWriter();
try {
JAXBElement<T> jaxbElement = new JAXBElement<T>(new QName(rootElementNamespace, rootElementName), clazz,
xmlBean);
marshaller.marshal(jaxbElement, stringWriter);
} catch (Exception e) {
log.error(e);
throw new RuntimeException(e);
}
return stringWriter.toString();
}
英文:
You can set root element name while generating XML string with marshaller.<br>
To do this create JAXBElement for your object of DOCLogType type.<br>
Something like this:<br>
protected Marshaller marshaller;
protected <T> String marshal(T xmlBean, Class<T> clazz, String rootElementNamespace, String rootElementName) {
StringWriter stringWriter = new StringWriter();
try {
JAXBElement<T> jaxbElement = new JAXBElement<T>(new QName(rootElementNamespace, rootElementName), clazz,
xmlBean);
marshaller.marshal(jaxbElement, stringWriter);
} catch (Exception e) {
log.error(e);
throw new RuntimeException(e);
}
return stringWriter.toString();
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论