8

JAXBに問題があります

<element name="create">
    <complexType>
        <sequence>
            <element name="name" type="string"></element>
        </sequence>
    </complexType>
</element>

私のxml:

<Create>
<name> coco </name>
</Create>

私のコードJava:

JAXBContext context = JAXBContext.newInstance("MyPackage");
 Unmarshaller decodeur =    context.createUnmarshaller();
System.out.println("text : " + message);
msgObject = decodeur.unmarshal(sr);  
     if (msgObject instanceof Create)

{
      System.out.println(" action");
}

そして、私はこれを持っています:

予期しない要素 (uri:""、local:"Create")。期待される要素は <{ http://www.example.org/XSD_Maths }create>です

そして、私のコードはこれで止まりました:

 msgObject = decodeur.unmarshal(sr);  

私のxmlは良いですか?、何が問題なのかわからないので、助けてもらえますか

4

1 に答える 1

18

XML スキーマにはschema、次のようなタグが含まれている可能性があります。

<?xml version="1.0" encoding="UTF-8"?>
<schema 
    xmlns="http://www.w3.org/2001/XMLSchema" 
    targetNamespace="http://www.example.org/XSD_Maths" 
    xmlns:tns="http://www.example.org/XSD_Maths" 
    elementFormDefault="qualified">

targetNamespaceのを指定しているためhttp://www.example.org/XSD_Maths。XML は次のようにする必要があります。

<create xmlns="http://www.example.org/XSD_Maths">
    <name> coco </name>
</create>

DOM からのアンマーシャリングに関する注意

DOM からアンマーシャリングする場合、DocumentまたはElement使用した DOM パーサーが名前空間を認識していることを確認する場合。これは、 に次のフラグを設定することによって行われますDocumentBuilderFactory

documentBuilderFactory.setNamespaceAware(true);

詳細については

以下は、JAXB と名前空間について詳しく説明している私のブログの記事へのリンクです。

于 2014-04-21T20:24:07.573 に答える