need help in writing xml document.

M

Moqtar

I am using python to walk a directory and write the filename in an xml
document of type

<?xml version="1.0" encoding="ISO-8859-1"?>
<job>
<jobname>Test</jobname>
<jobtime>200607120616</jobtime>
<Directory>
<dirname>C:\Program Files\Acorden SourceXT</dirname>
<file>
<name>readme.txt</name>
<time>200011041444</time>
</file></Directory>
<Directory>
<dirname>C:\Program Files\Yahoo</dirname>
</Directory>
</job>

the code works. But if the directoryname of file name contains the
character "&" .
for eg. <dirname>C:\Program Files\LetterSize B&W</dirname>
it gives an error. i cannot view the xml document.
 
H

harvey.thomas

Moqtar said:
I am using python to walk a directory and write the filename in an xml
document of type

<?xml version="1.0" encoding="ISO-8859-1"?>
<job>
<jobname>Test</jobname>
<jobtime>200607120616</jobtime>
<Directory>
<dirname>C:\Program Files\Acorden SourceXT</dirname>
<file>
<name>readme.txt</name>
<time>200011041444</time>
</file></Directory>
<Directory>
<dirname>C:\Program Files\Yahoo</dirname>
</Directory>
</job>

the code works. But if the directoryname of file name contains the
character "&" .
for eg. <dirname>C:\Program Files\LetterSize B&W</dirname>
it gives an error. i cannot view the xml document.

You need to replace any & characters with &amp;

name = "LetterSize B&W"
name = name.replace("&", "&amp;")

HTH
 
I

Insane

Moqtar napisal(a):
I am using python to walk a directory and write the filename in an xml
document of type

<?xml version="1.0" encoding="ISO-8859-1"?>
<job>
<jobname>Test</jobname>
<jobtime>200607120616</jobtime>
<Directory>
<dirname>C:\Program Files\Acorden SourceXT</dirname>
<file>
<name>readme.txt</name>
<time>200011041444</time>
</file></Directory>
<Directory>
<dirname>C:\Program Files\Yahoo</dirname>
</Directory>
</job>

the code works. But if the directoryname of file name contains the
character "&" .
for eg. <dirname>C:\Program Files\LetterSize B&W</dirname>
it gives an error. i cannot view the xml document.

It gives an error because & normally starts an XML entity, which must
be defined first.
XML general entity starts with & and ends with ; so You lead your xml
parser to an error starting entity and not ending it;

To use special character in XML markup (even content) you must use
"escape sequence" - and its an entity ofcourse.

So You write special entity:
&amp; - and it means &

You can also use a unicode value for a single character. It works like
this:
  - it means 160 decimal in unicode (non braking space) number is
preceded with hash symbol.

Note that 160 is in decimal but most unicode charts are in hexadecimal
- you must convert it by yourself.


<dirname>C:\Program Files\LetterSize B&W</dirname>

should be:

<dirname>C:\Program Files\LetterSize B&amp;W</dirname>
or
<dirname>C:\Program Files\LetterSize B&W</dirname>


Unicode charts:
http://www.unicode.org/charts/
 
A

Andy Dingley

Moqtar said:
I am using python to walk a directory and write the filename in an xml
document of type

Don't write XML documents. Use a DOM to do it for you.

If you think it's bad now with a trivial problem over &amp;, then just
wait until you have to port your app simultaneously to Czech and Arabic
within a week and you have a load of character set encoding issues to
worry about. The DOM saves you from all this - make use of it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,951
Messages
2,570,113
Members
46,699
Latest member
Abigail89S

Latest Threads

Top