【发布时间】: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