[XML-SIG] using minidom
Stefan Behnel
stefan_ml at behnel.de
Mon Jun 25 14:45:01 CEST 2007
Hi,
Stefan Behnel wrote:
> Alexandro Colorado wrote:
>> So basically this is what I want, I want to progamatically be able to
>> change the atribute of an XML:
>>
>> <?xml version="1.0" encoding="UTF-8"?>
>> <!DOCTYPE toolbar:toolbar PUBLIC "-//OpenOffice.org//DTD OfficeDocument
>> 1.0//EN" "toolbar.dtd">
>> <toolbar:toolbar xmlns:toolbar="http://openoffice.org/2001/toolbar"
>> xmlns:xlink="http://www.w3.org/1999/xlink">
>> <toolbar:toolbaritem xlink:href=".uno:OpenUrl" toolbar:visible="false"/>
>> ....
>> </toolbar:toolbar>
>>
>> I want to change the toolbar:visible atribute from false to true
>> preffering to use minidom.
>
> In case minidom is not a requirement, you can use lxml.etree:
>
> >>> import lxml.etree as et
> >>> finditems = et.XPath("//tb:toolbaritem[@tb:visible = 'false']",
> {'tb' : "http://openoffice.org/2001/toolbar"})
> >>> tree = et.parse("myfile.xml")
> >>> for toolbaritem in finditems(tree):
> ... toolbaritem.set(
> ... "{http://openoffice.org/2001/toolbar}visible", "true")
>
>
> http://codespeak.net/lxml/
Ah, BTW, if you need something that runs under an older Python version (I
assume you want it to run in OOo), here's a solution that also runs with
ElementTree (which can be installed on Python 1.5 and later). lxml requires at
least Python 2.3.
>>> import elementtree.ElementTree as et
>>> TBNS = "{http://openoffice.org/2001/toolbar}"
>>> tree = et.parse("myfile.xml")
>>> for tbitem in tree.getiterator(TBNS+"toolbaritem"):
... if tbitem.get(TBNS+"visible") == "false":
... tbitem.set(TBNS+"visible", "true")
Have fun,
Stefan
More information about the XML-SIG
mailing list