Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > RE: Parsing xml file using python

Reply
Thread Tools

RE: Parsing xml file using python

 
 
David LeBlanc
Guest
Posts: n/a
 
      03-05-2004

> Hello, all,
>
> I am new to Python.
>
> I need to read an XML document and ignore all XML tags and write only
> those between the tags to a text file. In other words, if I have an
> XML document like so:
>
> <tag1>This</tag1>
> <tag2>is</tag2>
> <tag3>a</tag3>
> <tag1>test</tag1>
>
> I need to write "This is a test" to a text file. How do I achieve
> this? Thanks.


PyXML would be a perfect solution - and easy too.

Dave LeBlanc
Seattle, WA USA

 
Reply With Quote
 
 
 
 
Josiah Carlson
Guest
Posts: n/a
 
      03-05-2004
> PyXML would be a perfect solution - and easy too.

Even easier would be to use an SGML parser:
http://flangy.com/dev/python/striphtml.html

Works for XML, HTML, etc.

- Josiah
 
Reply With Quote
 
 
 
 
chad
Guest
Posts: n/a
 
      03-05-2004
Hi, Thanks.

I downloaded PyXML from sourceforge, but could not install it. When I
double click the install file, it asks me to go next and choose the
folder to install it, but then it gives me no choice to choose from.
It seems the installer cannot get my file system info.

I am using Python on Win2K.


"David LeBlanc" <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
> > Hello, all,
> >
> > I am new to Python.
> >
> > I need to read an XML document and ignore all XML tags and write only
> > those between the tags to a text file. In other words, if I have an
> > XML document like so:
> >
> > <tag1>This</tag1>
> > <tag2>is</tag2>
> > <tag3>a</tag3>
> > <tag1>test</tag1>
> >
> > I need to write "This is a test" to a text file. How do I achieve
> > this? Thanks.

>
> PyXML would be a perfect solution - and easy too.
>
> Dave LeBlanc
> Seattle, WA USA

 
Reply With Quote
 
Tim Heaney
Guest
Posts: n/a
 
      03-05-2004
http://www.velocityreviews.com/forums/(E-Mail Removed) (chad) writes:
>
> I downloaded PyXML from sourceforge, but could not install it.


Python already comes with stuff like xmllib, minidom, and the
aforementioned sgmllib

http://www.python.org/doc/current/lib/markup.html

You should be able to do what you described without installing
anything else.

I hope this helps,

Tim
 
Reply With Quote
 
chad
Guest
Posts: n/a
 
      03-05-2004
Josiah Carlson <(E-Mail Removed)> wrote in message news:<c29981$4eu$(E-Mail Removed)>...
> > PyXML would be a perfect solution - and easy too.

>
> Even easier would be to use an SGML parser:
> http://flangy.com/dev/python/striphtml.html
>
> Works for XML, HTML, etc.
>
> - Josiah


This is absolutely cool. Nice and neat and beautiful. Thank you very
much for that cool link, Josiah.
 
Reply With Quote
 
Sam Smith
Guest
Posts: n/a
 
      03-05-2004
Please try the following code. As Tim says everything you need for
your problem at hand is included in the core distribution. Look for
documentation for xml.sax module.

Code ::

import xml.sax.handler
import xml.sax
import sys

class ReadXML(xml.sax.handler.ContentHandler):
def __init__(self, xml_file):
xml.sax.handler.ContentHandler.__init__(self)
self.content = ""
xml.sax.parse(xml_file, self)

def characters(self, content):
self.content = self.content.lstrip().rstrip() + " " + content

if __name__=="__main__":
if len(sys.argv) != 3:
print "usage: %prog xml_file output_file"
sys.exit()
xml_file = sys.argv[1]
output_file = sys.argv[2]
readXML = ReadXML(xml_file)
f = file(output_file, "w+")
f.write(readXML.content)
f.close()
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
What libraries should I use for MIME parsing, XML parsing, and MySQL ? John Levine Ruby 0 02-02-2012 11:15 PM
Different results parsing a XML file with XML::Simple (XML::Sax vs. XML::Parser) Erik Wasser Perl Misc 5 03-05-2006 10:09 PM
Print XML parsing to JspWriter (out) Class org.xml.sax.helpers.NewInstance can not access a member of class javax.xml.parsers.SAXParser with modifiers "protected" Per Magnus L?vold Java 0 11-15-2004 02:27 PM
RE: Parsing xml file using python Tony Meyer Python 0 03-07-2004 04:16 AM
Parsing xml file using python chad Python 4 03-05-2004 05:30 PM



Advertisments