Reputation: 4160
I need to remove the following from this xml -
<entry>
<id>1234</id>
<title>hello</title>
<source>com.server.webclient.xxx</source>
<xxx:component>
<xxx:id>2134</xxx:id>
<xxx:name>name</xxx.name>
</xxx:component>
</entry>
What I want to do is remove <entry>
, <id>
, <title>
and <source>
My code is trying to just delete ID right now but isn't returning an error but it isn't removing the values.
with open('c:\\temp\\%s.xml' % args.componentName, 'w') as f:
xmlObject = etree.fromstring(r.content)
for elem in xmlObject.xpath( '//id' ) :
elem.remove(elem)
f.write(etree.tostring(xmlObject, pretty_print=True))
This is what I want my XML to look like -
<xxx:component>
<xxx:id>2134</xxx:id>
<xxx:name>name</xxx.name>
</xxx:component>
Upvotes: 1
Views: 763
Reputation: 90889
An easier option for you to achieve what you want would be to find the <xxx:component>
element inside the <entry>
component and write that to the file.
Example -
with open('c:\\temp\\%s.xml' % args.componentName, 'w') as f:
xmlObject = etree.fromstring(r.content)
reqElem = xmlObject.xpath('//xxx:component',namespaces=ns) #ns should have the `xxx` prefix and whatever its actual namespace is
if len(reqElem) == 1:
f.write(etree.tostring(reqElem[0], pretty_print=True))
Upvotes: 1