XML
  Home arrow XML arrow Page 3 - Java and XML Basics, Part 1
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 
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 1
By: Liviu Tudor
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 46
    2004-03-08

    Table of Contents:
  • Java and XML Basics, Part 1
  • The javax.xml.parsers Java Package
  • Parsing Using JAXP and the DocumentBuilder
  • Traversing the DOM
  • Appendix: Installing Xerces-J 2.0.0 under JDK 1.4

  • 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 1 - Parsing Using JAXP and the DocumentBuilder


    (Page 3 of 5 )

    Using the steps described above, let’s have a closer look at what our code would look like (SimpleDOMParser.java). Disregarding the try/catch blocks and the comments, it’s just as simple as this:


    DocumentBuilderFactoty factoryBuilder DocumentBuilderFactory.newInstance( );
     
    DocumentBuilder builder factoryBuilder.newDocumentBuilder();
     
    Document doc builder.parsefileName );

    As you can see, the process of creating a parser and the entire (sometimes ulgy) bunch of classes hidden behind each parser are transparent to the programmer, as we’re only working at the interface level and we’re not worried about the whole “behind the scenes” process.

    Assuming that we launch this program against the following piece of XML (simple1.xml):


    <?xml version="1.0" standalone="no"? >
    <
    applog>
      
    <session type="manual" date="12/12/2003">
        
    <duration>01:00:00</duration>
        
    <files>7</files>
        
    <application>notepad.exe</application>
        
    <comments>Started by the administrator to edit some config files.</comments>
      
    </session>
      <
    session type="automatic" date="13/12/2003">
        
    <duration>00:10:00</duration>
        
    <distance>37</distance>
        
    <application>grep.exe</application>
        
    <comments>Probably part of one of the maintenance scripts.</comments>
      
    </session>
    </applog >

    We will obtain the following result:


    java -classpath "%CLASSPATH%;." SimpleDOMParser simple1.xml
    Parsing successfull
    !

    This means that we have instantiated our XML parser and parsed the XML document successfully. Of course, at this stage we will have to “take its word” for it, as we haven’t put in place any way of actually dumping the result Document on the screen; however, if we want to test our parser, we can change the XML document in such a way that is no longer a well-formed XML structured (simple2.xml):


    <?xml version="1.0" standalone="no"? >
     
    <
    applog>
      
    <session type="manual" date="12/12/2003">
        
    <duration>01:00:00<duration>
        
    <files>7</files>
        
    <application>notepad.exe</application>
        
    <comments>Started by the administrator to edit some config files.</comments>
      
    </session>
     
      <
    session type="automatic" date="13/12/2003">
        
    <duration>00:10:00</duration>
        
    <distance>37</distance>
        
    <application>grep.exe</application>
        
    <comments>Probably part of one of the maintenance scripts.</comments>
      
    </session>
    </applog >

    If we run our program against it, we get the following:


    Parsing error:
    org
    .xml.sax.SAXParseExceptionExpected "</duration>" to terminate element starting on line 5.
     at org
    .apache.crimson.parser.Parser2.fatal(Unknown Source)
     at org
    .apache.crimson.parser.Parser2.fatal(Unknown Source)
     at org
    .apache.crimson.parser.Parser2.maybeElement(Unknown Source)
     at org
    .apache.crimson.parser.Parser2.content(Unknown Source)
     at org
    .apache.crimson.parser.Parser2.maybeElement(Unknown Source)
     at org
    .apache.crimson.parser.Parser2.content(Unknown Source)
     at org
    .apache.crimson.parser.Parser2.maybeElement(Unknown Source)
     at org
    .apache.crimson.parser.Parser2.content(Unknown Source)
     at org
    .apache.crimson.parser.Parser2.maybeElement(Unknown Source)
     at org
    .apache.crimson.parser.Parser2.parseInternal(Unknown Source)
     at org
    .apache.crimson.parser.Parser2.parse(Unknown Source)
     at org
    .apache.crimson.parser.XMLReaderImpl.parse(Unknown Source)
     at org
    .apache.crimson.jaxp.DocumentBuilderImpl.parse(Unknown Source)
     at javax
    .xml.parsers.DocumentBuilder.parse(Unknown Source)
     at SimpleDOMParser
    .main(SimpleDOMParser.java:92)

    More XML Articles
    More By Liviu Tudor


       · good one for beginers....
     

    XML ARTICLES

    - Using Regions with XSL Formatting Objects
    - Using XSL Formatting Objects
    - More Schematron Features
    - Schematron Patterns and Validation
    - Using Schematron
    - 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







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek