繁体   English   中英

JAXB更改@XMLElements批注的元素“名称”

[英]JAXB change the Element “name” of @XMLElements annotation

我问题的根源是具有可变节点名称的给定XML。 XML已在使用中,不能更改。

我为XML创建了一个XSD,以生成相应的类,然后通过DOMParser对其进行解析。

我使用DOMParser是因为通过JAXB,由于变量标记名而无法解析,但是生成的类很有帮助。

现在,我只需要类中变量Node的标识符即可。 我认为最好只是更改@XmlElement Annotations元素“名称”,并给它一个像“?”的值。 来识别变量节点,但是我不知道是否以及如何更改它。

我已经创建了一个情节文件来更改某些类名,并认为可以通过该类名进行更改,但是我没有找到解决方案。

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>

变量节点为:名称为“ variable”的节点,以“ variable-column”开头和一个数字(表数据的子节点)开头的节点,以及以“ variable-series”和一个数字(sub-用户设置的节点)

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>

在生成它之后,带有视图列表的类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 = "?")

所以我知道我要搜索的标签名称不是View,但可以是任何东西。

我在这里先向您的帮助表示感谢。

我的解决方案是更换发电机。

首先,我像这样重新调整我的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-plugin,因为它易于与jaxb2-basics-annotate插件一起使用。 我给了generationplugin一个带有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)中,变量SeriesDefinition的XmlElement注释为:

@XmlElement(name = "?")

通过反射,我可以获得此注释,并检查“名称”属性是否为“?” 并将其写在“名称值”中我可以给班级。 当然,只有在每个类中最多有一个带有Annotation XmlElement(name =“?”)的变量时,这才起作用,我必须首先获取具有已定义名称的所有其他Tag,其余的必须是具有变量的Tag名称。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM