Problems using rev-XML functions
Jim Lyons
jimlyons at earthlink.net
Tue Jan 20 13:33:34 EST 2004
I've been investigating rev's built in functions for parsing XML using
this simple XML file (the angle brackets are shown as parens for this
message):
(root)
(record id="1")
(fld1)Harry(/fld1)
(fld2)Ron(/fld2)
(/record)
(record id="2")
(fld1)Fred(/fld1)
(fld2)George(/fld2)
(/record)
(/root)
The two main functions for parsing XML are revCreateXMLTree, which
takes the XML source as a parameter, and revCreateXMLTreeFromFile,
which gets the XML from a file. Besides the source, there are three
other parameters, all boolean switches. The first switch is whether to
bail or not if an error in syntax occurs; the second is whether you
want the function to build a tree in memory for you and return a
pointer to it; the last is whether you want messages sent as the
parsing proceeds. You'll want to set one of the last two switches true
or the function will only tell you whether there was an error in syntax.
There are basically two ways to use these functions, controlled by the
last two switches: create a tree in memory and then use the other
rev-XML functions to traverse and interrogate it; or have the parser
send messages as it goes, like revStartXMLNode, revStartXMLData, etc.
Here is what I found out.
1. The TD says that revStartXMLTree and revEndXMLTree messages will be
sent when the parsing begins and ends; they are not. I noticed a
reference in the "See Also" menu to "revXMLStartTree" but no entry for
it, so on a hunch I tried that and it worked: the functions (Rev 2.1.2)
send revXMLStartTree and revXMLEndTree. I filed this as a documentation
bug (#1165), but probably they meant to change the names as the docs
say; guess they will sort it all out.
2. There is a difference in the messages sent depending on if you also
create the tree. I put simple handlers similar to this for all the
messages:
on revStartXMLNode attr
put "start node" && attr & return after msg
end revStartXMLNode
Here is the sort of thing we get from either function with create_tree
false and send_messages true:
start tree:
start node: root
start data:
start node: record
start data:
start node: fld1
start data: Harry
end node fld1
start data:
start node: fld2
start data: Ron
end node fld2
start data:
end node record
....
But with create_tree true and send_messages true we get the much
cleaner:
start tree:
start node: root
start node: record
start node: fld1
start data: Harry
end node fld1
start node: fld2
start data: Ron
end node fld2
end node record
....
Obviously this is not a big problem, but it just don't seem right. I
don't need the tree, but I wish I didn't have to deal with extra start
data messages....
3. The TD says the message "revStartXMLnode nodeAttributes" will get
sent when the parser encounters an opening tag, where nodeAttributes
"is a string containing the attributes of the XML element currently
being parsed, one attribute per line." The reason I started playing
with this is because this doesn't make clear how you would know what
the tag is. Notice in the test source above that when (record id="1")
is parsed the parameter sent with the start node message is just the
tag, "record". Where's the id attribute? I didn't write a bug report
about this because I'm not sure I understand how it's supposed to work.
So can anyone provide any help, understanding, advice, or warnings
about any of this? Thanks, and sorry for the long message.
Jim Lyons
More information about the use-livecode
mailing list