I need your help, because I'm trying to solve this issue but without success! :(
Here's my code!
Territorialita.xsd
<?xml version="1.0" encoding="UTF-8"?> <!-- xmlns:xs : assergno il namespace di default in cui troviamo gli elementi XML Schema --> <!-- targetNamespace : definisco il namespace di destinazione, in cui salvare gli elementi definiti --> <!-- xmlns:tns : assegno il namespace di destinazione --> <!-- elementFormDefault : posto a "qualified" indica che tutti i documenti che usano gli elementi definiti in questa sintassi dovranno sempre dichiararne il namespace --> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.org/Territorialita" xmlns:tns="http://www.example.org/Territorialita" elementFormDefault="qualified"> <!-- definisco il tipo Territorialita --> <xs:complexType name="TerritorialitaType"> <xs:sequence> <xs:element name="Titolo" type="xs:string" /> <xs:element name="descrizioneSintetica" type="xs:string" /> <xs:element name="descrizioneDettagliata" type="xs:string" /> </xs:sequence> <xs:attribute name="Codice" type="xs:integer" /> </xs:complexType> <!-- definisco il tipo Array Territorialita --> <xs:complexType name="ArrayTerritorialitaType"> <xs:sequence> <xs:element name="Territorialita" type="tns:TerritorialitaType" minOccurs="1" maxOccurs="unbounded" /> </xs:sequence> </xs:complexType> <!-- definisco la struttura del documento --> <xs:complexType name="root"> <xs:sequence> <xs:element name="ArrayTerritorialita" type="tns:ArrayTerritorialitaType" minOccurs="1" maxOccurs="unbounded" /> </xs:sequence> </xs:complexType> </xs:schema>
Territorialita.xml
<?xml version="1.0" encoding="UTF-8"?> <Root> <ArrayTerritorialita> <Territorialita codice="1"> <Titolo>Elemento 1</Titolo> <descrizioneSintetica> Descrizione sintetica - elemento 1</descrizioneSintetica> <descrizioneDettagliata> Descrizione dettagliata - elemento 1</descrizioneDettagliata> </Territorialita> <Territorialita codice="2"> <Titolo>Elemento 2</Titolo> <descrizioneSintetica> Descrizione sintetica - elemento 2</descrizioneSintetica> <descrizioneDettagliata> Descrizione dettagliata - elemento 2</descrizioneDettagliata> </Territorialita> <Territorialita codice="3"> <Titolo>Elemento 3</Titolo> <descrizioneSintetica> Descrizione sintetica - elemento 3</descrizioneSintetica> <descrizioneDettagliata> Descrizione dettagliata - elemento 3</descrizioneDettagliata> </Territorialita> </ArrayTerritorialita> </Root>
Root.java
package it.sogei.studi.territorialita.JAXB; 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.XmlElement; import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlType; /** * <p>Java class for root complex type. * * <p>The following schema fragment specifies the expected content contained within this class. * * <pre> * <complexType name="root"> * <complexContent> * <restriction base="{http://www.w3.org/2001/XMLSchema}anyType"> * <sequence> * <element name="ArrayTerritorialita" type="{http://www.example.org/Territorialita}ArrayTerritorialitaType" maxOccurs="unbounded"/> * </sequence> * </restriction> * </complexContent> * </complexType> * </pre> * * */ @XmlRootElement(name="root") @XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "root", propOrder = { "arrayTerritorialita" }) public class Root { @XmlElement(name = "ArrayTerritorialita", required = true) protected List<ArrayTerritorialitaType> arrayTerritorialita; /** * Gets the value of the arrayTerritorialita 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 arrayTerritorialita property. * * <p> * For example, to add a new item, do as follows: * <pre> * getArrayTerritorialita().add(newItem); * </pre> * * * <p> * Objects of the following type(s) are allowed in the list * {@link ArrayTerritorialitaType } * * */ public List<ArrayTerritorialitaType> getArrayTerritorialita() { if (arrayTerritorialita == null) { arrayTerritorialita = new ArrayList<ArrayTerritorialitaType>(); } return this.arrayTerritorialita; } }
ArrayTerritorialitaType.java
package it.sogei.studi.territorialita.JAXB; 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.XmlElement; import javax.xml.bind.annotation.XmlType; /** * <p>Java class for ArrayTerritorialitaType complex type. * * <p>The following schema fragment specifies the expected content contained within this class. * * <pre> * <complexType name="ArrayTerritorialitaType"> * <complexContent> * <restriction base="{http://www.w3.org/2001/XMLSchema}anyType"> * <sequence> * <element name="Territorialita" type="{http://www.example.org/Territorialita}TerritorialitaType" maxOccurs="unbounded"/> * </sequence> * </restriction> * </complexContent> * </complexType> * </pre> * * */ @XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "ArrayTerritorialitaType", propOrder = { "territorialita" }) public class ArrayTerritorialitaType { @XmlElement(name = "Territorialita", required = true) protected List<TerritorialitaType> territorialita; /** * Gets the value of the territorialita 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 territorialita property. * * <p> * For example, to add a new item, do as follows: * <pre> * getTerritorialita().add(newItem); * </pre> * * * <p> * Objects of the following type(s) are allowed in the list * {@link TerritorialitaType } * * */ public List<TerritorialitaType> getTerritorialita() { if (territorialita == null) { territorialita = new ArrayList<TerritorialitaType>(); } return this.territorialita; } }
TerritorialitaType.java
package it.sogei.studi.territorialita.JAXB; 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.XmlType; /** * <p>Java class for TerritorialitaType complex type. * * <p>The following schema fragment specifies the expected content contained within this class. * * <pre> * <complexType name="TerritorialitaType"> * <complexContent> * <restriction base="{http://www.w3.org/2001/XMLSchema}anyType"> * <sequence> * <element name="Titolo" type="{http://www.w3.org/2001/XMLSchema}string"/> * <element name="descrizioneSintetica" type="{http://www.w3.org/2001/XMLSchema}string"/> * <element name="descrizioneDettagliata" type="{http://www.w3.org/2001/XMLSchema}string"/> * </sequence> * <attribute name="Codice" type="{http://www.w3.org/2001/XMLSchema}integer" /> * </restriction> * </complexContent> * </complexType> * </pre> * * */ @XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "TerritorialitaType", propOrder = { "titolo", "descrizioneSintetica", "descrizioneDettagliata" }) public class TerritorialitaType { @XmlElement(name = "Titolo", required = true) protected String titolo; @XmlElement(required = true) protected String descrizioneSintetica; @XmlElement(required = true) protected String descrizioneDettagliata; @XmlAttribute(name = "Codice") protected int codice; /** * Gets the value of the titolo property. * * @return * possible object is * {@link String } * */ public String getTitolo() { return titolo; } /** * Sets the value of the titolo property. * * @param value * allowed object is * {@link String } * */ public void setTitolo(String value) { this.titolo = value; } /** * Gets the value of the descrizioneSintetica property. * * @return * possible object is * {@link String } * */ public String getDescrizioneSintetica() { return descrizioneSintetica; } /** * Sets the value of the descrizioneSintetica property. * * @param value * allowed object is * {@link String } * */ public void setDescrizioneSintetica(String value) { this.descrizioneSintetica = value; } /** * Gets the value of the descrizioneDettagliata property. * * @return * possible object is * {@link String } * */ public String getDescrizioneDettagliata() { return descrizioneDettagliata; } /** * Sets the value of the descrizioneDettagliata property. * * @param value * allowed object is * {@link String } * */ public void setDescrizioneDettagliata(String value) { this.descrizioneDettagliata = value; } /** * Gets the value of the codice property. * * @return * possible object is * {@link BigInteger } * */ public int getCodice() { return codice; } /** * Sets the value of the codice property. * * @param value * allowed object is * {@link BigInteger } * */ public void setCodice(int value) { this.codice = value; } }
Test.java
package it.sogei.studi.territorialita.main; import java.io.File; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Unmarshaller; import it.sogei.studi.territorialita.JAXB.ArrayTerritorialitaType; import it.sogei.studi.territorialita.JAXB.Root; public class Test { public static void main(String[] args) { // definisco il percorso del file XML String filePath = "src/it/sogei/studi/territorialita/risorse/Territorialita.xml"; try { File file = new File(filePath); JAXBContext jaxbContext = JAXBContext.newInstance(it.sogei.studi.territorialita.JAXB.Root.class); Unmarshaller unmarhsaller = jaxbContext.createUnmarshaller(); ArrayTerritorialitaType ArrayTerr = (ArrayTerritorialitaType) unmarhsaller.unmarshal(file); System.out.print(ArrayTerr); System.out.println("**** PROGRAMMA TERMINATO ****"); } catch(JAXBException e) { e.printStackTrace(); } } }
The XML document must have a root element, then an array of child nodes (arrayTerritorialita).
But when I try to unmarshal, I've this error:
javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"Root"). Expected elements are <{http://www.example.org/Territorialita}root>
Please help me!!!!!!
Tiada ulasan:
Catat Ulasan