1 Replies - 593 Views - Last Post: 17 April 2013 - 02:25 PM Rate Topic: -----

#1 syborg   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 17-April 13

parse an XML into a cfg file

Posted 17 April 2013 - 02:18 PM

I have a XML file which looks like this.

    <setting id="auto_backup" value="false" />
    <setting id="exitonbackspace" value="true" />
    <setting id="hidemousepointer" value="true" />
    <setting id="nb_backup_files" value="10" />
    <setting id="refreshonload" value="true" />
    <setting id="screen2" value="false" />
    <setting id="show_batch" value="true" />
    <setting id="show_log" value="true" />

I would like to run a python script on the XML which would extract the information into a cfg file. So it would look like this.


I have tried to use minidom & Ixml but I cant seem to get to output to a settings.cfg file

Is This A Good Question/Topic? 0
  • +

Replies To: parse an XML into a cfg file

#2 jon.kiparsky   User is offline

  • Beginner
  • member icon

Reputation: 11089
  • View blog
  • Posts: 18,963
  • Joined: 19-March 11

Re: parse an XML into a cfg file

Posted 17 April 2013 - 02:25 PM

If that's all you're looking at, get the <settings> node (document.getChildNodes()[0] should work for that) and then getAllElementsByTagName("setting") to get a list of nodes.

Here's a sketch of where I'd go from there:
config_items = []
for node in node_list:
  config_items.append(node.getAttribute("id") + '=' + node.getAttribute("value"))

(that's a little more concise with a listcomp, but this might be easier for you to follow)

then open a file called settings.cfg and write to it:

And you should be done.

I haven't tested any of this, so there might be some errors, but I hope it gets you started at least.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1