Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

module to create python object representation from xml [closed]

Tags:

python

xml

pickle

I'm searching for an easy to handle python native module to create python object representation from xml.

I found several modules via google (one of them is XMLObject) but didn't want to try out all of them.

What do you think is the best way to do such things?

EDIT: I missed to mention that the XML I'd like to read is not generated by me. It's an existing XML file in a structure of which I have no control over.

like image 271
Martin Avatar asked Nov 20 '08 20:11

Martin


People also ask

What is the name of Python built in module for XML processing?

The xml.etree.ElementTree module implements a simple and efficient API for parsing and creating XML data. Changed in version 3.3: This module will use a fast implementation whenever available.

Which module can you use to parse an XML file using Python?

Python allows parsing these XML documents using two modules namely, the xml. etree. ElementTree module and Minidom (Minimal DOM Implementation).

What is the name of the Python object that represents XML data?

The Document Object Model, or “DOM,” is a cross-language API from the World Wide Web Consortium (W3C) for accessing and modifying XML documents. A DOM implementation presents an XML document as a tree structure, or allows client code to build such a structure from scratch.

How do I open a XML file in Python?

To read an XML file using ElementTree, firstly, we import the ElementTree class found inside xml library, under the name ET (common convension). Then passed the filename of the xml file to the ElementTree. parse() method, to enable parsing of our xml file. Then got the root (parent tag) of our xml file using getroot().


1 Answers

You say you want an object representation, which I would interpret to mean that nodes become objects, and the attributes and children of the node are represented as attributes of the object (possibly according to some Schema). This is what XMLObject does, I believe.

There are some packages that I know of. 4Suite includes some tools to do this, and I believe Amara specifically implements this (built on top of 4Suite). You can also use lxml.objectify, which was inspired by Amara and gnosis.xml.objectify.

Of course a third option is, given a concrete representation of the XML (using ElementTree or lxml) you can build your own custom model around that. lxml.html is an example of that, extending the base interface of lxml with some HTML-specific functionality.

like image 102
ianb Avatar answered Oct 14 '22 11:10

ianb