我必须从我无法控制的外部系统中解组并处理XML。 XML是我认为是俄罗斯娃娃格式,它不容易处理。适用于常见嵌套类型的JAXB绑定
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有一个idential结构,只是一个不同的名称。
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在宣布nstypes.xsd。它实际上是一个BASETYPE,它是一个String。
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>
的XML用于CLASSA和CLASSB类似于
ClassA.xml
<?xml version="1.0" encoding="UTF-8"?>
<CLASSA>
<Prop>
<PROP>
<Value>AAA</Value>
</PROP>
</Prop>
</CLASSA>
ClassB.xml
<?xml version="1.0" encoding="UTF-8"?>
<CLASSB>
<Prop>
<PROP>
<Value>BBB</Value>
</PROP>
</Prop>
</CLASSB>
一旦JAXB类已经产生,我想写出像这样的代码:
testJAXB
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
{
}
谢谢回答什么,我想试试,并报告! – DUFF
嗨,我已经看过这个,我认为这是正确的做法。但它可能行不通。问题是你只能改变隐藏的实现。 getters和setter必须返回CLASSA或CLASSB包中的东西,所以它们仍然不是一个普通的java类(我认为)。我已经在此问题上发布了另一个问题[here](http://stackoverflow.com/questions/12655797/jaxb-bindings-customising-can-you-change-getter-setter-of-xjc-generated-class-t ) – DUFF