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

#1 syborg  Icon 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.

 <settings>
    <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" />
</settings>

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.

auto_backup=false
exitonbackspace=true
nb_backupfiles=10
refreshonload=true
screen2=false
show_batch=true
show_log=true


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  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7872
  • View blog
  • Posts: 13,354
  • 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:
file.write("\n".join(config_items)


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