|
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
|