繁体   English   中英

生成漂亮的JAXB类

[英]Generating Pretty JAXB classes

我过去曾使用JAXB进行XML解析,并为它创建了自己的简单类。 那些是POJO

public class Foo {

    @XmlAttribute
    public String someAttribute;

    public String someElement;

    public Bar bar;
}

现在我想编写一个必须从非常复杂的xml结构中解析数据的工具,我想避免自己编写所有这些数据。 我尝试使用xjc来生成类,但它们看起来与我上面的示例完全不同:

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

    @XmlElementRefs({ @XmlElementRef(name = "flavor2", type = JAXBElement.class, required = false),
            @XmlElementRef(name = "specific", type = JAXBElement.class, required = false),
            @XmlElementRef(name = "category2", type = JAXBElement.class, required = false),
            @XmlElementRef(name = "flavor1", type = JAXBElement.class, required = false),
            @XmlElementRef(name = "print-prereqs", type = JAXBElement.class, required = false),
            @XmlElementRef(name = "prereqs2", type = JAXBElement.class, required = false),
            @XmlElementRef(name = "rules", type = JAXBElement.class, required = false),
            @XmlElementRef(name = "prereqs1", type = JAXBElement.class, required = false) })
    @XmlMixed
    protected List<Serializable> content;
    @XmlAttribute(name = "name")
    protected String name;
    @XmlAttribute(name = "type1")
    protected String type1;
    @XmlAttribute(name = "internal-id")
    protected String internalId;
    @XmlAttribute(name = "source")
    protected String source;
    @XmlAttribute(name = "revision-date")
    protected String revisionDate;

    /* getters and setters omitted */

    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(name = "", propOrder = { "value" })
    public static class Category2 {

        @XmlValue
        protected String value;
        @XmlAttribute(name = "name")
        protected String name;

        /* getters and setters omitted */

    }

    /* additional class definitions omitted */

}

要获取名为“flavor1”的元素,我必须在“content”属性中搜索名为“flavor1”的JAXBElement,我觉得这非常不方便。 我希望能够做的是:

String flavor1 = rulesElement.getFlavor1();

有没有办法使用xjc或其他工具来实现这一目标?

编辑:来自my xsd的RulesElementType的complexType是:

  <xs:complexType name="RulesElementType" mixed="true">
    <xs:choice maxOccurs="unbounded" minOccurs="0">
      <xs:element name="category2">
        <xs:complexType>
          <xs:simpleContent>
            <xs:extension base="xs:string">
              <xs:attribute type="xs:string" name="name" use="optional"/>
            </xs:extension>
          </xs:simpleContent>
        </xs:complexType>
      </xs:element>
      <xs:element name="prereqs2">
        <xs:complexType>
          <xs:simpleContent>
            <xs:extension base="xs:string">
              <xs:attribute type="xs:string" name="name" use="optional"/>
            </xs:extension>
          </xs:simpleContent>
        </xs:complexType>
      </xs:element>
      <xs:element type="xs:string" name="print-prereqs"/>
      <xs:element name="flavor2">
        <xs:complexType mixed="true">
          <xs:choice maxOccurs="unbounded" minOccurs="0">
            <xs:element type="grantType" name="grant"/>
            <xs:element type="stataddType" name="statadd"/>
            <xs:element type="textstringType" name="textstring"/>
            <xs:element type="selectType" name="select"/>
            <xs:element type="replaceType" name="replace"/>
            <xs:element type="modifyType" name="modify"/>
            <xs:element type="dropType" name="drop"/>
            <xs:element type="suggestType" name="suggest"/>
            <xs:element type="stataliasType" name="statalias"/>
          </xs:choice>
          <xs:attribute type="xs:string" name="name" use="optional"/>
        </xs:complexType>
      </xs:element>
      <xs:element type="specificType" name="specific"/>
      <xs:element type="rulesType" name="rules"/>
      <xs:element type="xs:string" name="prereqs1"/>
      <xs:element name="flavor1">
        <xs:complexType mixed="true">
          <xs:choice maxOccurs="unbounded" minOccurs="0">
            <xs:element type="grantType" name="grant"/>
            <xs:element type="stataddType" name="statadd"/>
            <xs:element type="textstringType" name="textstring"/>
            <xs:element type="selectType" name="select"/>
            <xs:element type="replaceType" name="replace"/>
            <xs:element type="modifyType" name="modify"/>
            <xs:element type="dropType" name="drop"/>
            <xs:element type="suggestType" name="suggest"/>
            <xs:element type="stataliasType" name="statalias"/>
          </xs:choice>
          <xs:attribute type="xs:string" name="name" use="optional"/>
        </xs:complexType>
      </xs:element>
    </xs:choice>
    <xs:attribute type="xs:string" name="name" use="optional"/>
    <xs:attribute type="xs:string" name="type1" use="optional"/>
    <xs:attribute type="xs:string" name="internal-id" use="optional"/>
    <xs:attribute type="xs:string" name="source" use="optional"/>
    <xs:attribute type="xs:string" name="revision-date" use="optional"/>
  </xs:complexType>

XML文件看起来与类似。

我使用XMLBeans来生成xsd。 由于我正在读取的XML文件大约有680k行,因此不能手动创建XSD。

您可以使用Simplify插件来简化content属性。

免责声明:我是作者。

请看这个问题:

从选择中使用jaxb从xsd生成java类

提取元素文本值

简而言之,如果您使用此插件并使用simplify:as-element-property自定义进入content属性的其中simplify:as-element-property ,则可以为每个元素提供易于使用的元素属性。 我希望这就是你对“漂亮”的意思。

暂无
暂无

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

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