Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes XML and Related Technologies and the fly likes Inserting values in an XML doc Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Inserting values in an XML doc" Watch "Inserting values in an XML doc" New topic

Inserting values in an XML doc

Varek Boettcher

Joined: Aug 08, 2001
Posts: 7
Just by way of introduction - I'm a developer (surprise!), I'm not so new to Java but quite new to XML and JAXP.
FYI: JDK1.2.2_008 JAXP1.1 Xerces1.4.2 JDeveloper3.2
I'm writing an app that will take an existing data file and create an XML file from it. The hitch is that the nature of the data file will determine the structure of the document created.
I'm currently trying to use the XML file as a template. I load the data file and from a value in it I determine which XML file to load. I then want to insert the data values I have into the empty tags of the XML file. The problem is that I can't seem to find a method of the Node class that allows you to create a child node from thin-air. I'm using dom and dom.traversal and If I'm understanding what I've seen so far I need to create a #text child to hold the actual value of the tag in question...
I can clone a node but that requires an existing #text node. I've tried this in testing and it works but - there won't be an existing #text node to clone from in a real XML template.
Am I having a conceptual misunderstanding here or am I just missing something simple? How do I insert a value into an existing tag? Is DOM maybe the wrong tool for this?
Thanx for any help!
Varek Boettcher

Varek Boettcher
Ajith Kallambella

Joined: Mar 17, 2000
Posts: 5782
Here is something to get you started

Ajith Kallambella M.
Sun Certified Programmer for the Java�2 Platform.
IBM Certified Developer - XML and Related Technologies, V1.

Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
Varek Boettcher

Joined: Aug 08, 2001
Posts: 7
Thank you!
Your clue put me on the path of Document.createTextNode, which seems to be just the ticket!
So that others understand what I'm doing...
I'm filling empty XML tags from a csv file. The code I've ended up with is:

I'm using a TreeWalker with a filter to traverse the tag nodes...
I can't say I'm totally understanding this - but I'm getting there... Thanx again!

Varek Boettcher
rani bedi
Ranch Hand

Joined: Feb 06, 2001
Posts: 358
Can you tell us what is TreeWalker ?

Cheers,<br />Rani<br />SCJP, SCWCD, SCBCD
Varek Boettcher

Joined: Aug 08, 2001
Posts: 7
OK, I'll try.
WARNING: I'm a relative XML newbie here so I'm not very confident in my understanding of the technology or the tools. However, I find that my understanding of a topic increases when I have to attempt to explain it to someone and I'm sure someone else more knowledgable will cut in if I try to lead you too far astray...
The Traversal package defines a couple of nice ways to navigate a DOM document. The two main interfaces are the NodeIterator and the TreeWalker. The NodeIterator gives you a flat view of your document or a specific Node. You get a flat list of Nodes with no preserved hierarchy and methods to navigate up and down the list. The TreeWalker preserves the structure of a Document or sub-tree and also gives methods to navigate this tree.
One of the most powerful features (IMHO) is that the constructors for these interfaces can both make use of NodeFilters. You define a NodeFilter as part of the interface and then what you are navigating is actually a subset of the Document based on the NodeFilter you define.
The Traversal API is not currently supported in the current JAXP (1.1). It is supported in Xerces (Apache).
I'm currently learning this from "Professional Java XML" by WROX. So far I'm happy with this book.
Hope that helped...

Varek Boettcher
I agree. Here's the link:
subject: Inserting values in an XML doc
It's not a secret anymore!