JAXB - 如何在没有包含没有包装器的列表的XSD的情况下解组XML

时间:2015-02-20 12:20:59

标签: java xml jaxb

我有一个复杂的XML文件,我必须解析,但我没有任何XSD。 XML看起来像这样(简化):

<?xml version="1.0" encoding="UTF-8"?>
<mets:mets ... namespace declaration ommited ... >
      <mets:metsHdr>
          complex mets:metsHdr
      </mets:metsHdr>


      <mets:dmdSec>
          complex mets:dmdSec 1
      </mets:dmdSec>
        ....
      <mets:dmdSec>
          complex mets:dmdSec n
      </mets:dmdSec>


      <mets:fileSec>
          complex mets:fileSec 1
      <mets:fileSec>
        ....
      <mets:fileSec>
          complex mets:fileSec n
      <mets:fileSec>
</mets:mets>

有一个头元素,没有包装的dmdSec列表,也没有包装的fileSec列表。我试着先阅读mets:dmdSec list,使用这些类:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "mets")
public class Mets {
    @XmlAttribute(name="TYPE")
    private String type;

    @XmlList
    private List<DmdSec> dmdSec;

}


@XmlType
public class DmdSec {

    @XmlAttribute(name="ID")
    private String id;

}

但我得到了这个例外: Exception in thread "main" javax.xml.bind.DataBindingException: com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExceptions www.loc.gov.METS.DmdSec maps to a structured XML fragment (AKA complex type), and therefore incompatible with @XmlList. this problem is related to the following location: at private java.util.List www.loc.gov.METS.Mets.dmdSec at www.loc.gov.METS.Mets

at javax.xml.bind.JAXB.unmarshal(Unknown Source)

at javax.xml.bind.JAXB.unmarshal(Unknown Source)

1 个答案:

答案 0 :(得分:1)

只是

@XmlElement(name="dmdSec")
private List<DmdSec> dmdSec;

应该做的工作,你的类也不匹配xml,dmdSec没有id ......

您还可以使用:http://www.freeformatter.com/xsd-generator.html#ad-output生成架构,并从中生成类或至少从中寻找灵感。