|
Introduction Overview Installation Initiating the Process Small Site Tutorial Elements In Depth... Script Demos •Sample XML Processor In-line Math Site Content in Single File Platform Specific... Glossary History Support Downloads Support the good work: support email |
Examples : Sample XML Processor. The XML data is found in the file called "`sampleXML.xml" in the same location as this page in the Elements siteSrc. The XML file has a elementFilePrefix (see ElementsSettings) at the beginning of its name which tells Elements to read the file and create an element (see Glossary) out of it. As an element, it is available to scripts. The XML processing script is defined in the pages's element block (see Glossary). See the scripts page for details on how scripts operate. See the Python docs for more info on xml. The xml processing script looks like this:
#.sampleXMLProcessorCode#
def buildCategories():
import xml.dom.minidom
s = []
categoriesDom = xml.dom.minidom.parseString(string.join(sampleXML, '\n'))
categoriesNodes = categoriesDom.documentElement.childNodes
for category in categoriesNodes: # build the category array
if category.nodeType == xml.dom.minidom.Node.ELEMENT_NODE:
id = category.getAttribute('id')
name = category.getAttribute('name')
s.append('<tr><td align="right">'+ id +'</td><td>'+ name +'</td></tr>')
categoriesDom.unlink() # clean up
return string.join(s, "\n")
#
Note: An important detail to note in this script is that xml.dom.minidom.parseString() expects a string to parse, but, as the scripts page says, multiLineElements are internally represented as a list of strings, one string per line. To use the sampleXML element in the minidom.parseString(), we first convert it to a string like so:
string.join(sampleXML, '\n') The XML looks like this: <?xml version="1.0"?> <categories> <category id="1" name="Activism" /> <category id="2" name="Petitions" /> <category id="3" name="Peace" /> <category id="4" name="Anti-globalization" /> <category id="5" name="Contact Elected Officials" /> <category id="6" name="Capitalism" /> <category id="7" name="Effects on Environment" /> <category id="8" name="Effects on Culture and Society" /> <category id="9" name="Globalization" /> <category id="10" name="Environment" /> <category id="11" name="History" /> <category id="12" name="Middle East" /> <category id="13" name="Israel and Palestine" /> <category id="14" name="United States" /> <category id="15" name="Media and Propaganda" /> <category id="16" name="Alternative Sources" /> </categories>Here are the results after processing:
Obviously XML processing carries some organizational overhead so you wouldn't necessarily want to use it for such simple tasks as this sample. |
|
Elements is brought to you by nthWave Web Works this page was built on Fri May 2 15:31:28 2003 |