2017-10-04 48 views
3

我忙于创建XML文档。 xsd创建了java类,但是发生了一些奇怪的事情。我想有一个XML等这样从生成的来源创建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; 
} 

} 
+1

请给我们提供'ServiceIdentificationStructure'的映射。 –

+0

您想删除包含列表的中间对象; [这是你需要](https://stackoverflow.com/questions/18247182/remove-intermediate-class-when-generating-code-from-schema) – daniu

+0

ServiceIdentifactionStructure被添加 – Philipp

回答

0

好吧,我固定我的问题。有两种解决方案。在第一次使用

  • @JacksonXmlElementWrapper(useWrapping =假)

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

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

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