python - How to remove specific item and lines from xml? -
i trying remove specific items , line xml.
-items remove: matched <error>...</error>
-lines remove: <errors> , </errors>
this xml file has fixed format system. cannot modify xml file. , added following line first , last line because have error during et.parse.
modified.write("<errors>\n" + data + "\n</errors>\n") here whole script:
import xml.etree.elementtree et include_pattern = 'include_pattern' output = 'test.xml' def remove_excluded_path_from_xml(_patterns, _file) : #modify xml open(_file, 'r') original: data = original.read() if len(data) == 0 : return false if data[0] != '<errors>': open(_file, 'w') modified: modified.write("<errors>\n" + data + "\n</errors>\n") xml = et.parse(_file) root = xml.getroot() errors = root.findall('error') error in root.iter('error'): bmatch = false item in error.iter(): file = item.findtext('file') if file != none: p in _patterns: if p in file: bmatch = true break if bmatch == false: root.remove(error) errors = root.findall('error') xml.write(_file) return true if __name__ == '__main__': print '[start] removing defects excluded paths' #path include open(include_pattern) f: patterns = f.read().splitlines() result = remove_excluded_path_from_xml(patterns, 'test.xml') print '[end] removing defects excluded paths' question
i want know how remove 2 lines
<errors> , </errors>inserted.there 2 lines different format after running script. want know how fix these differences.
could give me kind advice questions?
1) <path_event></path_event> changed <path_event /> 2) <description>{covlstrv2{{t{calling {0} without checking return value. library function may fail , return error code.}{{code{remove("/tmp/cached-concurrent2.db")}}}}}}</description> changed <description>{covlstrv2{{t{calling {0} without checking return value. library function may fail , return error code.}{{code{remove("/tmp/cached-concurrent2.db")}}}}}}</description>
additionally, attaching test.xml , include_pattern
test.xml
<error> <domain>static_c</domain> <lang>c</lang> <checker>checked_return</checker> <file>/data/project/source/hlos/m_proc/leopard/persistence-client-library/test/persistence_client_library_test.c</file> <function>data_setuprecovery</function> <score>100</score> <ordered>true</ordered> <event> <tag>cond_true</tag> <description>{covlstrv2{{t{condition {0}, taking true branch.}{{code{gpathsegemnts[i] != null}}}}}}</description> <line>895</line> <path_event></path_event> </event> <event> <main>true</main> <tag>check_return</tag> <description>{covlstrv2{{t{calling {0} without checking return value. library function may fail , return error code.}{{code{mkdir(createpath, 484u)}}}}}}</description> <line>898</line> </event> <extra>mkdir</extra> <subcategory>library_function</subcategory> <typepropertiesgroup> <category>error handling issues</category> <impact>medium</impact> <type>unchecked return value library</type> <cwe>252</cwe> <localeffect>the function returns value indicates error condition. if not checked, error condition may not handled correctly.</localeffect> <longdescription>value returned library function not checked errors before being used. value may indicate error condition.</longdescription> <qualitykind>true</qualitykind> <eventsetcaptions> <eventsetcaption>unchecked call function</eventsetcaption> </eventsetcaptions> </typepropertiesgroup> </error> <error> <domain>static_c</domain> <lang>c</lang> <checker>checked_return</checker> <file>/data/project/source/hlos/m_proc/leopard/persistence-common-object/test/test_pco_key_value_store.c</file> <function>test_cachesize</function> <score>100</score> <ordered>true</ordered> <event> <tag>cond_true</tag> <description>{covlstrv2{{t{condition {0}, taking true branch.}{{code{k < databases}}}}}}</description> <line>2119</line> <path_event></path_event> </event> <event> <main>true</main> <tag>check_return</tag> <description>{covlstrv2{{t{calling {0} without checking return value. library function may fail , return error code.}{{code{remove("/tmp/cached-concurrent2.db")}}}}}}</description> <line>2123</line> </event> <extra>remove</extra> <subcategory>library_function</subcategory> <typepropertiesgroup> <category>error handling issues</category> <impact>medium</impact> <type>unchecked return value library</type> <cwe>252</cwe> <localeffect>the function returns value indicates error condition. if not checked, error condition may not handled correctly.</localeffect> <longdescription>value returned library function not checked errors before being used. value may indicate error condition.</longdescription> <qualitykind>true</qualitykind> <eventsetcaptions> <eventsetcaption>unchecked call function</eventsetcaption> </eventsetcaptions> </typepropertiesgroup> </error> include_pattern
file('.*/cm-audio/src/main/*') file('.*/cm-common/include/cm_common/utils/*')
Comments
Post a Comment