Rechercher dans ce site

XML Parsing in Java part 2

>> 13 June 2010

Using only the JDK (nothing to install)

Validating or not validating?

For parsing XML it's not at all necessary to validating document before. But what if your document does not have the structure you think it has? Although it's possible to check all what you get as elements or child. It's simpler to provide a DTD of the document and let Java check  the correct structure of the document.

DTD basic rules

Supplying a DTD, there are some methods to provide a DTD for instance :

You can include a DTD in an XML document
Use SYSTEM and give the path to an external file

For our example of Eclipse Template file, put the DTD directly after the xml declaration

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE templates [
   <!ELEMENT templates (template)+>

  • The document type must match the name of the root element (templates)
  • Elements are specified using regular expression. Here for instance "templates" contains 1 or more template.

  • Each template (Child element) contains "Text"

<!ELEMENT template (#PCDATA) >

  • And attributes like context :

<!ATTLIST template context (javadoc|java) "java">

Example of a DTD (This is just an example of a DTD for templates in Eclipse)
<!DOCTYPE templates [
   <!ELEMENT templates (template)+>
   <!ELEMENT template (#PCDATA) >
   <!ATTLIST template autoinsert (true|false) #REQUIRED>
   <!ATTLIST template context (javadoc|java) #REQUIRED>
   <!ATTLIST template deleted (true|false) #REQUIRED>
   <!ATTLIST template description CDATA #REQUIRED>
   <!ATTLIST template enabled  (true|false) #REQUIRED>
   <!ATTLIST template id ID #IMPLIED>
   <!ATTLIST template name CDATA #REQUIRED>

What a bout Java?
When you provide a DTD for your XML file call the method setValidating(true)
On an object of DocumentBuilderFactory

One more step. Call also setIgnoringElementContentWhitespace(true), if you don't want to get exceptions. This way the parser stops to report white-spaces between elements.

Example :
try {
    DocumentBuilderFactory     documentBuilderFactory=DocumentBuilderFactory.newInstance();
catch(Exception e)


Post a Comment

  © Blogger template Simple n' Sweet by 2009

Back to TOP