Vous pouvez trouver beaucoup de questions comme celle-ci sur ce site, mais aucune n’a résolu mon problème.
Ceci est mon 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>
J'ai créé la classe Jaxb en utilisant le compilateur XJC et le fichier créé est le suivant:
//
// 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;
}
}
}
}
Code démarsal:
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;
}
J'ai eu cette erreur:
javax.xml.bind.UnmarshalException: unexpected element (uri:"http://www.pqr.com/awd/rest/v1", local:"lookupInstances"). Expected elements are <{}lookupInstances>
S'il vous plaît aider.
Voici quelques informations qui devraient aider:
XML
Vous trouverez ci-dessous une partie de votre code XML. Une chose à noter est l'attribut xmlns
. Ceci est un attribut spécial et fait référence à la qualification d'espace de nom dans le document XML.
<lookupInstances xmlns='http://www.pqr.com/awd/rest/v1' name='LKIMGR'>
<lookupParameters/>
</lookupInstances>
Vous trouverez ci-dessous une autre version du code XML avec la même qualification d’espace de nom:
<abc:lookupInstances xmlns:abc='http://www.pqr.com/awd/rest/v1' name='LKIMGR'>
<abc:lookupParameters/>
</abc:lookupInstances>
Lorsque vous supprimez l'attribut xmlns
, la qualification d'espace de nom est supprimée. Le document ci-dessous n'est pas équivalent aux deux ci-dessus.
<lookupInstances name='LKIMGR'>
<lookupParameters/>
</lookupInstances>
Votre erreur
javax.xml.bind.UnmarshalException: unexpected element (uri:"http://www.pqr.com/awd/rest/v1", local:"lookupInstances"). Expected elements are <{}lookupInstances>
Cette erreur indique que vous n'avez pas mappé correctement la qualification d'espace de noms.
Mappage de la qualification Namesapce dans JAXB
La qualification de l'espace de noms dans JAXB est effectuée à l'aide de l'annotation de niveau de package @XmlSchema
. Les annotations de niveau de paquet vont dans package-info.Java
. Vous trouverez ci-dessous la source complète pour cette classe. Si vous avez déjà un fichier source package-info.Java
, assurez-vous qu’il est compilé et empaqueté avec le reste de vos classes.
@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;
Schéma XML
Si vous générez votre modèle à partir d'un schéma XML, assurez-vous que cette qualification d'espace de nom est correctement définie. Cela ressemblera à:
<?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>
Changement
@XmlRootElement(name = "lookupInstances")
à
@XmlRootElement( namespace = "http://www.pqr.com/awd/rest/v1", name = "lookupInstances")
J'ai changé la classe jaxb en changeant le xsd et cela fonctionne maintenant.
//
// 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;
}
}
}
}
Il semble que la classe créée par JAXB ne contienne pas les informations d'espace de nom:
@XmlType(name = "", propOrder = {
"lookupParameters"
})
Je soupçonne que cela cause cette erreur, simplement parce qu'il n'attend aucun espace de nom:
javax.xml.bind.UnmarshalException: élément inattendu (uri: " http://www.pqr.com/awd/rest/v1 ", local: "lookupInstances"). Les éléments attendus sont <{} lookupInstances>
Je ne sais pas pourquoi la classe générée manque de cette information, mais je commencerais par l'ajouter manuellement pour vérifier qu'il s'agit bien du problème:
@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;
Cette partie est très importante pour les classes générées par JAXB, en particulier elementFormDefault = XmlNsForm.QUALIFIED
ne sera pas automatiquement ajouté par JAXB.
Merci Blaise, ta réponse m'a aidé.
Tout d’abord, vous devez vérifier le fichier XML d’entrée. Assurez-vous que le XML d'entrée doit avoir le même élément racine que votre fichier XSD (que vous avez utilisé pour créer les classes JAXb) Essayez donc ci-dessous XML, cela fonctionnera 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>