我很难发现自己做错了什么。
这是我使用的XML
<PasswordResponse xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="PasswordResponse">
<date>
<day>1</day>
<month>1</month>
<year>1</year>
</date>
<type>1</type>
<version>1</version>
<error>0</error>
<status>200</status>
<password>00000</password>
</PasswordResponse>
这是我的Java类
package testhttprequest;
import java.io.DataOutputStream;
import java.net.URL;
import javax.net.ssl.HttpsURLConnection;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
public class TestHttpRequest
{
public static void main(String[] args) throws Exception
{
try
{
JAXBContext jaxbContext = JAXBContext.newInstance(PasswordResponse.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
PasswordResponse pr = (PasswordResponse) jaxbUnmarshaller.unmarshal(*legit-xml-input*);
System.out.println(pr);
}
catch (JAXBException e)
{
e.printStackTrace();
}
}
}
这是我的Java对象类
package testhttprequest;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name="PasswordResponse")
@XmlAccessorType(XmlAccessType.FIELD)
public class PasswordResponse
{
public date date;
public int type;
public int version;
public int error;
public int status;
public String password;
public PasswordResponse()
{
this.date = new date();
this.type = 0;
this.version = 0;
this.error = 0;
this.status = 0;
}
public PasswordResponse(int day, int month, int year, int type, int version)
{
this.date = new date(day, month, year);
this.type = type;
this.version = version;
this.error = 0;
this.status = 200;
}
public testhttprequest.date getDate()
{
return date;
}
public int getError()
{
return error;
}
public String getPassword()
{
return password;
}
public int getStatus()
{
return status;
}
public int getType()
{
return type;
}
public int getVersion()
{
return version;
}
public void setDate(testhttprequest.date date)
{
this.date = date;
}
public void setError(int error)
{
this.error = error;
}
public void setPassword(String password)
{
this.password = password;
}
public void setStatus(int status)
{
this.status = status;
}
public void setType(int type)
{
this.type = type;
}
public void setVersion(int version)
{
this.version = version;
}
}
我得到的错误是休闲
javax.xml.bind.UnmarshalException: unexpected element (uri:"PasswordResponse", local:"PasswordResponse"). Expected elements are <{}PasswordResponse>
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(UnmarshallingContext.java:648)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:236)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:231)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportUnexpectedChildElement(Loader.java:105)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext$DefaultRootLoader.childElement(UnmarshallingContext.java:1051)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext._startElement(UnmarshallingContext.java:484)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.startElement(UnmarshallingContext.java:465)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.SAXConnector.startElement(SAXConnector.java:135)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.startElement(AbstractSAXParser.java:501)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(XMLNSDocumentScannerImpl.java:400)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl$NSContentDriver.scanRootElementHook(XMLNSDocumentScannerImpl.java:626)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:3104)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(XMLDocumentScannerImpl.java:921)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:647)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:140)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:511)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:808)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:737)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:119)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1205)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:522)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:200)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:173)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:137)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:184)
at testhttprequest.TestHttpRequest.sendPost(TestHttpRequest.java:78)
at testhttprequest.TestHttpRequest.main(TestHttpRequest.java:29)
在错误中所有3个参数是相同的,那应该是那样的,对吧?