使用 JAXB 解组时格式错误 XML
Malformed XML while unmarshalling with JAXB
编辑: 感谢 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 文件的示例: )
现在,我正在尝试通过解组自定义 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、...)都在架构中定义。
你的 xml 格式不正确,你的 header 应该看起来像这样(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>
查看这些链接以获取更多信息:
你的XML确实是格式错误,你可以在线测试here for validation。您的 XML 提供的是 XML 正文中的多个根 nodes/elements,因为应该只有一个 XML 正文和 xmlns:xsi 和 xsi: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 应替换为架构文件的正确位置。
编辑: 感谢 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 文件的示例:
现在,我正在尝试通过解组自定义 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、...)都在架构中定义。
你的 xml 格式不正确,你的 header 应该看起来像这样(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>
查看这些链接以获取更多信息:
你的XML确实是格式错误,你可以在线测试here for validation。您的 XML 提供的是 XML 正文中的多个根 nodes/elements,因为应该只有一个 XML 正文和 xmlns:xsi 和 xsi: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 应替换为架构文件的正确位置。