python - Search Text and replace with lxml -
i need search text in multi-line xml file have multiple tags. xml file looks
<?xml version="1.0" encoding="utf-8"?> <nc:data xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0"> <system xmlns="http://www.abc.xyz"> <context> <name>context_1</name> <host> <name>xyz</name> <tag1> <name>pqr</name> <role>s1</role> <tag2>test</tag2> </tag1> <tag2> <name>pqr</name> <role>s1</role> <tag2>test</tag2> </tag2> </host> </context> </system> </nc:data> i want search appearances of text "test" in xml file , list parent tag in output. unfortunately unable so.
the python code have written :
import os import xml import sys xml.dom import minidom import xml.etree.elementtree et def xml_parsing(): ''' open('file.xml', 'rt') f: tree = et.parse(f) node in tree.findall('.//context'): print node, node.tag, node.attrib url = node.attrib.get('tag1') print url xml_parsing() i getting blank result output , unable beyond it. have tried both elementtree , lxml. believe has search pattern trying find using findall.
please advise expert comments should tried now.
i tried sax way , code this:
xmldoc = minidom.parse('file.xml') reflist = xmldoc.getelementsbytagname('tag1') print reflist[0].toxml() but returns me complete line other value between tags.
xpath expression find element, regardless of element name , location in xml document, having text value equals test //*[text()='test'] or alternatively //*[.='test'].
consider following working lxml example demonstrate finding such elements , update value :
from lxml import etree et xml = '''<?xml version="1.0" encoding="utf-8"?> <nc:data xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0"> <system xmlns="http://www.abc.xyz"> <context> <name>context_1</name> <host> <name>xyz</name> <tag1> <name>pqr</name> <role>s1</role> <tag2>test</tag2> </tag1> <tag2> <name>pqr</name> <role>s1</role> <tag2>test</tag2> </tag2> </host> </context> </system> </nc:data>''' tree = et.fromstring(xml) node in tree.xpath("//*[.='test']"): #update node value new text 'foo' node.text = 'foo' print et.tostring(node) output :
<tag2 xmlns="http://www.abc.xyz" xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0">foo</tag2> <tag2 xmlns="http://www.abc.xyz" xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0">foo</tag2>
Comments
Post a Comment