gpt4 book ai didi

java - 解码错误 : unexpected element (uri: local:), 预期元素为

转载 作者:太空宇宙 更新时间:2023-11-04 12:47:05 25 4
gpt4 key购买 nike

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

这是我的代码!

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>
* &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;
}

}

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>
* &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;
}

}

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>
* &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;
}

}

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

}

}

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

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

最佳答案

您的 XML 文件不符合 XML 架构定义的要求。使用它作为根元素:

<root xmlns="http://www.example.org/Territorialita">
...
</root>

提示:获取有效 XML 文档的一个好方法是创建一小组表示文档的 Java 对象并将其编码。

关于java - 解码错误 : unexpected element (uri: local:), 预期元素为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36204193/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com