2013-06-04 3 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

생성 최상위 클래스 JAXB 프로 구현이 정적 내부 클래스 익명 복합 유형에 해당하는 클래스를 생성합니다 기본적으로

I want get just Records class and separate Contract class.

. 당신은 모든 당신이 당신의 질문은 다음과 같은 외부 바인딩 사용자 정의를 사용에 명시된 수있는 최상위 클래스로 원하는 경우 :

<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 

정적 내부 클래스의 목적 중 하나는 이름 충돌을 방지하는 것입니다. 모든 최상위 클래스를 사용하면 외부 바인딩 파일을 사용하여 복합 유형에서 생성 된 클래스의 이름을 바꿀 수 있습니다. 다음은 복합 유형 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 - 현재 답변이 나와있는 '같은 이름'오류와 관련하여 도움이됩니다. 귀하의 질문에 부분적으로 언 마샬 된 부분을 이해하지 못합니다. –

관련 문제