1

I have created an XML document with the following contents.

<books>
  <book id="1">
    <title>Title01</title>
    <authors/>
    <pages>
      <page>Page01</page>
      <page>Page02</page>
      <page>Page03</page>
      <page>Page04</page>
      <page>Page05</page>
    </pages>
  </book>
  <book id="2">
    <title>Title02</title>
    <authors/>
    <pages>
      <page>Page01</page>
      <page>Page02</page>
      <page>Page03</page>
      <page>Page04</page>
      <page>Page05</page>
    </pages>
  </book>
</books>

I then use a Python script to split up and write the individual books into separate files;however, the resulting files are not XML files because they do not have the XML declaration. Is there a way of creating XML files in Python?

The idea is to ensure that each file has the XML declaration as show below.

 <?xml version="1.0" encoding="ISO-8859-1" ?>
  <book id="1">
    <title>Title01</title>
    <authors/>
    <pages>
      <page>Page01</page>
      <page>Page02</page>
      <page>Page03</page>
      <page>Page04</page>
      <page>Page05</page>
    </pages>
  </book>
lightonphiri
  • 782
  • 2
  • 14
  • 29
  • Duplicate of [Best XML writing tool for python](http://stackoverflow.com/questions/56229/best-xml-writing-tool-for-python) – Mike Pennington May 24 '11 at 11:10
  • The XML declaration is not required for XML well-formedness (see http://www.w3.org/TR/2008/REC-xml-20081126/#NT-prolog). Do you have another reason for needing it? – Paul Butcher May 24 '11 at 11:22
  • @Paul Butcher: Thank you for the response. I was rather hoping that I could have declaration to explicitly specify the encoding used. Also, I might find myself using the linux "find" command at some point to check if the file type is XML. – lightonphiri May 24 '11 at 11:34

3 Answers3

0

Why don't you write the xml declaration to each book file before you write the book entry?

joel goldstick
  • 4,393
  • 6
  • 30
  • 46
0

Encode your files in UTF-8 instead of some legacy encoding like ISO-8859-1. Then you don't need an XML declaration.

John Machin
  • 81,303
  • 11
  • 141
  • 189
0

You should look into the xml.etree.ElementTree module. The link is for Python 3, but it was included way before that. I use it in Python 2.5, so you should be ok.

Also, I have had good results with xml.dom.minidom. Once you have built a Document (by adding elements with createElement('ELEM_NAME'), you just write it to a stream with mydoc.toprettyxml().

Daren Thomas
  • 67,947
  • 40
  • 154
  • 200