Suivant le fichier TPAExtensionsType.java J'ai généré à partir d'un fichier XSD.JAXB marshalling objet ayant la propriété de type Liste <Element> (@XmlAnyElement) ne fournit pas de valeur de noeud de sortie
TPAExtensionsType.java
/*
* <p>The following schema fragment specifies the expected content contained within this class.
*
* <pre>
* <complexType name="TPA_Extensions_Type">
* <complexContent>
* <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
* <sequence>
* <any processContents='skip' maxOccurs="unbounded" minOccurs="0"/>
* </sequence>
* </restriction>
* </complexContent>
* </complexType>
* </pre>
*
*
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "TPA_Extensions_Type", propOrder = {
"any"
})
@XmlRootElement
public class TPAExtensionsType {
@XmlAnyElement
protected List<Element> any;
/**
* Gets the value of the any 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 any property.
*
* <p>
* For example, to add a new item, do as follows:
* <pre>
* getAny().add(newItem);
* </pre>
*
*
* <p>
* Objects of the following type(s) are allowed in the list
* {@link Element }
*
*
*/
public List<Element> getAny() {
if (any == null) {
any = new ArrayList<Element>();
}
return this.any;
}
}
l'application qui suit est autonome Marshall de l'objet ci-dessus pour XML.
TestUtil.java
public class TestUtil {
public static void main(String[] args) {
TPAExtensionsType tpaExtensions = new TPAExtensionsType();
Element consumerInfo = new DOMElement("ConsumerInfo");
consumerInfo.setNodeValue("Some Info");
tpaExtensions.getAny().add(consumerInfo);
StringWriter sw = new StringWriter();
JAXBContext context;
try {
context = JAXBContext.newInstance(TPAExtensionsType.class);
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
marshaller.marshal(tpaExtensions, sw);
System.out.println(sw.toString());
} catch (JAXBException e) {
e.printStackTrace();
}
}
}
Voici le résultat:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<tpaExtensionsType xmlns="SOME_NAMESPACE_ HERE">
<ConsumerInfo xmlns="" xmlns:ns2="SOME_NAMESPACE_ HERE"/>
</tpaExtensionsType>
Problème Je suis face:
Le nœud ConsumerInfo a été créé, mais sa valeur est visible dans le XML généré bien que j'ai mis sa valeur dans mon application autonome ci-dessus. Peut-on m'aider s'il vous plaît à obtenir ce corrigé et ce qui est provoquant ce problème?
Merci Sahil.Your worked.However suggestion dans la sortie je peux trouver un espace de noms se joint en annexe comme un attribut pour l'élément consumerInfo comme celui en italique gras avant: ** < consumerInfo _xmlns: ns2 = "QUELQUES NAMESPACE ICI" _ xmlns = ""> quelques infos consumerInfo> **. Peut-on se débarrasser de JAXB? –
@JiggneshhGohel Avez-vous trouvé la solution à vos attributs d'espace de noms? J'ai exactement le même problème et je me casse la tête pour trouver une solution. – tarares
Désolé @tarares! Je ne m'en souviens pas. Cela fait des années que je n'ai pas travaillé avec JAXB. –