2017-10-18 105 views
0

我的問題的根源是給定的XML與變量節點名。 XML已被使用且無法更改。JAXB改變元素「名」 @XMLElements註釋

我創建了一個XSD爲XML生成corrosponding類,然後通過的DOMParser解析它。

我使用的DOMParser因爲通過JAXB它不能分析,因爲變量標籤名稱,但生成的類是有幫助的。

現在我只需要在類的變量節點的標識符。我認爲最好只更改@XmlElement Annotations元素「name」,並給它一個值,如「?」來識別變量節點,但我不知道是否以及如何改變它。

我已經創建了一個小插曲文件來改變一些類名,並認爲它可以通過改變,但我沒有找到一個解決方案。

例XML:

<Settings> 
    <Window> 
     <Position Top="5" Left="5"/> 
    </Window> 
    <OpenData> 
     <variable> 
      <TableData> 
       <variable-column00 POS="1" Visible="true" SortDesc="true"/> 
       <variable-column01 POS="3" Visible="true" SortDesc="true"/> 
       <variable-column02 POS="2" Visible="true" SortDesc="true"/> 
      </TableData> 
     </variable> 
     <variable> 
      <UserSettings> 
       <variable-series1 Color="blue" Caption="Series blue" Visible="true" /> 
       <variable-series2 Color="yellow" Caption="Series yellow" Visible="true" /> 
       <variable-series3 Color="red" Caption="Series red" Visible="true" /> 
      </UserSettings> 
      <ChartConfig>configuration-json....</ChartConfig> 
     </variable> 
     <variable/> 
    </OpenData> 
    </Settings> 

變量節點將是: 節點名爲「可變的」,節點用「可變柱」和一個數字(表的數據的子節點)開始和用「可變序列」和一個數量開始(的用戶設置的子節點)的節點

這種情況的XSD是:

<?xml version="1.0" encoding="UTF-8"?> 
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified"> 

    <xs:complexType name="Window"> 
    <xs:complexContent> 
     <xs:extension base="SettingsElement"> 
     <xs:sequence> 
      <xs:element name="Position" type="Position" minOccurs="0" maxOccurs="1"/> 
     </xs:sequence> 
     </xs:extension> 
    </xs:complexContent> 
    </xs:complexType> 

    <xs:complexType name="Position"> 
    <xs:complexContent> 
     <xs:extension base="SettingsElement"> 
     <xs:attribute name="Top" type="xs:int" /> 
     <xs:attribute name="Left" type="xs:int" /> 
     </xs:extension> 
    </xs:complexContent> 
    </xs:complexType> 

    <xs:complexType name="OpenData"> 
    <xs:complexContent> 
     <xs:extension base="SettingsElement"> 
     <xs:sequence> 
      <!-- this would be the "variable" Node --> 
      <xs:element name="View" type="View" minOccurs="0" maxOccurs="unbounded" /> 
     </xs:sequence> 
     </xs:extension> 
    </xs:complexContent> 
    </xs:complexType> 


    <xs:complexType name="View"> 
    <xs:complexContent> 
     <xs:extension base="SettingsElement"> 
     <xs:sequence> 
     <!-- normaly the order would be random, but because i use the DOMParser i don't need to pass that to JAXB --> 
     <xs:element name="TableData" type="TableData" minOccurs="0" maxOccurs="unbounded" /> 
     <xs:element name="UserSettings" type="UserSettings" minOccurs="0" maxOccurs="1" /> 
     <xs:element name="ChartConfig" type="xs:string" minOccurs="0" maxOccurs="1" /> 
     </xs:sequence> 
     </xs:extension> 
    </xs:complexContent> 
    </xs:complexType> 

    <xs:complexType name="UserSettings"> 
    <xs:complexContent> 
     <xs:extension base="SettingsElement"> 
     <xs:sequence> 
     <!-- the next Variable Node name beginning with "variable-series" and then a number --> 
     <xs:element name="SeriesDefinition" type="SeriesDefinition" minOccurs="0" maxOccurs="unbounded" /> 
     </xs:sequence> 
     </xs:extension> 
    </xs:complexContent> 
    </xs:complexType> 

    <xs:complexType name="SeriesDefinition"> 
    <xs:complexContent> 
     <xs:extension base="SettingsElement"> 
     <xs:attribute name="Color" type="xs:string" /> 
     <xs:attribute name="Caption" type="xs:string" /> 
     </xs:extension> 
    </xs:complexContent> 
    </xs:complexType> 


    <xs:complexType name="TableData"> 
    <xs:complexContent> 
     <xs:extension base="SettingsElement"> 
     <xs:sequence> 
     <!-- the next Variable Node name beginning with "variable-column" and then a number --> 
     <xs:element name="Column" type="Column" minOccurs="0" maxOccurs="unbounded" /> 
     </xs:sequence> 
     </xs:extension> 
    </xs:complexContent> 
    </xs:complexType> 

    <xs:complexType name="Column"> 
    <xs:complexContent> 
     <xs:extension base="SettingsElement"> 
     <xs:attribute name="POS" type="xs:int" /> 
     <xs:attribute name="Visible" type="xs:boolean" /> 
     </xs:extension> 
    </xs:complexContent> 
    </xs:complexType> 

    <xs:element name="SettingsElement"> 
    </xs:element> 


    <xs:element name="Settings" > 
    <xs:complexType> 
      <xs:sequence> 
      <xs:element name="Window" type="Window" minOccurs="0" /> 
      <xs:element name="OpenData" type="OpenData" minOccurs="0" /> 
      </xs:sequence> 
    </xs:complexType> 
    </xs:element> 

</xs:schema> 

後,我生成它,我的C美景一覽姑娘將opendata會是這樣:

import java.io.Serializable; 
import java.util.ArrayList; 
import javax.xml.bind.annotation.XmlAccessType; 
import javax.xml.bind.annotation.XmlAccessorType; 
import javax.xml.bind.annotation.XmlElement; 
import javax.xml.bind.annotation.XmlType; 

@XmlAccessorType(XmlAccessType.FIELD) 
@XmlType(name = "OpenData", propOrder = { 
    "view" 
}) 
public class OpenData extends SettingsElement implements Serializable { 

    private final static long serialVersionUID = 1L; 
    @XmlElement(name = "View") 
    protected java.util.List<View> view; 

    public java.util.List<View> getview() { 
     if (view == null) { 
      view = new ArrayList<View>(); 
     } 
     return this.view; 
    } 

} 

我需要一個標識爲我的分析工作,因此像改變註釋

@XmlElement(name = "View") 

@XmlElement(name = "?") 

所以我知道我正在搜索的標籤名稱不是查看,但可以是任何東西。

感謝您的幫助提前。

+0

歡迎來到StackOverflow!請查看[提問問題指南](https://stackoverflow.com/help/asking),特別是[如何創建最小,完整和可驗證示例](https://stackoverflow.com/help/MCVE) – AesSedai101

回答

0

我的解決方案是改變發生器。

首先,我重新調整我的POM這樣的:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 


    <groupId>com.example</groupId> 
    <artifactId>my-generator</artifactId> 
    <version>1.0.0-SNAPSHOT</version> 

    <dependencies> 
    <dependency> 
     <groupId>org.jvnet.jaxb2_commons</groupId> 
     <artifactId>jaxb2-basics-tools</artifactId> 
     <version>1.11.1</version> 
    </dependency> 
    </dependencies> 

    <build> 

    <plugins> 
     <plugin> 
     <groupId>org.jvnet.jaxb2.maven2</groupId> 
     <artifactId>maven-jaxb2-plugin</artifactId> 
     <executions> 
      <execution> 
      <id>example</id> 
      <phase>generate-sources</phase> 
      <goals> 
       <goal>generate</goal> 
      </goals> 
      <inherited>false</inherited> 
      <configuration> 
       <schemaDirectory>src/main/resources/META-INF/schemas/</schemaDirectory> 
       <schemaIncludes> 
       <include>configuration.xsd</include> 
       </schemaIncludes> 

       <generateDirectory>${project.build.directory}/generated-sources/</generateDirectory> 
       <generatePackage>com.example.config</generatePackage> 
       <extension>true</extension> 
       <args> 
       <arg>-Xannotate</arg> 
       </args> 

       <plugins> 
       <plugin> 
        <groupId>org.jvnet.jaxb2_commons</groupId> 
        <artifactId>jaxb2-basics-annotate</artifactId> 
        <version>1.0.1</version> 
       </plugin> 
       </plugins> 
      </configuration> 
      </execution> 
      </executions> 
     </plugin> 
    </plugins> 
    </build> 
</project> 

我用org.jvnet.jaxb2.maven2.maven-JAXB2-插件,因爲它很容易與JAXB2-基礎,註釋插件使用。 我給了一個帶有一個Id,一個階段和目標的執行代碼,然後在配置執行過程中,我向其顯示了xsd文件的路徑並命名了wich模式。然後我告訴它把輸出給它一個說法,「-xannotate」,而且是重要的組成部分,與和給定的插件,可以通過* .xjb文件更改註釋(綁定文件)

我將我的* .episode文件更改爲*。XJB文件,因爲該發電機可以自動檢測到它們,並添加了線改變「的XmlElement」註釋:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<jxb:bindings xmlns:jxb="http://java.sun.com/xml/ns/jaxb" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc" xmlns:annox="http://annox.dev.java.net" jxb:extensionBindingPrefixes="Xequals annox" xmlns:javaee="http://java.sun.com/xml/ns/javaee" version="2.1"> 


    <jxb:globalBindings xmlns:jxb="http://java.sun.com/xml/ns/jaxb" xmlns:xs="http://www.w3.org/2001/XMLSchema"> 
    <xjc:serializable uid="1" /> 
    </jxb:globalBindings> 

    <jxb:bindings schemaLocation="configuration.xsd"> 
    <jxb:bindings node="//xs:element[@name='Settings']"> 
     <jxb:bindings node="//xs:element[@name='OpenData']"> 
     <jxb:bindings node="//xs:element[@name='View']"> 
      <annox:annotate target="field">@javax.xml.bind.annotation.XmlElement(name="?")</annox:annotate> 
      <jxb:bindings node="//xs:element[@name='TableData']"> 
      <jxb:bindings node="//xs:element[@name='Column']"> 
       <annox:annotate target="field">@javax.xml.bind.annotation.XmlElement(name="?")</annox:annotate> 
      </jxb:bindings> 
      </jxb:bindings> 
      <jxb:bindings node="//xs:element[@name='UserSettings']"> 
      <jxb:bindings node="//xs:element[@name='SeriesDefinition']"> 
       <annox:annotate target="field">@javax.xml.bind.annotation.XmlElement(name="?")</annox:annotate> 
      </jxb:bindings> 
      </jxb:bindings> 
     </jxb:bindings> 
     </jxb:bindings> 
    </jxb:bindings> 
    </jxb:bindings> 
</jxb:bindings> 

現在在生成的Java類如UserSettings的的XmlElement註釋的變量SeriesDefinition是:

@XmlElement(name = "?") 

並與反思我能得到這個註解,並檢查「名」 Propertie是「?」並寫在名字值我可以給班。 關閉當然,這隻適用於每個類中存在最多一個帶有Annotation XmlElement(name =「?」)的變量時,我必須首先獲取其他具有已定義名稱的其他Tag,其餘的必須是具有變量名稱。