From: Kala V. <kv...@ci...> - 2002-03-05 12:14:38
|
Hi, Thanks a lot for your time. I have some more doubts. In the script using TCLXML, variable $xml is parsed to the parser object. Can this $xml be an xml file name? Or should it be a string. I tried to run the script by defining the $xml variable with different options, set xml "<Outer><Inner> Testing </Inner></Outer>" I am able to retrieve the Inner tag value without any problem, but if i add another tag to the same string, say, set xml "<Outer><Inner> Testing </Inner></Outer><Another>AnotherTag</Another>" then, i am getting an error as follows : emch-sp22-ultra# tclsh8.3 test2.tcl error "junk after document element" at line 1 character 91 while executing "$p parse $xml" (file "test2.tcl" line 35) emch-sp22-ultra# Similarly, can i write into an XML file using TCLXML? Thanks in Advance Kala At 07:08 AM 3/5/2002 +1100, Steve Ball wrote: >Kala Veda wrote: >> I need to parse the xml document using a TCL Function, by passing a >> tag name to retrieve the value of the tag. Something similar to >> "Simple.pm" in perl. For example i have an example xml document : >><Outer> >> <Inner> >> Testing >> </Inner> >></Outer> >><Another> >> AnotherTag >></Another> >> Say to retrive the value contained in the tag "Another", i should >> pass "Another" to a TCL function, which should retreive me "AnotherTag". >> How can i use TCLXML to implement this? > >There are two ways to do this, using TclXML or by using TclDOM. > >If all you want to do is extract the value from that one element >then TclXML can do that by using an element handler. You need to >know about the start of the desired tag, capture its content >and then stop capturing when the end tag occurs. For example: > >package require xml >set p [xml::parser -elementstartcommand EStart \ > -elementendcommand EEnd \ > -characterdatacommand Data] > >proc EStart {tag attrs args} { > global capture > > if {[string equal $tag "Another"]} { > set capture 1 > } >} >proc EEnd {tag args} { > global capture > > if {[string equal $tag "Another"]} { > set capture 0 > } >} >proc Data data { > global capture pcdata > > if {$capture} { > append pcdata $data > } >} > >set pcdata {} >set capture 0 >$p parse $xml > >puts "captured data is \"$pcdata\"" > >------- > >Now, using TclDOM v2.0 with its XPath support is a little >easier: > >package require dom 2.0 > >set doc [dom::DOMImplementation parse $xml] > ># First get the desired element. ># This will return a list (nodeset) of all elements ># that match the expression. >set element_list [dom::document selectNode $doc //Another] >set one_element [lindex $element_list 0] > ># Now get its string value (the textual content). > >puts "element content is \"[dom::node stringValue $one_element]\"" > >> Moreover, i tried to run the flatten.tcl with the sample xml file >> given in the examples, it is giving an error. The error is as follows : > >Please submit a SF bug report. > >> Can you let me know what the problem is? Is there any detailed >> documentation for TCLXML implementation? > >There is the reference documentation (ie. the man page) on the >website. I should probably write up a "roadmap" for the >Tcl parser implementation. Why don't you submit a tracker >item on SF for that too? > >Cheers, >Steve Ball > >-- >Steve Ball | XSLT Standard Library | Training & Seminars >Zveno Pty Ltd | Web Tcl Complete | XML XSL Schemas >http://www.zveno.com/ | TclXML TclDOM | Tcl, Web Development >Ste...@zv... +---------------------------+--------------------- >Ph. +61 2 6242 4099 | Mobile (0413) 594 462 | Fax +61 2 6242 4099 > |