Parses an XML string, xmlstr, and returns the corresponding Jython variable v.
v = xml_parse(xmlstr,attswitch='on')
This is a non-validating parser. XML processing entries or comments starting with '<?' or '<!', are ignored by the parser.
xmlstr XML string, for example read from a file.
attswitch optional,
default='on':
'on' reads XML header attributes idx,
size, type if
present and interprets these to create the correct Jython data
types.
'off' ignores XML element header attributes and interprets contents
as strings.
v Jython variable or structure.
This example shows how to define a simple XML string and parse it into a Jython variable. As the idx, type, and size attributes are defined, the resulting Jython data structure conforms to these specifications (list of size [1x2]).
xmlstr = '<root idx="1" type="double" size="1 2">3.1416 1.4142</root>'
from gdxml import *
V1 = xml_parse(xmlstr)
print V1
[3.1416, 1.4142] % (type jclass org.python.core.PyList)
Again, setting the attswitch parameter to 'off' lets the parser ignore the attributes and the returned variable is interpreted as a string.
V2 = xml_parse(xmlstr,'off')
print V2
'3.1416 1.4142' % (type jclass org.python.core.PyString)
Let's define a more complex data set in XML and convert it into a Jython data structure:
# Paste this assignment into a Python script and run it:
xmlstr = """
<root>
<project>
<name>myProjectName</name>
<date>2004-09-13</date>
<bytes>10472</bytes>
</project>
<project>
<name>myProject Two</name>
<date>2004-09-13</date>
<bytes>9851</bytes>
</project>
</root>
"""
v = xml_parse(xmlstr)
print v
[{'project':
{'bytes': '10472',
'date': '2004-09-13',
'name': 'myProjectName'}},
{'project':
{'bytes': '9851',
'date': '2004-09-13',
'name': 'myProject Two'}}]
xml_format, xml_load, xml_save
Copyright © 2005, The Geodise Project, University of Southampton