need help in writing xml document.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Moqtar

    #1

    need help in writing xml document.


    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>200607 120616</jobtime>
    <Directory>
    <dirname>C:\Pro gram Files\Acorden SourceXT</dirname>
    <file>
    <name>readme.tx t</name>
    <time>200011041 444</time>
    </file></Directory>
    <Directory>
    <dirname>C:\Pro gram Files\Yahoo</dirname>
    </Directory>
    </job>

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

  • harvey.thomas@informa.com

    #2
    Re: need help in writing xml document.


    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>200607 120616</jobtime>
    <Directory>
    <dirname>C:\Pro gram Files\Acorden SourceXT</dirname>
    <file>
    <name>readme.tx t</name>
    <time>200011041 444</time>
    </file></Directory>
    <Directory>
    <dirname>C:\Pro gram Files\Yahoo</dirname>
    </Directory>
    </job>
    >
    the code works. But if the directoryname of file name contains the
    character "&" .
    for eg. <dirname>C:\Pro gram Files\LetterSiz e 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

    Comment

    • Insane

      #3
      Re: need help in writing xml document.


      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>200607 120616</jobtime>
      <Directory>
      <dirname>C:\Pro gram Files\Acorden SourceXT</dirname>
      <file>
      <name>readme.tx t</name>
      <time>200011041 444</time>
      </file></Directory>
      <Directory>
      <dirname>C:\Pro gram Files\Yahoo</dirname>
      </Directory>
      </job>
      >
      the code works. But if the directoryname of file name contains the
      character "&" .
      for eg. <dirname>C:\Pro gram Files\LetterSiz e 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:
      &#160; - 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:\Pro gram Files\LetterSiz e B&W</dirname>

      should be:

      <dirname>C:\Pro gram Files\LetterSiz e B&amp;W</dirname>
      or
      <dirname>C:\Pro gram Files\LetterSiz e B&#38;W</dirname>


      Unicode charts:


      Comment

      • Andy Dingley

        #4
        Re: need help in writing xml document.


        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.

        Comment

        Working...