473,505 Members | 15,212 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Use python to process XML file

Hi,
Can you please tell me how Use python to process XML file?
The example I find is build a DOM, but I just need to do it in SAX
based, how can I do that?

For example, I have a xml file like this:
<log>
<a> text </a>
<a> text </a>
<b> text </b>
<a> text </a>
<c> text </c>
<a> text </a>
<b> text </b>
</log>

For i want to process the node in the order they appears.
If element name == 'a'
do this
else If element name == 'b'
do this
else If element name == 'c'
do this

Mar 10 '06 #1
3 1424
si***************@gmail.com:
Can you please tell me how Use python to process XML file?
The example I find is build a DOM, but I just need to do it in SAX
based, how can I do that?


http://docs.python.org/lib/module-xml.sax.html
http://pyxml.sourceforge.net/topics/...ction-SAX.html

--
René Pijlman
Mar 10 '06 #2
you can use function parse from xml.sax
It takes your xml as the first argument and an
xml.sax.handler.ContentHandler instance as the second.
---
from xml.sax import parse
from xml.sax.handler import ContentHandler

class LogHandler(ContentHandler):
def startElement(self,name):
if name=='a':
#self.do_this()

parse(xmlLog.xml,LogHandler())

Mar 10 '06 #3
I have a little py module that implements the Composite pattern to
represent xml file contents. My class is called ExtensionRegistry
because that's what I use it for, you could rename it to whatever you
wanted. After parsing, this allows me to access my xml file contents
like this:

xmlFile = ExtensionRegistry(xmlFile = 'test.xml')

print xmlFile['log']['a'].getDataItems()

would result in printing
text


Let me know if you want me to send it to you.

Mar 10 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1372
by: Gregory Lielens | last post by:
Hello all, I am not sure this is the correct list to ask as my problem is really related to python low level implementation, but as I do not have found a python.dev list, here it is :-) We...
10
3660
by: Andrew Dalke | last post by:
Is there an author index for the new version of the Python cookbook? As a contributor I got my comp version delivered today and my ego wanted some gratification. I couldn't find my entries. ...
3
2812
by: fdsl ysnh | last post by:
--- python-list-request@python.orgĐ´µŔ: > Send Python-list mailing list submissions to > python-list@python.org > > To subscribe or unsubscribe via the World Wide Web, > visit >...
6
9886
by: tatamata | last post by:
Hello. How can I run some Python script within C# program? Thanks, Zlatko
1
4890
by: Justin Johnson | last post by:
Hello, I'm trying to build Python 2.5.0 on AIX 5.3 using IBM's compiler (VisualAge C++ Professional / C for AIX Compiler, Version 6). I run configure and make, but makes fails with undefined...
0
7098
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7367
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7018
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5613
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5028
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3187
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3176
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
754
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
407
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.