0
我試圖解組一個XML文件,但不斷收到此錯誤:javax.xml.bind.UnmarshalException:意外元素(uri:「MyProtocol.xsd」,local:「MyFrame」)。預計元素(無)
javax.xml.bind.UnmarshalException: unexpected element (uri:"MyProtocol.xsd", local:"MyFrame"). Expected elements are (none)
我使用Axis2的產生與亞行從我的WSDL文件綁定的類。這是的.wsdl根:
<?xml version="1.0" encoding="utf-8"?>
<definitions xmlns:xs="http://www.w3.org/2001/XMLSchema/"
xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:tns="MyProtocol.xsd"
xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:wsi="http://ws-i.org/profiles/basic/1.1/xsd"
xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="MyProtocol.xsd">
<types>
<xs:schema targetNamespace="MyProtocol.xsd"
xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="MyProtocol.xsd"
elementFormDefault="qualified">
<xs:import namespace="http://ws-i.org/profiles/basic/1.1/xsd"
schemaLocation="http://ws-i.org/profiles/basic/1.1/swaref.xsd" />
<xs:element name="MyFrame">
<xs:complexType>
<xs:choice maxOccurs="unbounded">
...
這是代碼我要解組,這我假設不Axis2和亞洲開發銀行的綁定工作。
public class Foobar<T>
{
private T obj;
private Class<T> type;
public Foobar(Class<T> type) {
this.type = type;
}
public void unmarshalXML(String xml) {
JAXBContext jaxbContext;
Unmarshaller unmarshaller;
StringReader reader;
try {
jaxbContext = JAXBContext.newInstance(type);
unmarshaller = jaxbContext.createUnmarshaller();
reader = new StringReader(xml);
obj = (T) unmarshaller.unmarshal(reader);
} catch(JAXBException e) {
e.printStackTrace();
}
}
}
Axis2生成一個存根類,其中包含我架構中所有選項的getters,setters和classes。我使用該類,並調用unmarshal
方法是這樣的:
Foobar<MyStub.MyFrame> foobar = new Foobar<MyStub.MyFrame>(MyStub.MyFrame.class);
// Unmarshal
String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
+ "<MyFrame xmlns=\"MyProtocol.xsd\">"
+ "<YC>"
+ "<SenderId>172</SenderId>"
+ "<RequestId>123saA</RequestId>"
+ "<SubNumber>5558879876</SubNumber>"
+ "</YC>"
+ "</MyFrame>";
foobar.unmarshalXML(xml);
然後我得到上面發佈的錯誤。這是爲什麼發生?我的代碼錯了嗎?
請使用鏈接http://stackoverflow.com/questions/20586737/javax-xml-bind-unmarshalexception-unexpected-element-expected-elements-are-no – Veeram
感謝您的回覆。但是,我不認爲我可以使用該鏈接中的信息。我相信我不能使用我使用的JAXB代碼,因爲我使用的是ADB。該鏈接表示使用註釋,但由Axis2使用ADB生成的類不使用註釋。 –