JAXB Unmarshalling

unknown

Bekanntes Mitglied
Hallo zusammen

Beim Thema Jaxb Unmarschalling habe ich probiert das Beispiel hier zu erweitern.

In diesem Beispiel bekommt man ja nur ein einziges Question-Object. Ich möchte allerdings Eine Liste von Questions erhalten.
Dementsprechend habe ich das XML erweitert, eine neue Klasse (RootClass) geschrieben und die Klassen XmlToObject und Question angepasst:

Java:
public class Question {
	private int id;
	private String questionname;
	private List<Answer> answers;

	@XmlAttribute
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	@XmlElement
	public String getQuestionname() {
		return questionname;
	}
	public void setQuestionname(String questionname) {
		this.questionname = questionname;
	}
	@XmlElement
	public List<Answer> getAnswers() {
		return answers;
	}
	public void setAnswers(List<Answer> answers) {
		this.answers = answers;
	}
}
Java:
@XmlRootElement
public class RootClass {	
              private List<Question> questions;
               
       @XmlElement
	public List<Question> getQuestions() {
		return questions;
	}

	public void setQuestions(List<Question> questions) {
		this.questions = questions;
	}
}
Java:
public class XmlToObject {
	public static void main(String[] args) {
		try {
			File file = new File("C:\\Workspace\\Jaxb\\src\\question.xml");
			JAXBContext jaxbContext = JAXBContext.newInstance(RootClass.class);

			Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
			RootClass rt= (RootClass) jaxbUnmarshaller.unmarshal(file);


		} catch (JAXBException e) {
			e.printStackTrace();
		}

	}
}
Mein XML
Java:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

<RootClass>
	<question id="1">
		<answers>
			<answername>java is a programming language</answername>
			<id>101</id>
			<postedby>ravi</postedby>
		</answers>
		<answers>
			<answername>java is a platform</answername>
			<id>102</id>
			<postedby>john</postedby>
		</answers>
		<questionname>What is java?</questionname>
	</question>
		<question id="2">
		<answers>
			<answername>java is a programming language2</answername>
			<id>102</id>
			<postedby>ravi2/postedby>
		</answers>
		<answers>
			<answername>java is a platform2</answername>
			<id>103</id>
			<postedby>john2</postedby>
		</answers>
		<questionname>What is java?2</questionname>
	</question>
</RootClass>

Die Exception:
Java:
javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"RootClass"). Expected elements are <{}rootClass>
	at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(Unknown Source)
	at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportError(Unknown Source)
	at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportError(Unknown Source)
	at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportUnexpectedChildElement(Unknown Source)
	at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext$DefaultRootLoader.childElement(Unknown Source)
	at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext._startElement(Unknown Source)
	at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.startElement(Unknown Source)
	at com.sun.xml.internal.bind.v2.runtime.unmarshaller.SAXConnector.startElement(Unknown Source)
	at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.startElement(Unknown Source)
	at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(Unknown Source)
	at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl$NSContentDriver.scanRootElementHook(Unknown Source)
	at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(Unknown Source)
	at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(Unknown Source)
	at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(Unknown Source)
	at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(Unknown Source)
	at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
	at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
	at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
	at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source)
	at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(Unknown Source)
	at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
	at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(Unknown Source)
	at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(Unknown Source)
	at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(Unknown Source)
	at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(Unknown Source)
	at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(Unknown Source)
	at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(Unknown Source)
	at XmlToObject.main(XmlToObject.java:18)

Weiss gerade jemand, was ich korriegieren soll, damit das Programm richtig läuft?
Generell würde mich interessieren, wie man solche Verschachtelungen von Objekten am besten lösen kann.

Noch eine zusätzliche Frage hätte ich noch: Wieso hat man bei den Attributen der Klasse Answer auf die Annotation "@XmlElement" verzichtet und es funktioniert immer noch?
 
Zuletzt bearbeitet:

knilch

Bekanntes Mitglied
Hi,
zuest mal ein Hinweis. In deinem xml ist ein Fehler, das sollte eigentlich eigentlich zuerst behoben werden.
... In Zeile 22: <postedby>ravi2/postedby> sollte aber sein: <postedby>ravi2/<postedby>
... Elementnamen in einem xml fangen immer klein an, also rootClass, nicht RootClass
... Da du mehrere question-Elemente in deinem xml haben möchtest, musst du das so machen:
Beispiel: root.xml
Code:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<rootClass id="111">
    <questions id="1">
        <answers>
            <answername>java is a programming language</answername>
            <id>101</id>
            <postedby>ravi</postedby>
        </answers>
        <answers>
            <answername>java is a platform</answername>
            <id>102</id>
            <postedby>john</postedby>
        </answers>
        <questionname>What is java?</questionname>
    </questions>
    <questions id="2">
        <answers>
            <answername>java is a programming language2</answername>
            <id>102</id>
            <postedby>ravi2</postedby>
        </answers>
        <answers>
            <answername>java is a platform2</answername>
            <id>103</id>
            <postedby>john2</postedby>
        </answers>
        <questionname>What is java 2?</questionname>
    </questions>
</rootClass>
Mit dieser Methode kannst du das dann testen:
Java:
private static void test() {
    try {
        File file = new File("src/root.xml");  
        JAXBContext jaxbContext = JAXBContext.newInstance(RootClass.class);  
        Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();  
        RootClass root = (RootClass) jaxbUnmarshaller.unmarshal(file);    
        List<Question1> questions = root.getQuestions();    
        if(questions == null) {
            System.err.println("no Questions available");
            return;
        }        
        System.out.println("Questions:");  
        for(Question1 question : questions)  {
            System.out.println(question.getId() + ": " + question.getQuestionname());
            for (Answer ans : question.getAnswers()) {
                System.out.println(ans.getId()+" "+ans.getAnswername()+"  "+ans.getPostedby());  
            }
        }
    } catch (JAXBException e) {  
        e.printStackTrace();  
    }
}
 
Zuletzt bearbeitet:

Ähnliche Java Themen

Neue Themen


Oben