0

Hey. I want to have a config.xml file for settings in a Python web app.

I made car.xml manually. It looks like this:

<car>
    <lights>
        <blinkers>off</blinkers>
    </lights>
</car>

Now I want to see whether the blinkers are on or off, using xml.etree.ElementTree.

import xml.etree.ElementTree as ET
tree = ET.parse('car.xml')
blinkers = tree.findtext('lights/blinkers')
print blinkers
> off

Now I want to turn the blinkers on and off, how can I do this?

Frode
  • 501
  • 2
  • 7
  • 20
  • 5
    you don't _really_ want an xml file. You want a simple python module name `settings` with module level constants in ALLCAPS that have whatever information you thought you wanted to store in xml. You can have tuples, lists, dictionaries, integers, floats, etc. When some part of your application needs a setting, FOO, it can just do `import settings; settings.FOO`. – aaronasterling Oct 03 '10 at 13:45
  • I can see how that would be faster and easier for settings, thanks. But say I have a CMS that stores articles as xml, if blinkers was “heading”, how would I change that heading. – Frode Oct 03 '10 at 13:53

4 Answers4

2

You can remove nodes by calling the parent node's remove method, and insert nodes by calling ET.SubElement:

import xml.etree.ElementTree as ET

def flip_lights(tree):
    lights = tree.find('lights')
    state=get_blinker(tree)
    blinkers = tree.find('lights/blinkers')
    lights.remove(blinkers)
    new_blinkers = ET.SubElement(lights, "blinkers")
    new_blinkers.text='on' if state=='off' else 'off'

def get_blinker(tree):
    blinkers = tree.find('lights/blinkers')
    return blinkers.text

tree = ET.parse('car.xml')
print(get_blinker(tree))
# off
flip_lights(tree)
print(get_blinker(tree))
# on
flip_lights(tree)
print(get_blinker(tree))
# off
flip_lights(tree)
print(get_blinker(tree))
# on
tree.write('car2.xml')
unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677
2

Without addressing the merits of using XML instead of a Python module for managing configuration files, here's how to do what you asked using lxml:

>>> from lxml import etree
>>> xml = """<car>
   <lights>
      <blinkers>on</blinkers>
   </lights>
</car>"""
>>> doc = etree.fromstring(xml)
>>> elm = doc.xpath("/car/lights/blinkers")[0]
>>> elm.text="off"
>>> etree.tostring(doc)
'<car>\n   <lights>\n      <blinkers>off</blinkers>\n   </lights>\n</car>'
Robert Rossney
  • 94,622
  • 24
  • 146
  • 218
0

Use beautifulstonesoup. Here is the section on modifying xml:

http://www.crummy.com/software/BeautifulSoup/documentation.html#Modifying%20the%20Parse%20Tree

amadain
  • 2,724
  • 4
  • 37
  • 58
0

XML is a rather poor way of storing configuration settings. For one, XML is not exactly human friendly in the context of settings. In the Python universe in particular you are better off using a settings module (as @AaronMcSmooth commented). Unfortunately a lot of projects in the Java world have (mis?)used XML for settings thereby making it a trend. I'd argue that this trend really sucks. Use native settings (module in Python) or something more human friendly like YAML.

Manoj Govindan
  • 72,339
  • 21
  • 134
  • 141