たとえば、別のスキーマをインポートする単純なスキーマがあります。2番目のスキーマ(urn:just:attributes、just-attributes.xsd)は、属性グループを定義するだけです。
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.example.org/MySchema"
xmlns:tns="http://www.example.org/MySchema"
elementFormDefault="qualified"
xmlns:ja="urn:just:attributes">
<import schemaLocation="just-attributes.xsd" namespace="urn:just:attributes"/>
<element name="MyElement">
<complexType>
<attributeGroup ref="ja:AttributeGroup"/>
</complexType>
</element>
</schema>
このスキーマからクラスを生成するためにMetroxjcAntタスクを使用しています。私が遭遇している問題は、私が対話しているサードパーティのアプリケーションが名前空間に特有のものであるということです。この場合、文字列値が必要なので、シリアル化する必要があります。これには定型コードを使用します。
private static <T> String marshal(T object) throws JAXBException{
OutputStream outputStream = new ByteArrayOutputStream();
JAXBContext jaxbContext = JAXBContext.newInstance(object.getClass());
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.marshal(object, outputStream);
return outputStream.toString();
}
それは私にの線に沿って何かを与えます
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:MyElement xmlns:ns1="urn:just:attributes" xmlns:ns2="http://www.example.org/MySchema" ns1:attrib1="1234" ns1:attrib2="5678"/>
私が抱えている問題は、このサードパーティがのようなものを期待していることです。つまり、名前空間に付けられた名前xmlns:thirdpartyns="urn:just:attributes"
に基づいて解析しているということです。ソフトウェアが機能するには、「サードパーティ」である必要があります。
結果の文字列で検索と置換を行う以外に、これを回避する方法を知っている人はいますか?おそらくカスタムバインディングルール?