0
Ich möchte Element in XML kommentieren und auskommentieren.kommentieren und kommentieren XML-Elemente mit Python
XML sieht so aus.
<ls>
<lo n="x" add="b" l="D">
<myconf conf="rf"/>
<!-- <myconf conf="st"/> -->
</lo>
<lo n="s" add="b" l="D">
<myconf conf="rf"/>
<myconf conf="st"/> <!-- would like to comment this element and uncomment when needed -->
</lo>
<lo n="v" add="b" l="D">
<myconf conf="rf"/>
<!-- <myconf conf="st"/> -->
</lo>
<lo n="h" add="b" l="D">
<myconf conf="rf"/>
<myconf conf="st"/> <!--- would like to comment this element and uncomment when needed-->
</lo>
<Root l="I">
<myconf conf="rf"/>
<!-- <myconf conf="st"/> -->
</Root>
</ls>
bekam ich das letzte Kind von Tag, aber ich verstehe nicht, wie das jeweilige Element kommentieren und Kommentar-, wenn nötig.
dies ist mein Code so weit:
from lxml import etree
tree = etree.parse(r'C:\stop.xml')
for logger in tree.xpath('//logger'):
if logger.get('name') == 'h':
for ref in logger.getchildren():
if ref.get('ref') == 'STDOUT':
ref.append(etree.Comment(' '))
tree.write(r'C:\Log_start.xml', xml_declaration=True, encoding='UTF-8')
Ausgang (nicht erwartet)
<ls>
<lo n="x" add="b" l="D">
<myconf conf="rf"/>
<!-- <myconf conf="st"/> -->
</lo>
<lo n="s" add="b" l="D">
<myconf conf="rf"/>
<myconf conf="st"/> <!-- would like to comment this element and uncomment when needed -->
</lo>
<lo n="v" add="b" l="D">
<myconf conf="rf"/>
<!-- <myconf conf="st"/> -->
</lo>
<lo n="h" add="b" l="D">
<myconf conf="rf"/>
<myconf conf="st"><!-- --></myconf> <!--- would like to comment this element and uncomment when needed-->
</lo>
<Root l="I">
<myconf conf="rf"/>
<!-- <myconf conf="st"/> -->
</Root>
</ls>
Jede Hilfe wird geschätzt.!
Ich habe den Code aktualisiert und die out put bekomme ich.! was nicht erwartet wird! – tgcloud