Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Question regarding XML Parsing

  1. #1
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    North Wales
    My Mood
    Thanked 115 Times in 106 Posts
    Blog Entries

    Default Question regarding XML Parsing

    I've recently received an assignment to parse an XML file using the SAX Parser.
    I've already sub-classed the DefaultHandler class to take control of the startElement etc. methods and it works fine.
    My only question is, what is the general practice used to parse XML files?
    • is it normal to simply develop the parser to match the contents of the XML file (XML structure known), as in; if the file contains 4 elements of an invoice, I take the 4 elements and pass them to the correct parameters etc.

    • Should it be developed so that it works with whatever XML file I throw at it where the actual structure isn't known?

    Apologies if it's possibly a rather a dopey question, just I wouldn't want to feel like my code was "cheating" if I did the first way, if the second is the way to go about it.

    (Please bare in mind it is an Assignment and nothing proffessional).
    Kind regards, Newbie.
    Last edited by newbie; February 19th, 2011 at 12:01 PM.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Thanked 833 Times in 772 Posts
    Blog Entries

    Default Re: Question regarding XML Parsing

    Working with whatever sort of xml you throw at it is in part the job of the xml parser - it's independent of the contents of the xml. So I guess the answer depends upon what you want to do with the contents of the xml in the end: you can readily generalize code to save the data in another format, but (for example) generalizing GUI code to display any xml data could potentially get complicated, and typically there is a specific goal in mind where generalization is not the point to meet project goals and deadlines. My .02

  3. The Following User Says Thank You to copeg For This Useful Post:

    newbie (February 20th, 2011)

  4. #3
    Forum old-timer
    Join Date
    Nov 2008
    Faversham, Kent, UK
    My Mood
    Thanked 58 Times in 54 Posts

    Default Re: Question regarding XML Parsing

    When using a SAX parser, the parser will step through the XML, calling back to your 'start' and 'end' parsing methods for the start and end of each element in the XML input. In those methods, you check which element has been passed to you (usually by tag name) and take appropriate action. So although the parser is handling the XML in a generic way, your parsing methods are specific to the XML definition you're using. If the XML is structured hierarchically (nested elements & lists, etc.), you often need to keep track of the current state between callbacks, which takes some care.

    The alternative DOM (Document Object Model) parser reads all the XML input in one go, and gives you random access to any element, which makes it easier to use, but it takes time and needs enough memory to read the whole XML document.

Similar Threads

  1. Java XML Parsing
    By nimilc2002 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 9th, 2011, 11:00 AM
  2. Parsing urls
    By Riddhi Sharma in forum Java Theory & Questions
    Replies: 2
    Last Post: January 25th, 2011, 10:06 AM
  3. Parsing CDATA
    By Sai in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 9th, 2010, 12:33 AM
  4. Printing xml to the console from .wmdb without printing junks
    By John in forum File I/O & Other I/O Streams
    Replies: 9
    Last Post: April 24th, 2009, 03:44 AM
  5. [SOLVED] Parsing ID3 tags from mp3
    By John in forum Java Theory & Questions
    Replies: 14
    Last Post: April 16th, 2009, 01:36 PM