impl - javax.xml.bind.UnmarshalException: elemento inesperado(uri: “”, local: “”). Los elementos esperados son
jaxb xjc download (6)
Puede encontrar muchas preguntas como esta en este sitio, pero ninguna ha resuelto mi problema.
Este es mi XML:
<?xml version=''1.0'' encoding=''UTF-8'' standalone=''yes''?>
<lookupInstances xmlns=''http://www.pqr.com/awd/rest/v1'' name=''LKIMGR''>
<lookupParameters>
<lookupParameter name=''businessArea''>PQAA</lookupParameter>
<lookupParameter name=''MEMBERNUMBER''>ANTHONY1900</lookupParameter>
</lookupParameters>
</lookupInstances>
He creado la clase Jaxb usando el compilador XJC y el archivo creado es como:
//
// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, vJAXB 2.1.10 in JDK 6
// See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a>
// Any modifications to this file will be lost upon recompilation of the source schema.
// Generated on: 2015.01.13 at 12:15:41 PM IST
//
package com.dsths.ga.awd.main;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
/**
* <p>Java class for anonymous complex type.
*
* <p>The following schema fragment specifies the expected content contained within this class.
*
* <pre>
* <complexType>
* <complexContent>
* <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
* <sequence>
* <element name="lookupParameters">
* <complexType>
* <complexContent>
* <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
* <sequence>
* <element name="lookupParameter">
* <complexType>
* <complexContent>
* <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
* <attribute name="name" type="{http://www.w3.org/2001/XMLSchema}string" />
* </restriction>
* </complexContent>
* </complexType>
* </element>
* </sequence>
* </restriction>
* </complexContent>
* </complexType>
* </element>
* </sequence>
* <attribute name="name" type="{http://www.w3.org/2001/XMLSchema}string" />
* </restriction>
* </complexContent>
* </complexType>
* </pre>
*
*
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"lookupParameters"
})
@XmlRootElement(name = "lookupInstances")
public class LookupInstances {
@XmlElement(required = true)
protected LookupInstances.LookupParameters lookupParameters;
@XmlAttribute
protected String name;
/**
* Gets the value of the lookupParameters property.
*
* @return
* possible object is
* {@link LookupInstances.LookupParameters }
*
*/
public LookupInstances.LookupParameters getLookupParameters() {
return lookupParameters;
}
/**
* Sets the value of the lookupParameters property.
*
* @param value
* allowed object is
* {@link LookupInstances.LookupParameters }
*
*/
public void setLookupParameters(LookupInstances.LookupParameters value) {
this.lookupParameters = value;
}
/**
* Gets the value of the name property.
*
* @return
* possible object is
* {@link String }
*
*/
public String getName() {
return name;
}
/**
* Sets the value of the name property.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setName(String value) {
this.name = value;
}
/**
* <p>Java class for anonymous complex type.
*
* <p>The following schema fragment specifies the expected content contained within this class.
*
* <pre>
* <complexType>
* <complexContent>
* <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
* <sequence>
* <element name="lookupParameter">
* <complexType>
* <complexContent>
* <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
* <attribute name="name" type="{http://www.w3.org/2001/XMLSchema}string" />
* </restriction>
* </complexContent>
* </complexType>
* </element>
* </sequence>
* </restriction>
* </complexContent>
* </complexType>
* </pre>
*
*
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"lookupParameter"
})
public static class LookupParameters {
@XmlElement(required = true)
protected LookupInstances.LookupParameters.LookupParameter lookupParameter;
/**
* Gets the value of the lookupParameter property.
*
* @return
* possible object is
* {@link LookupInstances.LookupParameters.LookupParameter }
*
*/
public LookupInstances.LookupParameters.LookupParameter getLookupParameter() {
return lookupParameter;
}
/**
* Sets the value of the lookupParameter property.
*
* @param value
* allowed object is
* {@link LookupInstances.LookupParameters.LookupParameter }
*
*/
public void setLookupParameter(LookupInstances.LookupParameters.LookupParameter value) {
this.lookupParameter = value;
}
/**
* <p>Java class for anonymous complex type.
*
* <p>The following schema fragment specifies the expected content contained within this class.
*
* <pre>
* <complexType>
* <complexContent>
* <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
* <attribute name="name" type="{http://www.w3.org/2001/XMLSchema}string" />
* </restriction>
* </complexContent>
* </complexType>
* </pre>
*
*
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "")
public static class LookupParameter {
@XmlAttribute
protected String name;
/**
* Gets the value of the name property.
*
* @return
* possible object is
* {@link String }
*
*/
public String getName() {
return name;
}
/**
* Sets the value of the name property.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setName(String value) {
this.name = value;
}
}
}
}
Código Unmarshal:
public LookupInstances unmarshallXmlRequest(String xmlReq)
{
LookupInstances instances = null;
try {
JAXBContext jc = JAXBContext.newInstance( LookupInstances.class );
Unmarshaller u = jc.createUnmarshaller();
StringBuffer xmlStr = new StringBuffer( xmlReq );
StringReader strReader = new StringReader( xmlStr.toString() );
StreamSource strSource = new StreamSource(strReader);
Object o = u.unmarshal( strSource );
instances = (LookupInstances)o;
} catch (JAXBException e) {
e.printStackTrace();
}
return instances;
}
Tengo este error
javax.xml.bind.UnmarshalException: unexpected element (uri:"http://www.pqr.com/awd/rest/v1", local:"lookupInstances"). Expected elements are <{}lookupInstances>
Por favor ayuda.
A continuación hay información que debería ayudar:
XML
A continuación se muestra una parte de su XML. Una cosa a tener en cuenta es el atributo xmlns
. Este es un atributo especial y se refiere a la calificación de espacio de nombres dentro del documento XML.
<lookupInstances xmlns=''http://www.pqr.com/awd/rest/v1'' name=''LKIMGR''>
<lookupParameters/>
</lookupInstances>
A continuación se muestra otra versión del XML con la misma calificación de espacio de nombres:
<abc:lookupInstances xmlns:abc=''http://www.pqr.com/awd/rest/v1'' name=''LKIMGR''>
<abc:lookupParameters/>
</abc:lookupInstances>
Cuando elimina el atributo xmlns
la calificación de espacio de nombres se elimina. El documento a continuación no es equivalente a los dos anteriores.
<lookupInstances name=''LKIMGR''>
<lookupParameters/>
</lookupInstances>
Tu error
javax.xml.bind.UnmarshalException: unexpected element (uri:"http://www.pqr.com/awd/rest/v1", local:"lookupInstances"). Expected elements are <{}lookupInstances>
Este error indica que no ha asignado correctamente la calificación de espacio de nombres.
Mapeo de la calificación del espacio de nombres en JAXB
La calificación del espacio de nombres en JAXB se realiza utilizando la anotación @XmlSchema
nivel de paquete. Las anotaciones a nivel de paquete van en package-info.java
. A continuación se muestra la fuente completa de esta clase. Si ya tiene un archivo fuente package-info.java
, asegúrese de que esté siendo compilado y empaquetado con el resto de sus clases.
@XmlSchema(
namespace = "http://www.pqr.com/awd/rest/v1",
elementFormDefault = XmlNsForm.QUALIFIED)
package your_package;
import javax.xml.bind.annotation.XmlNsForm;
import javax.xml.bind.annotation.XmlSchema;
Esquema XML
Si está generando su modelo a partir de un esquema XML, asegúrese de que la calificación del espacio de nombres esté correctamente definida allí. Se verá como
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema
xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.pqr.com/awd/rest/v1"
xmlns="http://www.pqr.com/awd/rest/v1"
elementFormDefault="qualified">
</xs:schema>
Cambio
@XmlRootElement(name = "lookupInstances")
a
@XmlRootElement( namespace = "http://www.pqr.com/awd/rest/v1", name = "lookupInstances")
En primer lugar tienes que comprobar el archivo XML de entrada. Asegúrese de que el XML de entrada debe tener el mismo elemento raíz que su archivo XSD (que ha utilizado para crear las clases de JAXb). Por favor, intente con XML, funcionará bien :)
<?xml version=''1.0'' encoding=''UTF-8'' standalone=''yes''?>
<lookupInstances name=''LKIMGR''>
<lookupParameters>
<lookupParameter name=''businessArea''>PQAA</lookupParameter>
<lookupParameter name=''MEMBERNUMBER''>ANTHONY1900</lookupParameter>
</lookupParameters>
</lookupInstances>
He cambiado la clase jaxb cambiando el xsd y está funcionando ahora.
//
// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v2.2.4-2
// See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a>
// Any modifications to this file will be lost upon recompilation of the source schema.
// Generated on: 2015.01.13 at 03:49:52 PM IST
//
package com.dsths.ga.awd.main;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
import javax.xml.bind.annotation.XmlValue;
/**
* <p>Java class for anonymous complex type.
*
* <p>The following schema fragment specifies the expected content contained within this class.
*
* <pre>
* <complexType>
* <complexContent>
* <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
* <sequence>
* <element name="lookupParameters">
* <complexType>
* <complexContent>
* <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
* <sequence>
* <element name="lookupParameter" maxOccurs="unbounded" minOccurs="0">
* <complexType>
* <simpleContent>
* <extension base="<http://www.w3.org/2001/XMLSchema>string">
* <attribute name="name" type="{http://www.w3.org/2001/XMLSchema}string" />
* </extension>
* </simpleContent>
* </complexType>
* </element>
* </sequence>
* </restriction>
* </complexContent>
* </complexType>
* </element>
* </sequence>
* <attribute name="name" type="{http://www.w3.org/2001/XMLSchema}string" />
* </restriction>
* </complexContent>
* </complexType>
* </pre>
*
*
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"lookupParameters"
})
@XmlRootElement(name = "lookupInstances")
public class LookupInstances {
@XmlElement(required = true)
protected LookupInstances.LookupParameters lookupParameters;
@XmlAttribute(name = "name")
protected String name;
/**
* Gets the value of the lookupParameters property.
*
* @return
* possible object is
* {@link LookupInstances.LookupParameters }
*
*/
public LookupInstances.LookupParameters getLookupParameters() {
return lookupParameters;
}
/**
* Sets the value of the lookupParameters property.
*
* @param value
* allowed object is
* {@link LookupInstances.LookupParameters }
*
*/
public void setLookupParameters(LookupInstances.LookupParameters value) {
this.lookupParameters = value;
}
/**
* Gets the value of the name property.
*
* @return
* possible object is
* {@link String }
*
*/
public String getName() {
return name;
}
/**
* Sets the value of the name property.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setName(String value) {
this.name = value;
}
/**
* <p>Java class for anonymous complex type.
*
* <p>The following schema fragment specifies the expected content contained within this class.
*
* <pre>
* <complexType>
* <complexContent>
* <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
* <sequence>
* <element name="lookupParameter" maxOccurs="unbounded" minOccurs="0">
* <complexType>
* <simpleContent>
* <extension base="<http://www.w3.org/2001/XMLSchema>string">
* <attribute name="name" type="{http://www.w3.org/2001/XMLSchema}string" />
* </extension>
* </simpleContent>
* </complexType>
* </element>
* </sequence>
* </restriction>
* </complexContent>
* </complexType>
* </pre>
*
*
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"lookupParameter"
})
public static class LookupParameters {
protected List<LookupInstances.LookupParameters.LookupParameter> lookupParameter;
/**
* Gets the value of the lookupParameter property.
*
* <p>
* This accessor method returns a reference to the live list,
* not a snapshot. Therefore any modification you make to the
* returned list will be present inside the JAXB object.
* This is why there is not a <CODE>set</CODE> method for the lookupParameter property.
*
* <p>
* For example, to add a new item, do as follows:
* <pre>
* getLookupParameter().add(newItem);
* </pre>
*
*
* <p>
* Objects of the following type(s) are allowed in the list
* {@link LookupInstances.LookupParameters.LookupParameter }
*
*
*/
public List<LookupInstances.LookupParameters.LookupParameter> getLookupParameter() {
if (lookupParameter == null) {
lookupParameter = new ArrayList<LookupInstances.LookupParameters.LookupParameter>();
}
return this.lookupParameter;
}
/**
* <p>Java class for anonymous complex type.
*
* <p>The following schema fragment specifies the expected content contained within this class.
*
* <pre>
* <complexType>
* <simpleContent>
* <extension base="<http://www.w3.org/2001/XMLSchema>string">
* <attribute name="name" type="{http://www.w3.org/2001/XMLSchema}string" />
* </extension>
* </simpleContent>
* </complexType>
* </pre>
*
*
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"value"
})
public static class LookupParameter {
@XmlValue
protected String value;
@XmlAttribute(name = "name")
protected String name;
/**
* Gets the value of the value property.
*
* @return
* possible object is
* {@link String }
*
*/
public String getValue() {
return value;
}
/**
* Sets the value of the value property.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setValue(String value) {
this.value = value;
}
/**
* Gets the value of the name property.
*
* @return
* possible object is
* {@link String }
*
*/
public String getName() {
return name;
}
/**
* Sets the value of the name property.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setName(String value) {
this.name = value;
}
}
}
}
Parece que la clase creada por JAXB no contiene la información del espacio de nombres:
@XmlType(name = "", propOrder = {
"lookupParameters"
})
Sospecho que causa este error, simplemente porque no espera ningún espacio de nombres:
javax.xml.bind.UnmarshalException: elemento inesperado (uri: " pqr.com/awd/rest/v1 ", local: "lookupInstances"). Los elementos esperados son <{}Instancias de búsqueda>
No estoy seguro de por qué la clase generada carece de esta información, pero comenzaría agregándola manualmente para verificar que este es el problema:
@XmlType(name = "", namespace= "http://www.pqr.com/awd/rest/v1", propOrder = {
"lookupParameters"
})
@XmlSchema(namespace = "http://www.pqr.com/awd/rest/v1",
elementFormDefault = XmlNsForm.QUALIFIED) package your_package;
import javax.xml.bind.annotation.XmlNsForm;
import javax.xml.bind.annotation.XmlSchema;
Esta parte es muy importante para las clases generadas por JAXB, especialmente el elementFormDefault = XmlNsForm.QUALIFIED
no agregará automáticamente elementFormDefault = XmlNsForm.QUALIFIED
.
Gracias Blaise, tu respuesta me ayudó.