Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / March 2004

Tip: Looking for answers? Try searching our database.

dom4j updating a file.

Thread view: 
jon g - 22 Mar 2004 16:12 GMT
I am new to dom4j...

I am using the SAXReader to get a handle to a document, and am able to
read it with no problem.
//i.e.
SAXReader reader = new SAXReader();
Document document = reader.read(myFile);

The problem is that now I want to update an attribute in that same XML
file.

This seems like it should be pretty simple, but I'm having trouble
finding a good example. I've been going through the API,  seems like
SAXWriter would do the trick, but I'm definitely missing something
here...

Any help or example would be appreciated.
Alan Meyer - 22 Mar 2004 23:52 GMT
> I am new to dom4j...
>
[quoted text clipped - 13 lines]
>
> Any help or example would be appreciated.

I haven't used dom4j so someone else can probably give you
better advice than I can, but in case nobody else jumps in, here
are some ideas.

SAX is a parser.  The concept behind it is that it makes a pass through
an XML document looking for standard types of objects - the start of
an element, end of an element, a text node, an attribute, etc.  You use it
by registering event handlers (references to callback routines) to be called
when the objects of interest to you are recognized by the parser.

SAX is a reasonable interface to use if you are going to read an XML
document and do something with the data - possibly create a new XML
document, or possibly just do things with the data you've read.

However if you want to read in an XML document, update it, and
write it out again, SAX is probably NOT the interface you want to
use.  The DOM ("document object model") interface is designed to
allow that.

SAX makes a single pass through a document, telling you what it finds.

DOM makes a pass through the document, builds an in-memory tree
of nodes, and gives you a reference to the top of the tree.  You can
then walk through it, reading nodes, text and attributes, and adding
deleting, or replacing nodes, text and attributes.  When you're done,
you "serialize" the tree, converting it back into a stream of characters
with "<" and ">" delimiters, etc.  Then you can write that to whatever
output sink you're interested in.

The DOM interface, a W3C standard, is complex and not always very
intuitive.  There is a learning curve to be climbed.  But it's useful and
ubiquitous.  You can find DOM implementations for Java, C++, Perl,
Python, and who knows what else.

Looking at the API docs for dom4j, they're mighty skimpy.  But
since DOM is a standard, you may be able to find documentation
at W3C, xml4c, or other places that will shed additional light on what
you see in dom4j.

Hope that helps.

   Alan
jon g - 29 Mar 2004 20:25 GMT
Thanks for your response, Alan.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.