You are here: Re: TCL/PHP/XML problem: I need to convert an XML file into a TCL list « All PHP « IT news, forums, messages
Re: TCL/PHP/XML problem: I need to convert an XML file into a TCL list

Posted by Gerald W. Lester on 11/25/06 20:34

comp.lang.tcl wrote:
> My TCL proc, XML_GET_ALL_ELEMENT_ATTRS, is supposed to convert an XML
> file into a TCL list as follows:
>
> attr1 {val1} attr2 {val2} ... attrN {valN}

First off, sorry to everyone else for the long post, but I got tired reading
these threads.

Second off, the following does not return *EXACTLY* what you asked for it
returns more, but you should be able to convert it to what you ask for.

##
## A node will be the following list of name value pairs:
## NAME nodeName
## TEXT text
## ATTRIBUTES attributeNameValueList
## CHILDREN childNodeList
##
package require tdom

set xml {<?xml version="1.0" encoding="utf-8" ?>
<trivia>
<entry id="1101" triviaID="233" question="Who wrote &quot;Trilogy
of Knowledge&quot;?" answerID="1" correctAnswerID="1" answer="Believer"
expDate="1139634000"></entry>
<entry id="1102" triviaID="233" question="Who wrote &quot;Trilogy
of Knowledge&quot;?" answerID="2" correctAnswerID="1" answer="Saviour
Machine" expDate="1139634000"> </entry>
<entry id="1103" triviaID="233" question="Who wrote &quot;Trilogy
of Knowledge&quot;?" answerID="3" correctAnswerID="1" answer="Seventh
Avenue" expDate="1139634000"></entry>
<entry id="1104" triviaID="233" question="Who wrote &quot;Trilogy
of Knowledge&quot;?" answerID="4" correctAnswerID="1" answer="Inevitable
End" expDate="1139634000"></entry>
<entry id="1105" triviaID="233" question="Who wrote &quot;Trilogy
of Knowledge&quot;?" answerID="5" correctAnswerID="1" answer="No such song
existed" expDate="1139634000"></entry>
</trivia>
}

##
## Convert a node to a list
##
proc NodeToList {node} {
##
## Get the name and text value of the node
##
set name [$node nodeName]
set text [$node text]

##
## Get the attributes of the node
##
set attrList {}
foreach attribute [$node attributes] {
lappend attrList $attribute [$node getAttribute $attribute]
}

##
## Get the children of the node
##
set childrenList {}
foreach child [$node childNodes] {
if {![string equal [$child nodeType] TEXT_NODE]} then {
lappend childrenList [NodeToList $child]
}
}

##
## All done so return the list representing this subtree
##
return [list NAME $name TEXT $text ATTRIBUTES $attrList CHILDREN
$childrenList]
}

##
## Convert the XML to a DOM tree
##
dom parse $xml doc

##
## No get the root element
##
$doc documentElement root

##
## Convert the tree to a list
##
set results [NodeToList $root]
$doc delete


--
+--------------------------------+---------------------------------------+
| Gerald W. Lester |
|"The man who fights for his ideals is the man who is alive." - Cervantes|
+------------------------------------------------------------------------+

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация