我必须从我无法控制的外部系统中解组和处理 XML。XML 是我认为的俄罗斯娃娃格式,处理起来并不容易。
XML 共享在通用 XSD 文件中声明的通用类型。但是,当 JABX 为这些类型生成 JAVA 类时,每个外部类都包含通用类型的嵌套声明,这使得它们就 JAVA 而言是不同的类型。
我希望有通用的 JAVA 函数来处理 XML 中的通用嵌套类型,但这不起作用,因为在 JAXB 类中这些通用类型是不相关的。
这使得我需要处理这些常见类型的 JAVA 代码非常混乱,我一直在尝试使用 JAXB 绑定来解决这个问题。但我一直没有成功。所以我的问题是: 鉴于这种 XML/XSD 格式,是否可以通过绑定或其他方法生成常见类型的 JAVA 代码?
举个例子;
XML CLASSA 和 CLASSB 中有两个类。两者都包含一个复杂类型,其类型为 testTYPE,它是一个字符串。XSD 是:
CLASSA.XSD
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:nstype="http://www.test.com/nstypes"
elementFormDefault="qualified">
<xs:import namespace="http://www.test.com/nstypes" schemaLocation="nstypes.xsd"/>
<xs:element name="CLASSA">
<xs:complexType>
<xs:sequence>
<xs:element name="Prop" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element name="PROP" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element name="Value" type="nstype:testTYPE" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
B 类具有相同的结构,只是名称不同。
CLASSB.XSD
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:nstype="http://www.test.com/nstypes"
elementFormDefault="qualified">
<xs:import namespace="http://www.test.com/nstypes" schemaLocation="nstypes.xsd"/>
<xs:element name="CLASSB">
<xs:complexType>
<xs:sequence>
<xs:element name="Prop" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element name="PROP" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element name="Value" type="nstype:testTYPE" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
nstype:testTYPE 类型的唯一称为 Value 的元素在 nstypes.xsd 中声明。它实际上是一个 BASETYPE,它是一个字符串。
nstypes.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:nstypes="http://www.test.com/nstypes"
targetNamespace="http://www.test.com/nstypes"
elementFormDefault="unqualified">
<xs:complexType name="BASETYPE">
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="TYPE" fixed="BASETYPE"/>
<xs:attribute name="derived" use="optional"/>
<xs:attribute name="readonly" use="optional"/>
<xs:attribute name="required" use="optional"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
<xs:complexType name="testTYPE">
<xs:simpleContent>
<xs:extension base="nstypes:BASETYPE"/>
</xs:simpleContent>
</xs:complexType>
</xs:schema>
CLASSA 和 CLASSB 的 XML 相似
类A.xml
<?xml version="1.0" encoding="UTF-8"?>
<CLASSA>
<Prop>
<PROP>
<Value>AAA</Value>
</PROP>
</Prop>
</CLASSA>
类B.xml
<?xml version="1.0" encoding="UTF-8"?>
<CLASSB>
<Prop>
<PROP>
<Value>BBB</Value>
</PROP>
</Prop>
</CLASSB>
生成 JAXB 类后,我想编写如下代码:
测试JAXB
import java.io.InputStream;
import org.generated.CLASSA.CLASSA;
import org.generated.CLASSA.TestTYPE;
import org.generated.CLASSB.CLASSB;
public class testJAXB
{
// there is a util function public static Object loadXML(String xmlFile, String fileName) throws Exception;
public static void main(String[] args) throws Exception
{
// create a ClassA & a ClassB
CLASSA anAClass = (CLASSA) loadXML("C:\\input\\ClassA.xml", "CLASSA");
CLASSB anBClass = (CLASSB) loadXML("C:\\input\\ClassB.xml", "CLASSB");
static void printClass(TestTYPE v)
{
// as CLASSA.TestTYPE is imported so v is a CLASSA.TestTYPE
System.out.println(v.toString());
}
// this call will work as there is a printClass which takes a CLASSA.TestTYPE
printClass(anAClass.getProp().getPROP().getValue());
// this call will not compile becase there is no printClass which takes a CLASSA.TestTYPE
printClass(anBClass.getProp().getPROP().getValue());
System.out.println("complete.");
}
}
我实际上想要做的是函数 printClass() 的一种实现。我想这将需要一个 org.generated.TestTYPE 并且所有 JAXB 类都将使用 org.generated.TestTYPEs ratehr 而不是 org.generated.CLASSA.TestTYPEs 生成。我希望这可以通过一些绑定魔法来实现。如果有人能指出我正确的方向,那将不胜感激。
我有 C++ 而不是 JAVA 背景,如果我的某些术语不正确,我深表歉意。
杰罗姆_
这就是我想看到的,但从 xjc 我在 CLASSA 中看到
public CLASSA.Prop getProp()
其中 CLASSA.Prop 是静态类,其中包含
protected CLASSA.Prop.PROP
这是另一个静态类,其中包含
protected TestTYPE value;
和 TestTYPE 是
org.generated.CLASSA.TestTYPE
CLASSB 中的 TestTYPE 是
org.generated.CLASSB.TestTYPE
由于两个 TestTYPE 嵌套在不同的类中,它们是不同的类型。
本质上,在运行 xjc 时,我得到两个包含 TestTYPE 类的文件:
CLASSA/TestTYPE.JAVA
//
// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, vJAXB 2.1.10 in JDK 6
// See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a>
// Any modifications to this file will be lost upon recompilation of the source schema.
// Generated on: 2012.09.07 at 07:45:23 AM BST
//
package org.generated.CLASSA;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlType;
/**
* <p>Java class for testTYPE complex type.
*
* <p>The following schema fragment specifies the expected content contained within this class.
*
* <pre>
* <complexType name="testTYPE">
* <simpleContent>
* <extension base="<http://www.test.com/nstypes>BASETYPE">
* </extension>
* </simpleContent>
* </complexType>
* </pre>
*
*
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "testTYPE")
public class TestTYPE
extends BASETYPE
{
}
and
CLASSB/TestTYPE.JAVA
//
// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, vJAXB 2.1.10 in JDK 6
// See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a>
// Any modifications to this file will be lost upon recompilation of the source schema.
// Generated on: 2012.09.07 at 07:45:23 AM BST
//
package org.generated.CLASSB;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlType;
/**
* <p>Java class for testTYPE complex type.
*
* <p>The following schema fragment specifies the expected content contained within this class.
*
* <pre>
* <complexType name="testTYPE">
* <simpleContent>
* <extension base="<http://www.test.com/nstypes>BASETYPE">
* </extension>
* </simpleContent>
* </complexType>
* </pre>
*
*
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "testTYPE")
public class TestTYPE
extends BASETYPE
{
}