bash/python script to retrieve nodes with given tag property -
the document.xml file looks following:
<?xml version="1.0" encoding="utf-8"?> <document version="4">     <tool version="21.4"/>     <notes>         <note id="minor" message="dont forget">             <place file="path/to/filea" line="81"/>         </note>         <note id="major" message="quite well">             <place file="path/to/fileb" line="11"/>             <place file="path/to/filec" line="67"/>         </note>         <!-- ... -->         <note id="medium" message="keep going">             <place file="path/to/filef" line="789"/>             <place file="path/to/filea" line="91"/>             <!-- ... -->             <place file="path/to/filek" line="6"/>         </note>     </notes> </document>   i need script "grep" above based on 'file' attribute of <note> tag.
for example:
prompt$ xmlgrep "path/to/filea" document.xml
would result in:
<?xml version="1.0" encoding="utf-8"?> <document version="4">     <tool version="21.4"/>     <notes>         <note id="minor" message="dont forget">             <place file="path/to/filea" line="81"/>         </note>         <note id="medium" message="keep going">             <place file="path/to/filef" line="789"/>             <place file="path/to/filea" line="91"/>             <place file="path/to/filek" line="6"/>         </note>     </notes> </document>   could please suggest me ?elegant? way achieve it?
best regards
this following code works not formatted xml
import xml.etree.elementtree et import sys path = sys.argv[1] xml_file = sys.argv[2] tree = et.parse(xml_file) root = tree.getroot() notes = root.find('notes') note in notes:     found_any_path = false     place in note[:]:         if place.attrib['file'] == path:             found_any_path = true     else:         if not found_any_path:             notes.remove(note)  print et.tostring(root, encoding="utf-8", method="xml")      
Comments
Post a Comment