Go Back   Velocity Reviews > Newsgroups > XML
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

XML - Problem with "&" charater in xml.

 
Thread Tools Search this Thread
Old 07-13-2006, 06:52 AM   #1
Default Problem with "&" charater in xml.



i have walked a directory and have written the foll xml document.
one of the folder had "&" character so i replaced it by "&"
#------------------test1.xml
<Directory>
<dirname>C:\Documents and Settings\Administrator\Desktop\1\bye
w&amp;y </dirname>
<file>
<name>def.txt</name>
<time>200607130417</time>
</file>
</Directory>
<Directory>
<dirname>C:\Documents and Settings\Administrator\Desktop\1\hii
wx</dirname>
<file>
<name>abc.txt</name>
<time>200607130415</time>
</file>
</Directory

now in my python code i want to parse this doc and print the directory
name.
###----------handler------------filename---handler.py
from xml.sax.handler import ContentHandler
class oldHandler(ContentHandler):
def __init__(self):
self.dn = 0
def startElement(self, name, attrs):
if name=='dirname':
self.dn=1

def characters(self,str):
if self.dn:
print str

def endElement(self, name):
if name == 'dirname':
self.dn=0


#---------------------------------------------------------------------
#main code--- fname----art.py
import sys
from xml.sax import make_parser
from handlers import oldHandler

ch = oldHandler()
saxparser = make_parser()

saxparser.setContentHandler(ch)
saxparser.parse(sys.argv[1])
#-----------------------------------------------------------------------------
i run the code as: $python art.py test1.xml

i am getting output as:

C:\Documents and Settings\Administrator\Desktop\1\bye w
&
y
C:\Documents and Settings\Administrator\Desktop\1\hii wx

where as i need an output which should look like this.
C:\Documents and Settings\Administrator\Desktop\1\bye w&y

C:\Documents and Settings\Administrator\Desktop\1\hii wx

Can someone tell me the solution for this.



Kirt
  Reply With Quote
Old 07-13-2006, 09:09 AM   #2
Bjoern Hoehrmann
 
Posts: n/a
Default Re: Problem with "&" charater in xml.

* Kirt wrote in comp.text.xml:
>i am getting output as:
>
>C:\Documents and Settings\Administrator\Desktop\1\bye w
>&
>y
>C:\Documents and Settings\Administrator\Desktop\1\hii wx
>
>where as i need an output which should look like this.
>C:\Documents and Settings\Administrator\Desktop\1\bye w&y
>
>C:\Documents and Settings\Administrator\Desktop\1\hii wx
>
>Can someone tell me the solution for this.


SAX allows to split characters events as you encounter here. If there is
no switch to force the SAX parser to accumulate the text before calling
the handler, you have to do that yourself.
--
Björn Höhrmann · private.php?do=newpm&u= · http://bjoern.hoehrmann.de
Weinh. Str. 22 · Telefon: +49(0)621/4309674 · http://www.bjoernsworld.de
68309 Mannheim · PGP Pub. KeyID: 0xA4357E78 · http://www.websitedev.de/
  Reply With Quote
Old 07-13-2006, 09:15 AM   #3
George Bina
 
Posts: n/a
Default Re: Problem with "&" charater in xml.

A SAX parser can notify a text node by calling any number of times the
characters method so you need to accumulate all the information you
receive on the characters method and output the text when you get a
notification different than characters.

Best Regards,
George
---------------------------------------------------------------------
George Cristian Bina
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com

Kirt wrote:
> i have walked a directory and have written the foll xml document.
> one of the folder had "&" character so i replaced it by "&amp;"
> #------------------test1.xml
> <Directory>
> <dirname>C:\Documents and Settings\Administrator\Desktop\1\bye
> w&amp;y </dirname>
> <file>
> <name>def.txt</name>
> <time>200607130417</time>
> </file>
> </Directory>
> <Directory>
> <dirname>C:\Documents and Settings\Administrator\Desktop\1\hii
> wx</dirname>
> <file>
> <name>abc.txt</name>
> <time>200607130415</time>
> </file>
> </Directory
>
> now in my python code i want to parse this doc and print the directory
> name.
> ###----------handler------------filename---handler.py
> from xml.sax.handler import ContentHandler
> class oldHandler(ContentHandler):
> def __init__(self):
> self.dn = 0
> def startElement(self, name, attrs):
> if name=='dirname':
> self.dn=1
>
> def characters(self,str):
> if self.dn:
> print str
>
> def endElement(self, name):
> if name == 'dirname':
> self.dn=0
>
>
> #---------------------------------------------------------------------
> #main code--- fname----art.py
> import sys
> from xml.sax import make_parser
> from handlers import oldHandler
>
> ch = oldHandler()
> saxparser = make_parser()
>
> saxparser.setContentHandler(ch)
> saxparser.parse(sys.argv[1])
> #-----------------------------------------------------------------------------
> i run the code as: $python art.py test1.xml
>
> i am getting output as:
>
> C:\Documents and Settings\Administrator\Desktop\1\bye w
> &
> y
> C:\Documents and Settings\Administrator\Desktop\1\hii wx
>
> where as i need an output which should look like this.
> C:\Documents and Settings\Administrator\Desktop\1\bye w&y
>
> C:\Documents and Settings\Administrator\Desktop\1\hii wx
>
> Can someone tell me the solution for this.


  Reply With Quote
Old 07-13-2006, 10:06 PM   #4
Joe Kesselman
 
Posts: n/a
Default Re: Problem with "&" charater in xml.

Note that any good SAX tutorial will demonstrate how to buffer the
characters() events, if you don't feel like reinventing the solution
yourself.
  Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump