【问题标题】:Malformed XML while unmarshalling with JAXB使用 JAXB 解组时出现格式错误的 XML
【发布时间】:2016-07-26 09:25:03
【问题描述】:

编辑:感谢 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 Schema (.xsd) 并使用 xjc 工具对其进行编译并生成我的 Java 类。 (有关 xsd 文件的示例,请参阅我的问题:schema.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: 2; columnNumber: 11; The type of element "xmlns:xsi" must be followed by attribut specifications, ">" or "/>".]

我尝试修改该行,甚至将其删除,但随后又出现另一个错误:

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、...)都在模式中定义。

【问题讨论】:

    标签: java xml xsd jaxb unmarshalling


    【解决方案1】:

    您拥有的 XML 格式确实不正确,因为您可以在线测试 here for validation。您的 XML 提供的是 XML 正文中的多个根节点/元素,因为应该只有一个 XML 正文,并且 xmlns:xsixsi:schemaLocation 应该属于唯一的根元素,在您的情况下是 crifile 元素。

    修改后的 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>
    

    应将 PATH_TO_SCHEMA_FILE 替换为架构文件的正确位置。

    【讨论】:

    • 好的,谢谢,它有效地解决了我的问题,但是,我遇到了另一个问题......我用新的错误编辑了这个问题。
    • 你应该提供xml格式是什么意思?
    • 让我看看。
    • 顺便说一句,我并不是要包含该文本。我现在已经删除了。 :)
    • 我认为您的 schema.xsd 文件有问题。您确定在您的 schema.xsd 文件中正确定义了 crifile 吗?
    【解决方案2】:

    您的 xml 格式错误,您的标头应如下所示(xmlns 和 schemaLocation 是根节点的属性,而不是单独的元素):

        <?xml version="1.0" encoding="UTF-8"?>  
        <root_element xmlns="uri to ns" 
                  xmlns:xsi="uri_to schema instance" 
                  xsi:schemaLocation="uri to schema location">
    ...
        </root_element>
    

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

    general xml tutorial in french - based on the "fichier_erreur" I guess this is your prefered language

    how to reference local files for schemaLocation

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-18
      • 2015-09-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多