Reputation: 77
EDIT : thanks to Preetam Kumar, this problem is solved, but the error is elsewhere now. When I try to unmarshall my xml file again, I get this error telling me the element cannot be found :
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.]
However, I give the xml file the correct path to the schema but it seems it does not use it... I don't understand.
NB : I know the schema and xml tags doesn't match completely in the examples bellow, I already corrected it in my files.
These days I have to work with the JAXB java library to parse some XML files into Java Objects.
I made an XML Schema (.xsd) and used the tool xjc to compile it and generate my Java Classes. (see my question here for an example of the xsd file : schema.xsd )
Now, I am trying to test my schema and the generated classes by unmarshalling a custom XML file. And here is my problem. I constantly get an error that my xml is malformed right from the start, when I declare the namespace, as I have seen in numerous websites and examples :
<?xml version="1.0" encoding="UTF-8"?>
<xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
<xsi:schemaLocation="***\Workspace_XML\fichier_cri\schema.xsd"/>
I get the error :
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 "/>".]
I tried modifying the line and even removed it, but then I get another error :
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.]
right now, I don't know what to do anymore, so if someone have a hint or something, I won't say no...
I am not really confident in my Java test program either... but here it is :
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();
}
}
}
and here is a sample of the xml file :
<?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>
all the elements (crifile, liste_cri,...) are defined in the schema.
Upvotes: 0
Views: 2039
Reputation: 446
The XML you have is really malformed as you can test online here for validation. What your XML provides is multiple root nodes/elements in the XML body as there should be only one XML body and the xmlns:xsi and xsi:schemaLocation should belong to the only root element which in your case is the crifile element.
Your XML should look like shown below after correction:
<?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>
Where PATH_TO_SCHEMA_FILE should be replaced with the proper location of your schema file.
Upvotes: 1
Reputation: 161
Your xml is malformed, your header should look something like this (xmlns and schemaLocation are attributes of the root node and not separated elements):
<?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>
Check these links for more info:
how to reference local files for schemaLocation
Upvotes: 1