Home arrow XML arrow Page 3 - Java and XML Basics, Part 1
XML

Java and XML Basics, Part 1


In a previous article (XML Basics Part One);we had a brief look at XML. However, as stated in the article, XML itself is worth nothing without the set of APIs that are generated-it would simply be just another fancy form of CSV, that is, a proprietary data format! My aim in this set of articles is not to actually reveal XML in its every detail, but rather have a look at the implications of XML in today’s technologies. That’s why the previous article only sheds a little light into the insights of XML, which included some terms and technologies you will be confronted with the moment you step in the XML arena.

Author Info:
By: Liviu Tudor
Rating: 5 stars5 stars5 stars5 stars5 stars / 48
March 08, 2004
TABLE OF CONTENTS:
  1. · Java and XML Basics, Part 1
  2. · The javax.xml.parsers Java Package
  3. · Parsing Using JAXP and the DocumentBuilder
  4. · Traversing the DOM
  5. · Appendix: Installing Xerces-J 2.0.0 under JDK 1.4

print this article
SEARCH DEVARTICLES

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)


blog comments powered by Disqus
XML ARTICLES

- Open XML Finally Supported by MS Office
- XML Features Added to Two Systems
- 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

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials