From: Kala V. <kv...@ci...> - 2002-03-04 12:07:45
|
Hi, 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? 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 : emch-sp22-ultra# tclsh8.3 flatten.tcl REC-xml-20001006.xml can't read "config(wantElementDecls)": no such variable while executing "if {$config(wantElementDecls)} { $parser configure -elementdeclcommand [list ElementDeclaration $out] }" (procedure "Process" line 13) invoked from within "Process [read $ch] [expr {[file extension $filename] == ".dtd" ? "dtd" : "xml"}] stdout -baseurl file://[file join [pwd] $filename]" ("foreach" body line 6) invoked from within "foreach filename $argv { if {$filename == "-"} { Process [read stdin] xml stdout } else { set ch [open $filename] Process [read $ch] [..." ("default" arm line 2) invoked from within "switch [llength $argv] { 0 { if {![catch {package require Tk}]} { # Create a nice little GUI array set config {wantElementDecls 1 wantP..." (file "flatten.tcl" line 270) emch-sp22-ultra# Can you let me know what the problem is? Is there any detailed documentation for TCLXML implementation? Thanks in Advance, Kala |
From: Steve B. <Ste...@zv...> - 2002-03-04 20:08:21
|
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 |
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 > |
From: <tim...@as...> - 2002-03-05 16:32:20
|
Kala, I believe XML requires you to have one "root" tag -- ie the outermost tag. Your modified example has two. Try placing the <another> tag just after the <Inner> tag and see what happens. I've been reading "Beginning XML 2nd Edition (XML Schemas, SOAP, XSLT, DOM, and SAX 2.0" From WROX Press and find it to be a pretty good tutorial on XML and related technologies. Kala Veda wrote: > 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# -- Tim Davis |
From: Steve B. <Ste...@zv...> - 2002-03-05 20:45:03
|
Kala Veda wrote: > Similarly, can i write into an XML file using TCLXML? No - TclXML is only for reading XML documents. To write an XML document you will want to use TclDOM. 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 |