(Tested) my_xml: Simple and Easy to Use XML Parser for Python

Python programming language

Python, MyXML package
my_xml in action in GeeXLab



my_xml is a very simple and easy to use XML parser for Python. I quickly tested it with GeeXLab and it works fine.

Help module to parse a simple XML buffer and store it as a read-only (mostly)
dictionary type object (MyXml). This dictionary can hold other dictionaries,
nodes-lists, or leaf nodes. Access to the nodes by using attributes.

This module is good only for simple XML! No name-spaces, CDATA and other fancy features are supported.

Installation

Unzip the archive somwhere, open a shell box and type:

$> python setup.py install

I tested my_xml with Python 2.6.6.

Code sample

Here is how to create a XML object (actually a dictionary: MyXml) from a XML memory buffer:

from my_xml import *
xml = parse("<gxl><scene vsync=\"1\">s01</scene> \
<cam fov=\"60.0\">c01</cam></gxl>")

There also a function to create a XML object from a XML file:

from my_xml import *
filename = "scene.xml"
xml = parse_file(filename)

And here is a GeeXLab per frame (run_mode=”FRAME”) script that uses the xml object:

import HYP_Utils
import sys

HYP_Utils.DrawText(10, 10, 1.0, 1.0, 0.0, "Python version = " + sys.version)
HYP_Utils.DrawText(10, 50, 1.0, 1.0, 0.0, "xml.gxl.cam = " + str(xml.gxl.cam))

HYP_Utils.DrawText(10, 70, 1.0, 1.0, 0.0, "xml.gxl.keys():")
HYP_Utils.DrawText(20, 90, 1.0, 1.0, 0.0, "%s" % str(xml.gxl.keys()))

HYP_Utils.DrawText(10, 110, 1.0, 1.0, 0.0, "xml.gxl.values():")
HYP_Utils.DrawText(20, 130, 1.0, 1.0, 0.0, "%s" % str(xml.gxl.values()))

HYP_Utils.DrawText(10, 150, 1.0, 1.0, 0.0, "xml.gxl.items():")
HYP_Utils.DrawText(20, 170, 1.0, 1.0, 0.0, "%s" % str(xml.gxl.items()))

y_offset = 200
for i in xml.gxl.items():
  HYP_Utils.DrawText(10, y_offset, 1.0, 1.0, 1.0, "%s" % str(i))
  y_offset = y_offset + 20
  for k in i:
    HYP_Utils.DrawText(20, y_offset, 1.0, 1.0, 1.0, "%s" % str(k))
    y_offset = y_offset + 20

The output of this script is displayed on the screenshot above.

5 thoughts on “(Tested) my_xml: Simple and Easy to Use XML Parser for Python”

  1. mareknr

    Hi. Do you plan to extend support of Python by GeeXLab to version 3.1?

  2. JeGX Post Author

    I didn’t look at it yet, but if the Python 3.x C API is similar to Python 2.x API, I can add it in one of the next versions.

  3. springer

    some DOM block from an HTML document can become a new part of a GXL scene tree. We are close to our (next to come )3D browser 😉

Comments are closed.