Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/rust/4.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 使用JAXB解组时XML格式错误_Java_Xml_Xsd_Jaxb_Unmarshalling - Fatal编程技术网

Java 使用JAXB解组时XML格式错误

Java 使用JAXB解组时XML格式错误,java,xml,xsd,jaxb,unmarshalling,Java,Xml,Xsd,Jaxb,Unmarshalling,编辑:多亏了Preetam Kumar,这个问题得到了解决,但错误现在在别处。当我再次尝试解组xml文件时,出现以下错误,告诉我找不到元素: javax.xml.bind.UnmarshalException - with linked exception: [org.xml.sax.SAXParseException; systemId: file:***/Workspace_XML/fichier_cri/xml/exemple1.xml; lineNumber: 5; columnNumb

编辑:多亏了Preetam Kumar,这个问题得到了解决,但错误现在在别处。当我再次尝试解组xml文件时,出现以下错误,告诉我找不到元素:

javax.xml.bind.UnmarshalException
- with linked exception:
[org.xml.sax.SAXParseException; systemId: file:***/Workspace_XML/fichier_cri/xml/exemple1.xml; lineNumber: 5; columnNumber: 185; cvc-elt.1 : Declaration of element 'crifile' cannot be found.]
但是,我为xml文件提供了模式的正确路径,但它似乎没有使用它。。。我不明白

注意:我知道模式和xml标记在下面的示例中不完全匹配,我已经在我的文件中更正了它


现在,我必须使用JAXB java库将一些XML文件解析为java对象

我创建了一个XML模式(.xsd),并使用工具xjc编译它并生成我的Java类。(有关xsd文件的示例,请参见我的问题:)

现在,我试图通过解组自定义XML文件来测试我的模式和生成的类。这是我的问题。 从一开始,当我声明名称空间时,我经常会遇到一个错误,即我的xml格式不正确,正如我在许多网站和示例中看到的:

<?xml version="1.0" encoding="UTF-8"?> 
<xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/> 
<xsi:schemaLocation="***\Workspace_XML\fichier_cri\schema.xsd"/>
我尝试修改该行,甚至将其删除,但随后出现另一个错误:

javax.xml.bind.UnmarshalException
- with linked exception:
[org.xml.sax.SAXParseException; systemId: file:***/Workspace_XML/fichier_cri/xml/exemple1.xml; lineNumber: 5; columnNumber: 10; cvc-elt.1 : Déclaration of element 'crifile' not found.]
现在,我不知道该怎么办了,所以如果有人有什么提示或什么的,我不会说不

我对我的Java测试程序也不是很有信心。。。但这是:

import generated.FichierCri;

import java.io.File;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;

import org.xml.sax.SAXException;

public class main {

    final static File   baseDir = new File("***\\Workspace_XML\\fichier_cri\\");

    public static void main(String[] args) {

        System.out.println("*DEBUG* - load xml stream");
        System.out.println("*DEBUG* - xml file location : " + baseDir.getPath() + "\\xml\\exemple1.xml");

        File xmlFile = new File(baseDir, "\\xml\\exemple1.xml");

        if (xmlFile == null)
            throw new NullPointerException("xmlFile is null");

        System.out.println("*DEBUG* - Instantiate SchemaFactory");

        SchemaFactory schemaFactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");

        System.out.println("*DEBUG* - Load schema file");
        System.out.println("*DEBUG* - schema location : " + baseDir.getPath() + "\\schema\\schema.xsd");

        Schema schema = null;
        try {
            schema = schemaFactory.newSchema(new File(baseDir, "\\schema\\schema.xsd"));
        }
        catch (SAXException e) {
            e.printStackTrace();
        }

        if (schema == null) throw new NullPointerException("schema is null");

        System.out.println("*DEBUG* - Instantiate JAXBContext");

        JAXBContext context = null;
        try {
            context = JAXBContext.newInstance(FichierCri.class);
        }
        catch (JAXBException e) {
            e.printStackTrace();
        }

        System.out.println("*DEBUG* - create unmarshaller");

        Unmarshaller unmarshaller = null;
        try {
            unmarshaller = context.createUnmarshaller();
        }
        catch (JAXBException e) {
            e.printStackTrace();
        }

        System.out.println("*DEBUG* - set schema to unmarshaller");

        unmarshaller.setSchema(schema);

        System.out.println("*DEBUG* - Create unmarshall object of type FichierCri");
        System.out.println("*DEBUG* - unmarshall from xml file");

        try {
            FichierCri unmarshall = (FichierCri) unmarshaller.unmarshal(xmlFile);
        }
        catch (JAXBException e) {
            e.printStackTrace();
        }
    }
}
下面是xml文件的一个示例:

<?xml version="1.0" encoding="UTF-8"?> 
<xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/> 
<xsi:schemaLocation="***\Desktop\Workspace_XML\fichier_cri\schema.xsd"/>

<crifile>
    <init_liaison>
        <source>value</source>
    </init_liaison>
    <liste_cri>
        <demande_cri>
            <fichier_erreur>value</fichier_erreur>
        </demande_cri>
    </liste_cri>
</crifile>

价值
价值

所有元素(crifile、liste_cri,…)都在架构中定义。

您的xml格式不正确,您的头应该是这样的(xmlns和schemaLocation是根节点的属性,而不是分隔的元素):


...
查看这些链接以了解更多信息:


您的xml格式不正确,您的头应该是这样的(xmlns和schemaLocation是根节点的属性,而不是分隔的元素):


...
查看这些链接以了解更多信息:


您拥有的XML格式确实不正确,您可以在线测试。XML提供的是XML正文中的多个根节点/元素,因为应该只有一个XML正文,xmlns:xsixsi:schemaLocation应该属于唯一的根元素,在您的例子中是cribile元素

更正后,您的XML应如下所示:

<?xml version="1.0" encoding="UTF-8"?> 
<crifile xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="PATH_TO_SCHEMA_FILE\schema.xsd">
    <init_liaison>
        <source>value</source>
    </init_liaison>
    <liste_cri>
        <demande_cri>
            <fichier_erreur>value</fichier_erreur>
        </demande_cri>
    </liste_cri>
</crifile>

价值
价值

其中指向模式文件的路径应替换为模式文件的正确位置。

您拥有的XML格式确实不正确,您可以在线测试。XML提供的是XML正文中的多个根节点/元素,因为应该只有一个XML正文,xmlns:xsixsi:schemaLocation应该属于唯一的根元素,在您的例子中是cribile元素

更正后,您的XML应如下所示:

<?xml version="1.0" encoding="UTF-8"?> 
<crifile xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="PATH_TO_SCHEMA_FILE\schema.xsd">
    <init_liaison>
        <source>value</source>
    </init_liaison>
    <liste_cri>
        <demande_cri>
            <fichier_erreur>value</fichier_erreur>
        </demande_cri>
    </liste_cri>
</crifile>

价值
价值

其中指向模式文件的路径应替换为模式文件的正确位置。

好的,谢谢,它有效地解决了我的问题,但我还有另一个问题。。。我用新的错误编辑了这个问题。你说“你应该提供xml格式”是什么意思?让我看看。顺便说一句,我并不是要包含那个文本。我现在已将其删除。:)我认为schema.xsd文件中存在问题。您确定在schema.xsd文件中正确定义了CRI文件吗?好的,谢谢,它有效地解决了我的问题,但是,我还有另一个问题。。。我用新的错误编辑了这个问题。你说“你应该提供xml格式”是什么意思?让我看看。顺便说一句,我并不是要包含那个文本。我现在已将其删除。:)我认为schema.xsd文件中存在问题。您确定在schema.xsd文件中正确定义了CRI文件吗?
<?xml version="1.0" encoding="UTF-8"?> 
<crifile xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="PATH_TO_SCHEMA_FILE\schema.xsd">
    <init_liaison>
        <source>value</source>
    </init_liaison>
    <liste_cri>
        <demande_cri>
            <fichier_erreur>value</fichier_erreur>
        </demande_cri>
    </liste_cri>
</crifile>