2

次の XML を JAXB で非整列化しようとしています。

<artist xmlns="http://www.spotify.com/ns/music/1">
   <name>Basement Jaxx</name>
   <albums>
      <album href="spotify:album:3xOulZzGRe4Ycwm59iCdKg">
         <name>Back 2 the Wild</name>
         <artist href="spotify:artist:4YrKBkKSVeqDamzBPWVnSJ">
            <name>Basement Jaxx</name>
         </artist>          
         <released>2013</released>
         <id type="upc">5055489272702</id>          
      <album>
   </albums>
</artist>

@XmlTransientアルバム内のアーティストをスキップするために使用すると、関係なく解析されます。

@XmlTransient正しくマークされたフィールドを JAXB で処理するにはどうすればよいですか?

編集 1 - ファイル

テスト XML:

<artist>
     <name>Adema</name>
     <albums>
          <album href="spotify:album:07tjJowJIddz8c74x5WOvj">
               <name>Topple the Giants</name>
               <artist href="spotify:artist:3n4ersmDo55xV4fPSCKpXb">
                    <name>Adema</name>
               </artist>
               <released>2013</released>
               <id type="upc">886443922082</id>
               <availability>
                    <territories>AD AT AU BE CA CH DE DK EE ES FI FR GB HK IE IS IT LI LT LU LV MC MX MY NL NO NZ PL PT SE SG US</territories>
               </availability>
          </album>
          <album href="spotify:album:06QaJLqG068uXHQbAcPZKU">
               <name>Kill The Headlights</name>
               <artist href="spotify:artist:3n4ersmDo55xV4fPSCKpXb">
                    <name>Adema</name>
               </artist>
               <released>2007</released>
               <id type="upc">4030816195283</id>
               <availability>
                    <territories>AD AT BE CH DE DK EE ES FI FR GB IE IS IT LI LT LU LV MC NL NO PL PT SE</territories>
               </availability>
          </album>
    </albums>
</artist>

XTest.java:

package dao.spotify;

import dao.spotify.lookup.entities.LookupArtist;
import java.io.File;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.helpers.DefaultValidationEventHandler;

public class XTest {

    public XTest() {
        load();
    }

    public void load() {
        try { 
            File file = new File("c:/tmp/test5.xml");
            JAXBContext jc = JAXBContext.newInstance(LookupArtist.class);
            Unmarshaller um = jc.createUnmarshaller();
            um.setEventHandler(new DefaultValidationEventHandler());
            LookupArtist spotifyArtistWrapper = (LookupArtist) um.unmarshal(file);
            System.err.println("Done");
        } catch (JAXBException ex) {
            ex.printStackTrace();
        }
    }

    public static void main(String... args) {
        XTest test = new XTest();
    }
}

LookupArtist.java:

package dao.spotify.lookup.entities;

import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "artist")
@XmlAccessorType(XmlAccessType.FIELD)
public class LookupArtist {

    @XmlElement
    private String name;
    @XmlElementWrapper(name = "albums")
    @XmlElement(name = "album")
    private List<LookupInnerAlbum> albums = new ArrayList();

}

LookupInnerAlbum.java:

package dao.spotify.lookup.entities;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;

@XmlRootElement(name = "album")
@XmlAccessorType(XmlAccessType.FIELD)
public class LookupInnerAlbum {

    @XmlAttribute
    private String href;
    private String name;
    private int released;
    private String id;
    private LookupInnerAvailability availability;
    @XmlTransient
    private LookupInnerArtist artist;

}

LookupInnerAvailability.java:

package dao.spotify.lookup.entities;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "availability")
@XmlAccessorType(XmlAccessType.FIELD)
public class LookupInnerAvailability {

    @XmlElement
    private String territories;

}

LookupInnerArtist.java:

package dao.spotify.lookup.entities;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;

@XmlType(name = "artist")
@XmlAccessorType(XmlAccessType.FIELD)
public class LookupInnerArtist {

    @XmlElement
    private String name;
    @XmlAttribute
    private String href;
}

これを実行すると、次のようになります。

DefaultValidationEventHandler: [ERROR]: unexpected element (uri:"", local:"artist"). Expected elements are <{}id>,<{}released>,<{}name>,<{}availability> 
javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"artist"). Expected elements are <{}id>,<{}released>,<{}name>,<{}availability>
     Location: line 6 of file:/c:/tmp/test5.xml
    at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(UnmarshallingContext.java:662)
    at com.sun.xml.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:258)
    at com.sun.xml.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:253)
    at com.sun.xml.bind.v2.runtime.unmarshaller.Loader.reportUnexpectedChildElement(Loader.java:120)
    at com.sun.xml.bind.v2.runtime.unmarshaller.Loader.childElement(Loader.java:105)
    at com.sun.xml.bind.v2.runtime.unmarshaller.StructureLoader.childElement(StructureLoader.java:262)
    at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext._startElement(UnmarshallingContext.java:498)
    at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext.startElement(UnmarshallingContext.java:480)
    at com.sun.xml.bind.v2.runtime.unmarshaller.SAXConnector.startElement(SAXConnector.java:150)
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.startElement(AbstractSAXParser.java:506)
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(XMLNSDocumentScannerImpl.java:376)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2717)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:607)
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:116)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:489)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:835)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:764)
    at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:123)
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1210)
    at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:568)
    at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:258)
    at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:229)
    at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:136)
    at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:141)
    at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:150)
    at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:168)
    at dao.spotify.XTest.load(XTest.java:30)
    at dao.spotify.XTest.<init>(XTest.java:21)
    at dao.spotify.XTest.main(XTest.java:38)
4

1 に答える 1