By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,764 Members | 1,466 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,764 IT Pros & Developers. It's quick & easy.

need help in writing xml document.

P: n/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.

Jul 12 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a

Moqtar wrote:
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

Jul 12 '06 #2

P: n/a

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/

Jul 12 '06 #3

P: n/a

Moqtar wrote:
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.

Jul 12 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.