Reputation: 157
I'm unable to figure the below unmarshalling error that I'm receiving. Please help. I have reviewed passed posts and I don't know whats going on.
unexpected element (uri:"http://schemas.xmlsoap.org/soap/envelope/", local:"Envelope")
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tt="http://sample.examples">
<soapenv:Header/>
<soapenv:Body>
<tt:VersionResponse>
<tt:SchemaVersionDeclared>1.0.0.1</tt:SchemaVersionDeclared>
</tt:VersionResponse>
</soapenv:Body>
</soapenv:Envelope>
static public String getVersionFromWSResponseFromJAXB(XMLStreamReader xsr) {
String versionDataAsXML = badData;
try {
JAXBContext jaxbContext = JAXBContext.newInstance(SchemaVersion.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
CommonMovementRequirementSchemaVersion schemaVersionDeclared = (SchemaVersion) jaxbUnmarshaller.unmarshal(xsr);
// Check for empty result
if (!badData.equalsIgnoreCase(schemaVersionDeclared.getSchemaVersionDeclared())) {
versionDataAsXML = schemaVersionDeclared.getSchemaVersionDeclared();
}
} catch (Exception e) {
e.printStackTrace();
}
return versionDataAsXML;
}
package org.examples.tools;
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamReader;
import javax.xml.transform.stream.StreamSource;
import org.examples.tools.WebServiceTools;
public class TestGetVersionFromWSResponseFromJAXB {
public static void main (String[] args) throws Exception {
XMLInputFactory xif = XMLInputFactory.newFactory();
StreamSource xml = new StreamSource("C:/examples/VersionResponse.xml");
XMLStreamReader xsr = xif.createXMLStreamReader(xml);
String result = WebServiceTools.getVersionFromWSResponseFromJAXB(xsr);
// Treat result
System.out.print(result);
}
}
package org.examples.tools;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "SchemaVersion", propOrder = {
"schemaVersionDeclared"
})
public class SchemaVersion {
@XmlElement(name = "SchemaVersionDeclared", required = true, defaultValue = "1.0.0.0")
protected String schemaVersionDeclared;
public void setSchemaVersionDeclared(String value) {
this.schemaVersionDeclared = value;
}
}
Upvotes: 0
Views: 2969
Reputation: 8552
Your context has nothing to do with the xml you are parssing:
JAXBContext jaxbContext = JAXBContext.newInstance(SchemaVersion.class);
SchemaVersion {
@XmlElement(name = "SchemaVersionDeclared", required = true, defaultValue = "1.0.0.0")
protected String schemaVersionDeclared;
The expected xml is then:
<SchemaVersion>
<SchemaVersionDeclared>xxx</SchemaVersionDeclared>
</SchemaVersion>
Upvotes: 1