javax.xml.bind.UnmarshalException:意外元素(uri:“”,local:“sRAssignments”)。预期的要素是

时间:2012-12-26 21:46:25

标签: xml jaxb bind unmarshalling

SRAssignments.java(包装类):

@XmlRootElement
@XmlType(propOrder = {})
public class SRAssignments
{

@XmlElement(name = "srAssignments", type = SRAssignment.class) private     
List<SRAssignment> srAssignments = new ArrayList<SRAssignment>();

public SRAssignments()
{
}

@SuppressWarnings("javadoc")
public SRAssignments(List<SRAssignment> srAssignments)
{
    this.srAssignments = srAssignments;
}

@SuppressWarnings("javadoc")
public List<SRAssignment> getAssignments()
{
    return this.srAssignments;
}


@SuppressWarnings("javadoc")
public void setAssignments(List<SRAssignment> srAssignments)
{
    this.srAssignments = srAssignments;
}

}

客户代码:

        StringBuilder getAssignmentsResponse = 
(StringBuilder) RestClientUtil.sendGetMethod(urlGetAssignments
                + URLEncoder.encode(technician, "UTF-8"));
      Unmarshaller unmarshaller =  RestClientUtil.getUnmarshaller(SRAssignments.class);
      reader = new StringReader(new String(getAssignmentsResponse));
      assignments = (SRAssignments) unmarshaller.unmarshal(reader);

getAssignmentsResponse中的XML内容显示:

<?xml version="1.0" encoding="UTF-8"?><sRAssignments><srAssignment><assignedBy><bonus>0.0</bonus><commission>0.0</commission><educationLevel>1</educationLevel><email>email1@test.com</email><empNo>RMGR1</empNo><firstName>first1</firstName><lastName>last1</lastName><role>manager</role><salary>0.0</salary></assignedBy><assignedOn>2012-12-26T14:09:41.763-08:00</assignedOn><comment>remote agent assigning a request...</comment><employee><bonus>0.0</bonus><commission>0.0</commission><educationLevel>1</educationLevel><email>email1@ge.com</email><empNo>TN1</empNo><firstName>first1</firstName><lastName>last1</lastName><role>technician</role><salary>0.0</salary></employee><serviceRequest><customer><city>city1</city><companyName>companyName1</companyName><custNo>RCN1</custNo><email>email1@ge.com</email><firstName>first1</firstName><gender>F</gender><lastName>last1</lastName><midInit>K</midInit><phone>4082229871      </phone><state>CA</state><streetAddress>streetAddress1</streetAddress><title>S   </title><zipCode>zipCode   </zipCode></customer><dateCreated>2012-12-26T14:09:38.678-08:00</dateCreated><description>Stress testing, customer is submitting a service request for some repair request.10Request by: Thread-1</description><srNo>107824</srNo><status>2</status></serviceRequest><sraId>34974</sraId><status>1</status></srAssignment></sRAssignments>

例外:

    javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"sRAssignments"). Expected elements are <{}customer>,<{}customerSurvey>,<{}employee>,<{}serviceRequest>,<{}srAssignment>,<{}srAssignments>,<{}surveyResponse>
        at 
com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(UnmarshallingContext.java:662)
        at com.sun.xml.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:258)
        at com.sun.xml.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:253)
        at com.sun.xml.bind.v2.runtime.unmarshaller.Loader.reportUnexpectedChildElement(Loader.java:120)
        at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext$DefaultRootLoader.childElement(UnmarshallingContext.java:1063)
        at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext._startElement(UnmarshallingContext.java:498)
        at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext.startElement(UnmarshallingContext.java:480)
        at com.sun.xml.bind.v2.runtime.unmarshaller.SAXConnector.startElement(SAXConnector.java:150)
        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.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:217)
        at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:189)
        at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:137)
        at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:194)
        at com.ge.dsp.iworkRemote.remoteAgents.TechnicianRemoteAgent.getAssignmentsByEmployee(TechnicianRemoteAgent.java:134)
        at com.ge.dsp.iworkRemote.remoteAgents.TechnicianRemoteAgent.execute(TechnicianRemoteAgent.java:86)
        at com.ge.dsp.iworkRemote.remoteAgents.TechnicianRemoteAgent$TechnicianAgentRunner.run(TechnicianRemoteAgent.java:256)
        at java.lang.Thread.run(Thread.java:662)

1 个答案:

答案 0 :(得分:2)

下面注释的情况与从响应中读取的xml不同。

@XmlElement(name = “srAssignments”,type = SRAssignment.class)

检查你的情况。你最后的注释应该如下所示。

@XmlElement(name = "sRAssignments", type = SRAssignment.class)