Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 解组错误:意外元素(uri:local:),预期元素为_Java_Xml_Xsd_Jaxb_Unmarshalling - Fatal编程技术网

Java 解组错误:意外元素(uri:local:),预期元素为

Java 解组错误:意外元素(uri:local:),预期元素为,java,xml,xsd,jaxb,unmarshalling,Java,Xml,Xsd,Jaxb,Unmarshalling,我需要你的帮助,因为我试图解决这个问题,但没有成功!:( 这是我的密码 Territorialita.xsd * * * *列表中允许以下类型的对象 *{@link arrayteritorialitatype} * * */ 公共列表getArrayTerritorialita(){ if(arrayTerritorialita==null){ arrayTerritorialita=新的ArrayList(); } 返回此.arrayTerritorialita; } } Arra

我需要你的帮助,因为我试图解决这个问题,但没有成功!:(

这是我的密码

Territorialita.xsd

* * * *列表中允许以下类型的对象 *{@link arrayteritorialitatype} * * */ 公共列表getArrayTerritorialita(){ if(arrayTerritorialita==null){ arrayTerritorialita=新的ArrayList(); } 返回此.arrayTerritorialita; } } ArrayTerrialitaType.java

* * * *列表中允许以下类型的对象 *{@link TerritorialitaType} * * */ 公共列表getterritoriata(){ 如果(territorialita==null){ territorialita=新ArrayList(); } 归还这个。territorialita; } } TerritorialitaType.java

XML文档必须有一个根元素,然后是一个子节点数组(
arrayTerritorialita
)。但当我尝试解组时,出现以下错误:

javax.xml.bind.UnmarshaleException:意外元素(uri:,local:“Root”)。预期元素为

您的XML文件不符合XML架构定义的要求。请将其用于根元素:


...

提示:获取有效XML文档的一个好方法是创建一小组表示文档的Java对象并封送它。

您的XML文件不符合XML架构定义的要求。请将此用于根元素:


...
提示:获取有效XML文档的一个好方法是创建一小组表示文档的Java对象并封送它

<?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>
<?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>
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>
 * &lt;complexType name="root">
 *   &lt;complexContent>
 *     &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
 *       &lt;sequence>
 *         &lt;element name="ArrayTerritorialita" type="{http://www.example.org/Territorialita}ArrayTerritorialitaType" maxOccurs="unbounded"/>
 *       &lt;/sequence>
 *     &lt;/restriction>
 *   &lt;/complexContent>
 * &lt;/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;
    }

}
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>
 * &lt;complexType name="ArrayTerritorialitaType">
 *   &lt;complexContent>
 *     &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
 *       &lt;sequence>
 *         &lt;element name="Territorialita" type="{http://www.example.org/Territorialita}TerritorialitaType" maxOccurs="unbounded"/>
 *       &lt;/sequence>
 *     &lt;/restriction>
 *   &lt;/complexContent>
 * &lt;/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;
    }

}
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>
 * &lt;complexType name="TerritorialitaType">
 *   &lt;complexContent>
 *     &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
 *       &lt;sequence>
 *         &lt;element name="Titolo" type="{http://www.w3.org/2001/XMLSchema}string"/>
 *         &lt;element name="descrizioneSintetica" type="{http://www.w3.org/2001/XMLSchema}string"/>
 *         &lt;element name="descrizioneDettagliata" type="{http://www.w3.org/2001/XMLSchema}string"/>
 *       &lt;/sequence>
 *       &lt;attribute name="Codice" type="{http://www.w3.org/2001/XMLSchema}integer" />
 *     &lt;/restriction>
 *   &lt;/complexContent>
 * &lt;/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;
    }

}
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();
        }

    }

}
javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"Root"). Expected elements are <{http://www.example.org/Territorialita}root>
<root xmlns="http://www.example.org/Territorialita">
    ...
</root>