I have searched all the questions related to this topic but i didnt find an answer to this. I am trying to convert an xml doc to java object. After searching for options to do the same, I found jaxb as the best option to do the same. I referred many articles and came up with this solution which is not working. Can somebody help me in this.
XML Dod:
<Vectors>
<Vector>
<Id>1</Id>
<Name>abc</Name>
<Desc>abcdefg</Desc>
<Type>body</Type>
<Snippet>
<![CDATA[<total>1234</total>]]>
</Snippet>
</Vector>
<Vector>
<Id>2</Id>
<Name>def</Name>
<Desc>abcdefghigj</Desc>
<Type>body</Type>
<Snippet>
<![CDATA[<total>12345</total>]]>
</Snippet>
</Vector>
</Vectors>
I want to create 2 java objects of class Vector. Class Vector looks as given below
package com.abc.TryXmltoJava;
import java.util.List;
import javax.xml.bind.annotation.*;
public class Vector {
int Id,
String Name;
String Desc;
String Type;
@XmlElement
public String getName() {
return Name;
}
public void setName(String name) {
this.Name = name;
}
@XmlElement
public String getDesc() {
return Desc;
}
public void setDesc(String desc) {
this.Desc = desc;
}
@XmlElement
public String getType() {
return Type;
}
public void setType(String type) {
this.Type = type;
}
}
Now i have created Vectors class which keeps track of the two objects and creates a list of Vector objects using list utility..
package com.abc.TryXMLtoObject;
import javax.xml.bind.annotation.*;
@XmlRootElement(name="Vectors")
public class Vectors {
@XmlElement(name="Vector")
Vector vector;
public Vector getVector() {
return vector;
}
public void setVector(Vector vector1) {
this.vector = vector1;
}
}
I will try to unmarshall the xml object using these two classes which create a list of java objects. Unmarshalling class is as given below..
package com.abc.TryXMLtoObject;
import java.io.File;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
public class JAXBExampleUnmarshal {
public static void main(String[] args) {
try {
File file = new File("C:\\file.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(Vectors.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
Vectors vectors = (Vectors) jaxbUnmarshaller.unmarshal(file);
System.out.println(vectors);
} catch (JAXBException e) {
e.printStackTrace();
}
}
}
After running this code.. i get the following errors:
javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"Vectors"). Expected elements are (none)
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 com.abc.TryXMLtoObject.JAXBExampleUnmarshal.main(JAXBExampleUnmarshal.java:17)