【问题标题】:Create XML from generated sources从生成的源创建 XML
【发布时间】:2017-10-04 11:54:04
【问题描述】:

我正忙于创建 XML 文档。 xsd 正在创建 java 类,但发生了一些奇怪的事情。我想要一个像这样的 XML

<SystemDocumentationService.GetSystemConfigurationResponse>
<SystemConfiguration>
    <TimeStamp>
        <Value>2017-10-04T13:30:38</Value>
    </TimeStamp>
    <ServiceStartList>
        <ServiceIdentification>
                <Service>
                  <ServiceName>CustomerInformationService</ServiceName>
                    <IBIS-IP-Version>
                        <Value>token</Value>
                    </IBIS-IP-Version>
                </Service>
        </ServiceIdentification>
    </ServiceStartList>
</SystemConfiguration>
</SystemDocumentationService.GetSystemConfigurationResponse>

但是我得到了这个

<SystemDocumentationService.GetSystemConfigurationResponse>
<SystemConfiguration>
    <TimeStamp>
        <Value>2017-10-04T13:30:38</Value>
    </TimeStamp>
    <ServiceStartList>
        <ServiceIdentification>
            <ServiceIdentification>
                <Service>
                    <ServiceName>CustomerInformationService</ServiceName>
                    <IBIS-IP-Version>
                        <Value>token</Value>
                    </IBIS-IP-Version>
                </Service>
            </ServiceIdentification>
        </ServiceIdentification>
    </ServiceStartList>
</SystemConfiguration>
</SystemDocumentationService.GetSystemConfigurationResponse>

正如您所见,出于某种原因,标签 ServiceIdentifcation 出现了两次,这是我不想要的。

作为 maven 插件,我正在使用这个

<plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>jaxb2-maven-plugin</artifactId>
            <version>1.5</version>
            <executions>
                <execution>
                    <id>ibis_ip</id>
                    <goals>
                        <goal>xjc</goal>
                    </goals>
                    <configuration>
                        <extension>true</extension>
                        <packageName>com.ximedes.giva.core.ibisip</packageName>
                        <schemaDirectory>${project.basedir}/src/main/resources/xsd</schemaDirectory>
                        <staleFile>${project.build.directory}/jaxb2/.ibisStaleFileFlag</staleFile>
                        <clearOutputDir>true</clearOutputDir>
                        <bindingDirectory>${project.basedir}/src/main/resources/xsd</bindingDirectory>
                        <bindingFiles>binding.xjb</bindingFiles>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>build-helper-maven-plugin</artifactId>
            <version>1.10</version>
            <executions>
                <execution>
                    <id>test</id>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>add-source</goal>
                    </goals>
                    <configuration>
                        <sources>
                            <source>${basedir}/target/generated-sources</source>
                        </sources>
                    </configuration>
                </execution>
            </executions>
        </plugin> 

生成的Java类就是这个

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "SystemDocumentationService.SystemConfigurationData", 
propOrder = {
"timeStamp",
"serviceStartList",
"deviceSpecificationList",
"heartbeatIntervall"
})
public class SystemDocumentationServiceSystemConfigurationData {

@XmlElement(name = "TimeStamp", required = true)
protected IBISIPDateTime timeStamp;
@XmlElement(name = "ServiceStartList", required = true)
protected ServiceStartListStructure serviceStartList;
@XmlElement(name = "DeviceSpecificationList", required = true)
protected DeviceSpecificationListStructure deviceSpecificationList;
@XmlElement(name = "HeartbeatIntervall")
protected IBISIPDouble heartbeatIntervall;

    //getter's and setter's
}

ServiceStartListStructure 是

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "ServiceStartListStructure", propOrder = {
"serviceIdentifications"
})
public class ServiceStartListStructure {

@XmlElement(name = "ServiceIdentification", required = true)
protected List<ServiceIdentificationStructure> serviceIdentifications;


public List<ServiceIdentificationStructure> getServiceIdentifications() {
    if (serviceIdentifications == null) {
        serviceIdentifications = new ArrayList<ServiceIdentificationStructure>();
    }
    return this.serviceIdentifications;
}

}

我猜maven插件的某些东西不正确,但我不确定。

编辑:

这里是 ServiceIdentificationStructure

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "ServiceIdentificationStructure", propOrder = {
"service",
"device"
})
public class ServiceIdentificationStructure {

@XmlElement(name = "Service", required = true)
protected ServiceSpecificationStructure service;
@XmlElement(name = "Device", required = true)
protected DeviceSpecificationStructure device;

/**
 * Gets the value of the service property.
 * 
 * @return
 *     possible object is
 *     {@link ServiceSpecificationStructure }
 *     
 */
public ServiceSpecificationStructure getService() {
    return service;
}

/**
 * Sets the value of the service property.
 * 
 * @param value
 *     allowed object is
 *     {@link ServiceSpecificationStructure }
 *     
 */
public void setService(ServiceSpecificationStructure value) {
    this.service = value;
}

/**
 * Gets the value of the device property.
 * 
 * @return
 *     possible object is
 *     {@link DeviceSpecificationStructure }
 *     
 */
public DeviceSpecificationStructure getDevice() {
    return device;
}

/**
 * Sets the value of the device property.
 * 
 * @param value
 *     allowed object is
 *     {@link DeviceSpecificationStructure }
 *     
 */
public void setDevice(DeviceSpecificationStructure value) {
    this.device = value;
}

}

【问题讨论】:

  • 请提供ServiceIdentificationStructure的映射关系。
  • 要删除包含列表的中间对象; this is what you need
  • 添加了ServiceIdentifactionStructure
  • @daniu 当我添加 XmlElementWrapper 注释时,我得到 CustomerInformationServicetoken

标签: java xml maven


【解决方案1】:

好的,我解决了我的问题。有两种解决方案。第一次使用

  • @JacksonXmlElementWrapper(useWrapping = false)

注解。我无法使用该解决方案,因为我的 java 代码是从我无法更改的 xsd 生成的。

不过,第二种解决方案是在 XmlMapper 上将选项 defaultUseWrapper 设置为 false。

XmlMapper mapper = new XmlMapper();
mapper.setDefaultUseWrapper(false);

【讨论】:

    猜你喜欢
    • 2015-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-29
    • 2012-12-18
    • 2016-02-22
    • 1970-01-01
    相关资源
    最近更新 更多