This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes XML and Related Technologies and the fly likes can i use DOM.. to see if xml is well formed Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "can i use DOM.. to see if xml is well formed " Watch "can i use DOM.. to see if xml is well formed " New topic

can i use DOM.. to see if xml is well formed

lavnish lalchandani
Ranch Hand

Joined: Feb 28, 2007
Posts: 79
I am getting a string from user and need to check whether its well formed
( no need to validate against schema )
for the same i am using the following code ...

but its using SAX ... which "principally" is advisable to use for big xml files ...
I am aware that i will be getting a small xml file from user

two questions here
(i) Which way is better shud i use DOM or SAX ? why ? ( for my usecase i think DOM )
(ii) saw the APIs for DocumentBuilder.parse( ) as i am getting a String object from user cant give input as File , URI , StringBufferInputStream (deprecated) .. so is using SAX the only way ?
[ April 15, 2008: Message edited by: lavnish lalchandani ]
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 12759
Personally I would use SAX just to save time and memory. It is important to note that you can get useful information about the position of a parsing error out of a SAXParseException so I would catch that first.

Being able to tell your user the line and column of the error could be very helpful.

Note that DOM parsers really use a SAX parser underneath.

I agree. Here's the link:
subject: can i use DOM.. to see if xml is well formed
Similar Threads
about charset on XML Dom Object
Creating JDOM Document from a String
Parsing input from a string using JAXP, Xerces 1.4.1 with Visual Age
Different behaviours plz help!!
XML in applet is not working......:-(