JAXB生成意外的标签名,返回类型名

huangapple go评论83阅读模式
英文:

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...

&lt;xs:element name=&quot;DOSLog&quot; type=&quot;DOSLogType&quot;/&gt;
&lt;xs:complexType name=&quot;DOSLogType&quot;&gt;
	&lt;xs:sequence&gt;
		&lt;xs:element name=&quot;Transaction&quot; type=&quot;DOSLogTransaction&quot;/&gt;
	&lt;/xs:sequence&gt;
&lt;/xs:complexType&gt;

When I generate the XML from these JAXB objects I get the following XML...

&lt;DOSLogType&gt;
    &lt;Transaction/&gt;
&lt;/DOSLogType&gt;

I was not expecting the outputted tag to be &lt;DOSLogType>, but to be &lt;DOSLog>.

The code is returning an object of type DOSLogType.java

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(
  name = &quot;DOSLogType&quot;,
  propOrder = {&quot;transaction&quot;}
)
@XmlRootElement(
  name = &quot;DOSLogType&quot;
)
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.

&lt;jaxb:bindings scd=&quot;~arts:DOSLogType&quot;&gt;&lt;jaxb:class ref=&quot;org.doslog.bean.DOSLogType&quot; /&gt;&lt;/jaxb:bindings&gt;

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

&lt;jaxb:bindings xmlns:jaxb=&quot;http://java.sun.com/xml/ns/jaxb&quot;
	xmlns:xjc=&quot;http://java.sun.com/xml/ns/jaxb/xjc&quot;
	xmlns:xs=&quot;http://www.w3.org/2001/XMLSchema&quot;
	xmlns:inheritance=&quot;http://jaxb2-commons.dev.java.net/basic/inheritance&quot;
	xmlns:annox=&quot;http://annox.dev.java.net&quot;
	jaxb:extensionBindingPrefixes=&quot;inheritance annox&quot;
	jaxb:version=&quot;2.1&quot;&gt;

&lt;jaxb:bindings schemaLocation=&quot;DOSLog.xsd&quot; node=&quot;/xs:schema&quot;&gt;
	&lt;jaxb:bindings node=&quot;xs:complexType[@name=&#39;DOSLogType&#39;]&quot;&gt;
		&lt;annox:annotate&gt;
			&lt;annox:annotate annox:class=&quot;javax.xml.bind.annotation.XmlRootElement&quot; name=&quot;DOSLog&quot;/&gt;
		&lt;/annox:annotate&gt;
	&lt;/jaxb:bindings&gt;

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

&lt;xs:element name=&quot;DOSLog&quot; type=&quot;DOSLogType&quot;/&gt;
&lt;xs:complexType name=&quot;DOSLogType&quot;&gt;
&lt;xs:annotation&gt;
                    &lt;xs:documentation&gt;Some DOSLog type documentatioms.&lt;/xs:documentation&gt;
                    &lt;xs:appinfo&gt;
                            &lt;annox:annotate&gt;
                                &lt;annox:annotate annox:class=&quot;javax.xml.bind.annotation.XmlRootElement&quot; name=&quot;DOSLog&quot;/&gt;
                            &lt;/annox:annotate&gt;
                            &lt;jaxb:class name=&quot;DOSLogType&quot;/&gt;
                        &lt;/xs:appinfo&gt;
                    &lt;/xs:annotation&gt;
    &lt;xs:sequence&gt;
        &lt;xs:element name=&quot;Transaction&quot; type=&quot;DOSLogTransaction&quot;/&gt;
    &lt;/xs:sequence&gt;
&lt;/xs:complexType&gt;

答案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 &lt;T&gt; String marshal(T xmlBean, Class&lt;T&gt; clazz, String rootElementNamespace, String rootElementName) {
	StringWriter stringWriter = new StringWriter();

	try {
		JAXBElement&lt;T&gt; jaxbElement = new JAXBElement&lt;T&gt;(new QName(rootElementNamespace, rootElementName), clazz,
				xmlBean);

		marshaller.marshal(jaxbElement, stringWriter);
	} catch (Exception e) {
		log.error(e);
		throw new RuntimeException(e);
	}

	return stringWriter.toString();
}

huangapple
  • 本文由 发表于 2020年7月23日 22:22:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/63056520.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定