2016-09-30 2 views
0

타사 공급 업체에서 제공하는 XSD 파일이 있습니다. XSD 파일을 구문 분석하고 Java 객체를 생성해야합니다. 나는 Maven 플러그인을 통해 XSD 파일을 파싱하기 위해 JAXB를 사용하고있다.JAXB를 사용하여 XSD에서 혼합 유형 데이터 marshlling

최근에 XML에서 파싱 된 태그 중 하나의 데이터를 사용해야한다는 요구가있을 때까지 모든 것이 순조롭게 진행되었습니다. 태그의 complex typemixed = true으로 JAXB에 의해 생성 된 java 클래스는 다음과 같습니다.

XSD 복합 유형 :

<xs:complexType name="Object_Data"> 
    <xs:sequence> 
     <xs:element name="GeneralRemarks" type="GeneralRemarks" minOccurs="0" maxOccurs="1"> 
      <xs:annotation> 
       <xs:documentation>General remarks</xs:documentation> 
      </xs:annotation> 
     </xs:element> 
    </xs:sequence> 
</xs:complexType> 
<xs:complexType name="GeneralRemarks" mixed="true"> 
    <xs:sequence> 
     <xs:element name="GeneralRemark" type ="GeneralRemark" maxOccurs="unbounded"/> 
    </xs:sequence> 
</xs:complexType> 
<xs:complexType name="GeneralRemark" mixed="true"> 
    <xs:sequence> 
     <xs:element name="GeneralRemark_RemarkQualifier" type="GeneralRemark_RemarkQualifier" minOccurs="1" maxOccurs="1"/> 
     <xs:element name="GeneralRemark_RemarkText" type="xs:string" minOccurs="1" maxOccurs="1"/> 
    </xs:sequence> 
</xs:complexType> 

JAXB 클래스가 대신 목록 <GeneralRemark>있는의

import java.io.Serializable; 
import java.util.ArrayList; 
import java.util.List; 
import javax.xml.bind.JAXBElement; 
import javax.xml.bind.annotation.XmlAccessType; 
import javax.xml.bind.annotation.XmlAccessorType; 
import javax.xml.bind.annotation.XmlElementRef; 
import javax.xml.bind.annotation.XmlMixed; 
import javax.xml.bind.annotation.XmlType; 
/** 
* <p>Java class for GeneralRemarks complex type. 
* 
* <p>The following schema fragment specifies the expected content contained within this class. 
* 
* <pre> 
* &lt;complexType name="GeneralRemarks"> 
* &lt;complexContent> 
*  &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType"> 
*  &lt;sequence> 
*   &lt;element name="GeneralRemark" type="{}GeneralRemark" maxOccurs="unbounded"/> 
*  &lt;/sequence> 
*  &lt;/restriction> 
* &lt;/complexContent> 
* &lt;/complexType> 
* </pre> 
* 
* 
*/ 
@XmlAccessorType(XmlAccessType.FIELD) 
@XmlType(name = "GeneralRemarks", propOrder = { 
    "content" 
}) 
public class GeneralRemarks { 

    @XmlElementRef(name = "GeneralRemark", type = JAXBElement.class) 
    @XmlMixed 
    protected List<Serializable> content; 

    /** 
    * Gets the value of the content property. 
    * 
    * <p> 
    * This accessor method returns a reference to the live list, 
    * not a snapshot. Therefore any modification you make to the 
    * returned list will be present inside the JAXB object. 
    * This is why there is not a <CODE>set</CODE> method for the content property. 
    * 
    * <p> 
    * For example, to add a new item, do as follows: 
    * <pre> 
    * getContent().add(newItem); 
    * </pre> 
    * 
    * 
    * <p> 
    * Objects of the following type(s) are allowed in the list 
    * {@link String } 
    * {@link JAXBElement }{@code <}{@link GeneralRemark }{@code >} 
    * 
    * 
    */ 
    public List<Serializable> getContent() { 
     if (content == null) { 
      content = new ArrayList<Serializable>(); 
     } 
     return this.content; 
    } 
} 

를 생성, GeneralRemarks 클래스는 목록 < 직렬화 >을 포함하고 있습니다.

목록 <Serializable>에서 데이터를 가져 오는 방법을 많이 검색했으며 데이터를 추출하기 위해 아래 코드를 사용하는 해결책을 찾았습니다. 어떻게 든 중 하나를 복합 타입을 재정의하거나 어떻게 든 올바른 클래스가되도록 false로 혼합 속성 값을 변경하기 위해 칠레 XSD의 복합 타입 또는 (.xjc) 바인딩 파일을 대체 할 수있는 경우

GeneralRemarks remarks = xmlObject.getGeneralRemarks(); 
for(int i=0;i<remarks.getContent().size();i++){ 
    if(remarks.getContent().get(i) instanceof JAXBElement<?>){ 
    JAXBElement j = (JAXBElement)remarks.getContent().get(i); 
    org.w3c.dom.Element el = (org.w3c.dom.Element) j.getValue(); 
    System.out.println("TEXT--"+el.getTextContent()); 
    System.out.println("TAG--"+el.getTagName()); 
    System.out.println("CHILD --"+el.getElementsByTagName("GeneralRemark_RemarkQualifier").item(0).getTextContent()); 
    } 

} 

내가 알고 싶은 생성됩니다.

내 xsd에서 확장/제한을 사용하려고했습니다. 또한 사용자 지정 형식을 만들고 GeneralRemarks 형식 요소가있는 parentXML 노드를 확장하여 자식 xsd에서 내 사용자 지정 복합 형식을 사용하려고 시도했지만 모두 작동하지 않았습니다.

나는 인터넷 검색을 많이 해봤지만이 쿼리와 관련된 솔루션을 찾지 못했습니다. 대부분의 링크는 확장/제한 중 하나만 사용하도록 제안되었지만 작동하지 않았습니다.

어떻게 든 복합 유형을 대체하는 솔루션이 있는지 제안하십시오.

답변

0

jaxb2-simplify 플러그인을 사용하면 특히 simplify:as-element-property 확장 프로그램을 사용하여 설명한대로 개체를 생성 할 수 있습니다. 그러나 당신이 maven을 사용하지 않는다면, xjc 커맨드 라인의 확장으로서 플러그인을 실행하는 것이 실용적이지 않은 것처럼 보입니다. 그래서 대신 maven을 사용하려고 시도했습니다. here

관련 문제