1

以前は「インポート」ではなく「インクルード」を使用していたサードパーティからxsdファイルを入手しました。これらのxsdファイルを使用して、jaxbを使用してJavaファイルを生成しています。初期のxsd構造は、同じクラスが異なるパッケージに含まれている出力をもたらしました。たとえば、「aa」と「bb」の2つのパッケージが生成された場合、両方に同じ共通ファイルが含まれていました。

aa / commonElement.java
aa / a.java

bb / commonElement.java
bb / b.java

これは避けたかったことでした。commonElement.javaを1つのパッケージで一度作成し、残りのパッケージでインポートするのではなく、代わりにインポートを使用し始めました。

<xs:schema xmlns="http://www.ns.com/aa" xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:DD="http://www.ns.com/common" targetNamespace="http://www.ns.com/aa" elementFormDefault="qualified" jaxb:version="1.0" jaxb:extensionBindingPrefixes="xjc">

 <xs:import namespace="http://www.ns.com/common"  schemaLocation="common.xsd"/>
   <xs:element name="Response">
                <xs:complexType>
                        <xs:sequence>
                                    <xs:element name="element" type="DD:commonElement" ../>

javaクラスは、私が期待したとおりに作成およびコンパイルされました。

common / commonElement.java
aa / aa.java

問題は、API呼び出しからaaの結果を受け取り、結果をアンマーシャリングすると、commonElementが正しく作成されているが、フィールドが空のaaクラスを取得することです。

私の推測では、アンマーシュラーは「common」名前空間で定義を探す必要があることを理解しておらず、代わりに「aa」名前空間でそれらを探しているため、フィールドは空ですが、正しく機能させるにはどうすればよいですか?

助けてくれてありがとう

4

2 に答える 2

0

マーシャリングが正しく行われていない理由を診断するのに十分な情報がありません。以下は機能します、あなたはそれをあなたがしていることと比較してエラーを見つけることができるかもしれません。

最も可能性の高い候補は次のとおりです。

  • JAXBContextを作成するときに、十分なクラスについてJAXBに通知していません。
  • XMLドキュメントが適切に名前空間で修飾されていません。

次のスキーマを使用します。

common.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema 
    xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    targetNamespace="http://www.ns.com/common" 
    xmlns="http://www.ns.com/common" 
    elementFormDefault="qualified">

    <xs:complexType name="commonElement">
        <xs:sequence>
            <xs:element name="commonChild" type="xs:string"/>
        </xs:sequence>
    </xs:complexType>
</xs:schema>

aa.xsd

<xs:schema xmlns="http://www.ns.com/aa" xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:DD="http://www.ns.com/common" targetNamespace="http://www.ns.com/aa"
    elementFormDefault="qualified">

    <xs:import namespace="http://www.ns.com/common"
        schemaLocation="common.xsd" />

    <xs:element name="Response">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="element" type="DD:commonElement" />
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

次のクラスが生成されました。

com.ns.aa.package-info

@javax.xml.bind.annotation.XmlSchema(namespace = "http://www.ns.com/aa", elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
package com.ns.aa;

com.ns.aa.Response

package com.ns.aa;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
import com.ns.common.CommonElement;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "element"
})
@XmlRootElement(name = "Response")
public class Response {

    @XmlElement(required = true)
    protected CommonElement element;

    public CommonElement getElement() {
        return element;
    }

    public void setElement(CommonElement value) {
        this.element = value;
    }

}

com.ns.common.package-info

@javax.xml.bind.annotation.XmlSchema(namespace = "http://www.ns.com/common", elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
package com.ns.common;

com.ns.common.CommonElement

package com.ns.common;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "commonElement", propOrder = {
    "commonChild"
})
public class CommonElement {

    @XmlElement(required = true)
    protected String commonChild;

    public String getCommonChild() {
        return commonChild;
    }

    public void setCommonChild(String value) {
        this.commonChild = value;
    }

}

これらのクラスを使用して、次のXMLドキュメントをアンマーシャリングできます。

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:Response xmlns="http://www.ns.com/common" xmlns:ns2="http://www.ns.com/aa">
    <ns2:element>
        <commonChild>FOO</commonChild>
    </ns2:element>
</ns2:Response>

次のコードを使用します。

デモ

import java.io.File;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

import com.ns.aa.Response;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Response.class);

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        File xml = new File("input.xml");
        Response response = (Response) unmarshaller.unmarshal(xml);

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(response, System.out);
    }
}
于 2010-09-01T20:52:30.193 に答える
0

知っている場合は1つ質問がありますので、説明してください。私は上記の答えとそれが与えられた詳細でうまく機能することを試みました。

しかし、私がxml検証を下回りたいと仮定しますそれはあなたが与えたのと同じです、しかし私は私が検証したいいくつかの小さな変更をしました

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Response xmlns="http://www.ns.com/aa">
    <element xmlns="http://www.ns.com/common">
        <commonChild>FOO</commonChild>
    </element>
</Response>

しかし、それは以下のエラーを与えます

SAX Exception: cvc-complex-type.2.4.a: Invalid content was found starting with element 'element'. One of '{"http://www.ns.com/aa":element}' is expected.
 is not valid against 
于 2020-03-26T08:26:12.197 に答える