-3

I need to convert docstrings into XML data and append this XML data in the begining of the already existing XML file.

Data to convert into XML looks something like this:

"""
Sample Description:
Sample author: abc
sample version: x.1
multiple lines like this
"""

string = __doc__ gives me doc string, but how to proceed further, converting to XML and appending into the begining of the already existing XML file?

The XML file should look like this:

<sample-description
    sample-author="abc"
    sample-version="x.1">
    multiple-lines
</sample-description>
Paolo Moretti
  • 54,162
  • 23
  • 101
  • 92
user1116309
  • 91
  • 1
  • 4
  • you should really try anything, searching on google is very easy and gives you a lot of answers – Samuele Mattiuzzo Oct 22 '12 at 09:12
  • 2
    is your problem breaking the doc string up into useful parts, or creating the xml nodes? Your question makes it look like you haven't attempted either... – Sheena Oct 22 '12 at 09:16

1 Answers1

1

You need to break the problem down into steps -- smaller problems to solve that move you closer to your goal:

  1. Enumerate over each line in the string;
  2. Split each line into a (name, value) pair;
  3. If the value is blank, create an XML element with name as the nodeName and store this for use later;
  4. Otherwise, add an attribute with the specified name and value.

You can use minidom to create elements and attributes in an XML file.

To convert the name to a form it can be used by XML, you need to:

  1. Replace the spaces with the '-' character;
  2. Convert the string to lower case.

Use Google search and the Python docs for examples on how to do these steps.

Also, adding print statenents at each point will help you verify you are implementing the step correctly.

reece
  • 7,945
  • 1
  • 26
  • 28