Subject: Re: any experience on TclDOM - DN [1]


Steve Ball <Steve.Ball@zveno.com> - 20 Jan 2000 - comp.lang.tcl

 Jack Cheng wrote:
 >
 > Any one got some good examples on using TclDOM ?

 I have uploaded my TclXML tutorial to www.fatbrain.com
 for those who aren't going to be at the Tcl2k tutorial.
 It includes topics such as TclDOM and TclXML.

 > Right now, I can have it parse a XML file and build a tree. But I've trouble
 > in accessing the nodes of the tree except the Root node. I know this is from
 > my lack of understanding in how to use it.
 >
 > I will appreciate someone show me how to traverse the an already built tree
 > and access the attributes of element.

 To briefly answer your question, once you have the root node
 the next most important thing is the document element.
 The -documentElement option returns that.  Eg:

 set doc [dom::DOMImplementation parse $xml]
 set docEl [dom::document cget $doc -documentElement]

 Once you have the document element, you can traverse the children
 in the usual DOM way.  The -childNodes option returns a *variable*
 that contains the children, or the 'children' method returns
 a static list (this latter method is not standard DOM).  Eg:

 set childList [dom::node cget $docEl -childNodes]
 foreach child [set $childList] {
     # Process the child node
 }

 foreach child [dom::node children $docEl] {
     # Process the child node
 }

 Similarly, for attributes there is the -attributes option that
 returns an array variable containing the attribute names
 (the indices of the array) and their values.  Eg.

 set attrList [dom::element cget $docEl -attributes]
 foreach {name value} [array get $attrList] {
     # Process attribute
 }

 The getAttribute option can retrieve individual attribute values.

 Probably the best pieces of code I've got to demonstrate this
 are the DOMWidgets - DOMText and TreeDOM.  However, they're
 not exactly simple examples.

 Hope that helps,
 Steve Ball

 --
 Steve Ball            |   Swish XML Editor    | Training & Seminars
 Zveno Pty Ltd         |   Web Tcl Complete    |      XML XSL
 http://www.zveno.com/ |    TclXML TclDOM      | Tcl, Web Development
 Steve.Ball@zveno.com  +-----------------------+---------------------
 Ph. +61 2 6242 4099   | Mobile (0413) 594 462 | Fax +61 2 6242 4099

Last modified
2000-02-10

(195.108.246.50)

Note: you are looking at
the snapshot of an old wiki
- much of this information
is likely to be very outdated