JAXB 2.2没有解组无界选择元素

时间:2012-05-09 16:41:21

标签: java jaxb xml-parsing musicxml

我正在更新使用JAXB 1.0和内部编写的MusicXML 1.0模式的Java应用程序,以使用JAXB 2.2和供应商提供的MusicXML 3.0模式。当我解组一个包含多个< score-part>的文件时< part-list>的子元素仅使用JAXB 2.2 1< score-part>元素被返回。  我目前正在使用DOM的变通方法来获取addidiontal< score-parts>但这不是很理想,因为我正在解析文件两次以获取数据。如何让JAXB返回所有<得分>在< part-list>?

适用于< part-list>的MusicXML 1.0架构代码段

<xs:element name="part-list">
<xs:complexType>
  <xs:sequence>
    <xs:element minOccurs="0" maxOccurs="unbounded" ref="part-group"/>
    <xs:element ref="score-part"/>
    <xs:choice minOccurs="0" maxOccurs="unbounded">
      <xs:element ref="part-group"/>
      <xs:element ref="score-part"/>
    </xs:choice>
  </xs:sequence>
</xs:complexType>
</xs:element>

适用于&lt; part-list&gt;

的MusicXML 3.0架构代码段
<xs:complexType name="part-list">       
<xs:sequence>
    <xs:group ref="part-group" minOccurs="0" maxOccurs="unbounded"/>
    <xs:group ref="score-part"/>
    <xs:choice minOccurs="0" maxOccurs="unbounded">
        <xs:group ref="part-group"/>
        <xs:group ref="score-part"/>
    </xs:choice>
</xs:sequence>
</xs:complexType>

用于解组的Java代码段

package test;
import java.io.FileInputStream;
import java.util.*;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
import recodare.MusicXML.*;
import javax.xml.bind.JAXBElement;


public class MusicXMLReader{

    private java.lang.String m_sXMLFielPath = "";
    private ScorePartwise m_spScorePartwise = null;
    public MusicXMLReader(java.lang.String filePath){
        m_sXMLFilePath = filePath;
    }
    public void processXML() throws Exception{

        JAXBContext jc = JAXBContext.newInstance("recodare.MusicXML");
        Unmarshaller u = jc.createUnmarshaller();
        m_spScorePartwise = (ScorePartwise)u.unmarshal(new FileInputStream(m_sXMLFilePath));

        PartList pl = m_spScorePartwise.getPartList();
        ScorePart scorePart = pl.getScorePart(); // 1st score-part
        // for remaining score-part or part-group this list is null even when part-list  
        // has multiple score-part elements
        List<Object> listPartGroupOrScorePart = pl.getPartGroupOrScorePart(); 
        //continue processing file

    }
}

对于测试数据,我正在使用来自The Unofficial MusicXML Test Suite的文件41a-MultiParts-Partorder.xml和41b-MultiParts-MoreThan10.xml

如果您希望为MusicXML 3.0构建JAXB绑定,您可能会从JAXB获得以下(或类似)错误

[ERROR] Property "Segno" is already defined. Use &lt;jaxb:property> to resolve this conflict.
line 2793 of file: musicxml.xsd

[ERROR] The following location is relevant to the above error
line 2800 file: musicXML.xsd
[ERROR] Property "Coda" is already defined. Use &lt;jaxb:property> to resolve this conflict.
line 2794 of musicXML.xsd

[ERROR] The following location is relevant to the above error
line 2801 of file: musicXML.xsd
[ERROR] Element "link" shows up in more than one properties.
line 4765 of file: musicXML.xsd
[ERROR] Element "bookmark" shows up in more than one properties.
line 4766 of file: musicXML.xsd

在这种情况下,请按照here

列出的步骤进行操作

0 个答案:

没有答案