2013-06-04 7 views
5

大きなXMLを部分的にアンマーシャリングする必要があります。 XJCで生成JAXBで部分的にアンマーシャリングする

<Records> 
    <Contract> 
     ... 
    </Contract> 
    <Contract> 
     ... 
    </Contract> 
    ... 
    <Contract> 
     ... 
    </Contract> 
    <Contract> 
     ... 
    </Contract> 
</Records> 

、その結果クラス:

XMLの構造は次の通りである

- Records 
    |- Contract 

私はthese(JAXB里からのサンプル)に従うならば、私が取得エラー:

Exception in thread "main" javax.xml.bind.UnmarshalException: unexpected element (uri:"http://somedomain.com", local:"Contract"). Expected elements are <{http://somedomain.com}Records> 

私が使用している場合:

<jaxb:globalBindings localScoping="toplevel"/> 

は、私はエラーを取得:

org.xml.sax.SAXParseException: A class/interface with the same name "com.my.package.Text" is already in use. Use a class customization to resolve this conflict. 

をしかし、私は多くのクラスを変更する必要があります。これは解決策ではありません。

答えて

7
/** 
* User: r.ibragimov 
* Date: 04.06.13 
*/ 
public class PartialJAXB1 { 

    public static void main(String[] args) throws JAXBException, XMLStreamException, FileNotFoundException { 

     final QName qName = new QName("http://www.domain.com","Contract"); 

     InputStream inputStream = new FileInputStream(new File("c:\\test.xml")); 

     // create xml event reader for input stream 
     XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance(); 
     XMLEventReader xmlEventReader = xmlInputFactory.createXMLEventReader(inputStream); 

     // initialize jaxb 
     JAXBContext context = JAXBContext.newInstance(Records.class); 
     Unmarshaller unmarshaller = context.createUnmarshaller(); 

     XMLEvent e = null; 

     // loop though the xml stream 
     while((e = xmlEventReader.peek()) != null) { 

      // check the event is a Document start element 
      if(e.isStartElement() && ((StartElement)e).getName().equals(qName)) { 

       // unmarshall the document 
       Records.Contract contract = unmarshaller.unmarshal(xmlEventReader, Records.Contract.class).getValue(); 
       System.out.println(contract); 
      } else { 
       xmlEventReader.next(); 
      } 

     } 

    } 
} 
+0

ありがとう、それは完璧に動作します! – Margaret

2

生成トップレベルクラス

I want get just Records class and separate Contract class.

デフォルトではJAXBの実装では、静的な内部クラスとして匿名の複合型に対応するクラスを生成します。あなたはすべてがあなたがあなたの質問には、次の外部結合のカスタマイズを使うに記載できるよう、トップレベルのクラスになりたい場合は、次の

<jaxb:bindings 
    xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
    xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" 
    version="2.1"> 
    <jaxb:globalBindings localScoping="toplevel"/> 
</jaxb:bindings> 

名前解決の競合

I get error:

org.xml.sax.SAXParseException: A class/interface with the same name "com.my.package.Text 

静的内部クラスの目的の1つは、名前の競合を防ぐことです。すべての最上位クラスでは、外部バインディングファイルを使用して、複合型から生成されたクラスの名前を変更できます。以下は、複合型itemTypeに対応するクラスがItemとして生成される例です。

あなたは詳細情報

について-bフラグ

xjc -b binding.xml your-schema.xsd 

を使用してXJCコールでバインディングファイルを指定するバインディング・ファイルを使用して

<jaxb:bindings 
    xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
    xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" 
    version="2.1"> 
    <jaxb:globalBindings localScoping="toplevel"/> 
    <jaxb:bindings schemaLocation="company.xsd"> 
     <jaxb:bindings node="//xsd:element[@name='employee']/xsd:complexType/xsd:sequence/xsd:element[@name='address']/xsd:complexType"> 
      <jaxb:class name="EmployeeAddress"/> 
     </jaxb:bindings> 
    </jaxb:bindings> 
</jaxb:bindings> 

+0

あなたは既に別のクラスを作成する方法については、この質問、私の他の質問:) をお願いします。 – IRus

+0

または、次のような構造を部分的にアンマーシャリングする方法についてです。 – IRus

+0

@irus - 私の現在の回答は、あなたが見ている '同じ名前 'のエラーに役立ちます。私はあなたの質問の部分的な非マーシャル部分を理解していません。 –

関連する問題