Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-11-2007, 12:41 AM
Administrator
 
Join Date: Dec 2006
Posts: 649
JavaForums has disabled reputation
Parsing XML Documents-1 (DOM and SAX)
XML files are very useful. Usually they are used to store configuration settings. We can also use XML files to store data so it can be used as a database. A lot of work has been done in this domain and its really very useful working with XML files.
Now suppose you have a XML file with some useful data.
college.xml
Shoaib Malik 22 Waseem Akram 36

We need to parse and get the data from xml file. We have a class called Student with first, last and age attributes. These attributes are declared private and we have public getter/setters. We want to get data from XML document and save the data to Java objects.
public class Student { private String first; private String last; private int age; public String getFirst() { return first; } public void setFirst(String first) { this.first = first; } public String getLast() { return last; } public void setLast(String last) { this.last = last; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }

One was is to create a File object and then use BufferedReader to read the file. Then we can use StringTokenizer to search for the required tokens and we can get the required text.
This way will work but Java provides a better way to parse XML documents. We can use DOM (Document Object Model) or SAX (Simple API for XML) to parse the XML file in an efficient way.
DOM parser creates a tree in memory to represent XML document so it takes a lot me memory. Even of you want to get data from one tag, whole file will be mapped to a tree in the memory. SAX parses the element as node in encountered.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Parsing URL in Java Java Tip java.net 0 04-07-2008 10:14 PM
Using SAXParserFactory for parsing XML Java Tip Java Tips 0 03-20-2008 11:42 AM
Parsing URL Java Tip Java Tips 0 12-26-2007 12:16 PM
Parsing XML documents using DOM JavaForums Java Blogs 0 11-19-2007 06:30 PM
Internet Explorer is unable to open Office documents from an SSL Web site pramod@netedgecomputing.c Web Frameworks 0 07-19-2007 09:18 AM


All times are GMT +3. The time now is 10:42 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org