我过去曾使用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文件看起来有点像this。
我使用XMLBeans来生成xsd。由于我正在读取的XML文件大约有680k行,因此无法手动创建XSD。
答案 0 :(得分:2)
您可以使用Simplify plugin来简化content
属性。
SO免责声明:我是作者。
请看这个问题:
Generate java classes from xsd with jaxb from a choice
简而言之,如果您使用此插件并使用content
自定义进入simplify:as-element-property
属性的其中一个元素,则可以为每个元素提供易于使用的元素属性。我希望这就是你对“漂亮”的意思。