自分で制御できない外部システムからXMLをアンマーシャリングして処理する必要があります。XMLは、処理を容易にしないロシアの人形形式であると私が信じているものです。
XMLは、共通のXSDファイルで宣言されている共通の型を共有します。ただし、JABXがこれらのタイプのJAVAクラスを生成する場合、各外部クラスには共通タイプのネストされた宣言が含まれるため、JAVAに関する限り異なるタイプになります。
XML内の一般的なネストされたタイプを処理するための一般的なJAVA関数が欲しいのですが、これはJAXBクラスのように機能しません。これらの一般的なタイプは無関係です。
これにより、これらの一般的なタイプを処理する必要のあるJAVAコードが非常に乱雑になり、JAXBバインディングを使用して問題を修正しようとしています。しかし、私は成功していません。だから私の質問は: XML / XSDのこのフォーマットを考えると、一般的なタイプのJAVAコードはバインディングまたは他の方法で生成できますか?
だから例;
XMLCLASSAとCLASSBには2つのクラスがあります。どちらにも、文字列であるtestTYPE型の値が1つある複合型が含まれています。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は類似しています
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()の実装を1つ持つことです。これにはorg.generated.TestTYPEが必要であり、すべてのJAXBクラスはorg.generated.CLASSA.TestTYPEsよりもorg.generated.TestTYPEsratehrで生成されると思います。私はそれがいくつかのバインディングマジックで達成できることを望んでいます。誰かが私を正しい方向に向けることができれば、それは大いにありがたいです。
私はJAVAのバックグラウンドではなくC++を使用しているので、用語の一部が正しくない場合はお詫びします。
ジェローム、
それは私が見たいものですが、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
2つのTestTYPEは異なるクラスにネストされているため、それらは異なるタイプです。
基本的に、xjcを実行すると、TestTYPEクラスを含む2つのファイルが取得されます。
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
{
}