You are viewing a plain text version of this content. The canonical link for it is here.
Posted to general@xerces.apache.org by Eric SCHAEFFER <es...@posterconseil.com> on 2000/03/15 12:05:17 UTC

Xerces document validation (J 1.0.1)

Hello,

I use Xerces J 1.0.1 (not the last version, I know, but it shouldn't make
differences), and want to create an XML document "on the fly" and to
validate it with a DTD. But it doesn't work
:-(

If I construct my document by programmation, I can't tell the document that
it is finished (and that all requiered elements should be present).
Thus, I try to parse the document that I've created to validate it. But it
still doesn't work...

Can you help me, please ?

Thank's a lot,
Eric.

_______________________
Here is my DTD :
_______________________

<?xml version="1.0" encoding="UTF-8"?>

<!-- DTD : Territoire PosterScan -->

<!ELEMENT territoire-posterscan (name, region+)>
<!ELEMENT region (name, district+)>
<!ELEMENT district (name, zone+)>
<!ELEMENT zone (name, commune+)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT commune (insee)>
<!ELEMENT insee (#PCDATA)>

_______________________
Here is what I do :
_______________________

 org.apache.xerces.dom.DocumentTypeImpl docType =
  new org.apache.xerces.dom.DocumentTypeImpl(
   null,
   "territoire-posterscan",
   null,
   "http://localhost/dtd/territoire_posterscan.dtd"
   );
 org.apache.xerces.dom.DocumentImpl doc = new
org.apache.xerces.dom.DocumentImpl(docType);
 org.w3c.dom.Node terposter = doc.createElement("territoire-posterscan");
 doc.appendChild(terposter);
 org.w3c.dom.Node reg = doc.createElement("region");
 terposter.appendChild(reg);
 org.w3c.dom.Node dist = doc.createElement("district");
 reg.appendChild(dist);

/*
If I stop here, validation is Ok, even if the doc isn't conform to the DTD,
because I can't tell the doc that I've finished
Thus, I try to parse it, to force validation
*/

 StringWriter writer = new StringWriter();
 org.apache.xml.serialize.OutputFormat outputFormat = new
org.apache.xml.serialize.OutputFormat(doc);
 outputFormat.setOmitXMLDeclaration(false);
 outputFormat.setDoctype(null,
"http://localhost/dtd/territoire_posterscan.dtd");
 org.apache.xml.serialize.XMLSerializer serializer = new
org.apache.xml.serialize.XMLSerializer(writer, outputFormat);
 serializer.serialize(doc);
 org.apache.xerces.parsers.DOMParser parser = new
org.apache.xerces.parsers.DOMParser();
 parser.setFeature("http://xml.org/sax/features/validation", true);

parser.setFeature("http://apache.org/xml/features/dom/create-entity-ref-node
s", false);

parser.setFeature("http://apache.org/xml/features/validation/warn-on-duplica
te-attdef", true);

parser.setFeature("http://apache.org/xml/features/validation/warn-on-undecla
red-elemdef", true);

parser.setFeature("http://apache.org/xml/features/dom/defer-node-expansion",
true);
 parser.parse(new org.xml.sax.InputSource(new
StringReader(writer.toString())));

/* and it's still Ok... */

_______________________________________

Eric SCHAEFFER
eschaeffer@posterconseil.com

POSTER CONSEIL
118 rue de Tocqueville
75017 PARIS
FRANCE
Tel. : 33-140541058
Fax : 33-140541059
_______________________________________

----------------------------------------------------------------------------
-----------------------------
 Come to the first official Apache Software Foundation Conference!
-----------------------------------
http://ApacheCon.Com ------------------------------------