You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-users@xerces.apache.org by Jean-Noël Colin <jn...@gmail.com> on 2010/02/02 13:22:31 UTC
Help generating and validating an xml document
Hi
I'm quite new to xerces, and having problems with basic operations: I have defined an XML Schema (xsd) with a specific namespace for the defined types and elements.
Then, I need to create an XML document (by adding nodes, etc), and then, to validate the document against the schema.
Using Oxygen, I manage to generate the following XML sample from the schema. It properly uses namespaces (although I'm wondering whether ns1: prefix should not also appear in front of all elements.
<?xml version="1.0" encoding="UTF-8"?>
<ns1:position xmlns:ns1="http://info.fundp.ac.be/infob318/2009/projetg"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://info.fundp.ac.be/infob318/2009/projetg file:/Users/jnc/Documents/workspace/GServer/WebContent/WEB-INF/gserver.xsd">
<longitude>0</longitude>
<latitude>0</latitude>
<timeStamp>-1073741773</timeStamp>
</ns1:position>
Using xerces, I'm getting the following:
<?xml version="1.0" encoding="UTF-16"?>
<position xmlns="http://info.fundp.ac.be/infob318/2009/projetg">
<longitude>3.5</longitude>
<latitude>4.5</latitude>
<altitude>5.5</altitude>
<timestamp>1265112364510</timestamp>
</position>
So i have several questions:
why doesn't xerces include also the other schema (xsi) and schemaLocation?
why are the namespace definitions included in the element and not in the root (<?xml...)
The code used to generate the above xml is presented below, and I'm attaching the .xsd
Thanks a lot for your help
Best regards
Jean-Noël Colin
DocumentBuilder docBuilder = null;
try {
docBuilder = factory.newDocumentBuilder();
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
document = docBuilder.newDocument();
Element positionElement = document.createElementNS("http://info.fundp.ac.be/infob318/2009/projetg", "position");
Element element;
element = document.createElement("longitude");
element.setTextContent(String.valueOf(3.5));
positionElement.appendChild(element);
element = document.createElement("latitude");
element.setTextContent(String.valueOf(4.5));
positionElement.appendChild(element);
element = document.createElement("altitude");
element.setTextContent(String.valueOf(5.5));
positionElement.appendChild(element);
element = document.createElement("timestamp");
element.setTextContent(String.valueOf(Calendar.getInstance()
.getTimeInMillis()));
positionElement.appendChild(element);
document.appendChild(positionElement);