XML
  Home arrow XML arrow Page 6 - Java and XML Basics, Part 3
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
XML

Java and XML Basics, Part 3
By: Liviu Tudor
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 22
    2004-04-20

    Table of Contents:
  • Java and XML Basics, Part 3
  • Which One is the Better One to Use?
  • Running the Parser
  • Problems with Big XML Files
  • Validating Parsers - DOM
  • Where do We Get a Validating Parser?
  • ErrorHandler
  • Validating Parsers - SAX
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Java and XML Basics, Part 3 - Where do We Get a Validating Parser?


    (Page 6 of 9 )

    JAXP specification requires that any parser conforming to the spec will be able to work in validating and non-validating mode and that the user should be able to dynamically specify whether parsing should perform validation as well or not. And to actually ask our parser to perform validity checking as well, all we have to do is ad one line to the code -- ValidatingDOMParser.java (OK, four, including the comment -- happy now?):


    /**
     * First instantiate a new factory which we'll use
     * to create the DOM parser
     */

    try
    {
     factoryBuilder 
    DocumentBuilderFactory.newInstance();
    }
    catch
    FactoryConfigurationError fce )
    {
     System
    .err.println"Error creating DOM parser factory:" );
     fce
    .printStackTrace();
     System
    .exit( );
    }
    /**
     * Require that the parsing process will do validation as well
     */

    factoryBuilder
    .setValidatingtrue );

    Now if we compile and run this example, we will get (nearly) what we expected -- an error! (Hmmm, I doubt really there are many programmers out there who will expect -- and pray for -- an error when they run their programs.):

    java -classpath "%CLASSPATH%;." ValidatingDOMParser employee.xml

    Warning: validation was turned on but an org.xml.sax.ErrorHandler was not set, which is probably not what is desired.  Parser will use a default ErrorHandler to print the first 10 errors.  Please call the 'setErrorHandler' method to fix this.

    Error: URI=null Line=14: Attribute value for "name" is #REQUIRED.
    Parsing successful!

    Apart from the fact that the program reports that the compilation went well, we got our error. Let's not worry for the moment about what the whole error message means and why the parsing continued afterward and look only at the line which says that there is something wrong with the name attribute - more specifically in our case, it is missing! In order to check that the problem only relies with the name attribute, we will change the employee.xml file back again by adding the name attribute and then run the program again:


    <?xml version="1.0" encoding="UTF-8"? >
    <!DOCTYPE Employees [
    <!ELEMENT Employees (Employee)*>
    <!ELEMENT Employee EMPTY>
    <!ATTLIST Employee
      name  CDATA 
    #REQUIRED
      surname CDATA #REQUIRED
      dob  CDATA #IMPLIED
      email  CDATA #IMPLIED
      phone  CDATA #IMPLIED
      address CDATA #IMPLIED>
    ]>
    <Employees>
     <Employee name="Liviu" surname="Tudor" dob="14/02/1975" email="user@domain.com" address="Coocooland"/>
     <Employee name="Janet" surname="Jackson" dob="unpolite to reveal" email="janet@rhythmnation.com" address="Really really secret ;-)"/>
    </Employees>

    Result:

    java -classpath "%CLASSPATH%;." ValidatingDOMParser employee.xml
    Parsing successful!

    Of course, you can try "unvalidating" the XML file as much as you like (for example add a new element type under Employees that is no declared in the DTD) and you will see an error message every time.

    As a side task, you can have a look at all the set functions in the DocumentBuilderFactory class to check what other functionality a JAXP-compliant parser has to offer.

    More XML Articles
    More By Liviu Tudor


     

    XML ARTICLES

    - Datatypes and More in RELAX NG
    - Providing Options in RELAX NG
    - An Introduction to RELAX NG
    - Path, Predicates, and XQuery
    - Using Predicates with XQuery
    - Navigating Input Documents Using Paths
    - XML Basics
    - Introduction to XPath
    - Simple Web Syndication with RSS 2.0
    - Java UI Design with an IDE
    - UI Design with Java and XML Toolkits
    - Displaying ADO Retrieved Data with XML Islan...
    - Widget Walkthrough
    - Introduction to Widgets
    - The Why and How of XML Data Islands






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
    Stay green...Green IT