xml - Remove empty <ul> nodes from IXMLDOMDOCUMENT in Delphi -
i have many xml nodes in xmldocument. want remove empty <ul> nodes. how can accomplish this?
here snippet:
  <li>     <a href="javascript:void(0);">level 1</a>     <ul id="subject19">       <li>         <a href="javascript:void(0);">level 2</a>         <ul id="subject20">           <li>             <a href="javascript:void(0);">level 3</a>             <ul id="subject21"/>           </li>         </ul>       </li>     </ul>   </li> i need remove <ul id="subject21"/>
you can use simple recursion. here example how:
procedure scanandremove(anode: ixmlnode); var   i: integer;   childnode: ixmlnode; begin   := 0;   while < anode.childnodes.count   begin     childnode := anode.childnodes[i];     if (childnode.nodename = 'ul') , (childnode.childnodes.count = 0)       anode.childnodes.remove(childnode) else       begin         scanandremove(childnode);         inc(i);       end;   end; end; and pass document root element:
procedure cleanup; var   xmldoc: ixmldocument; begin   xmldoc := txmldocument.create(nil);   try     xmldoc.loadfromxml('...');     scanandremove(xmldoc.documentelement);     // xmldoc contains desired result       xmldoc := nil;   end; end; edit recursive function will remove node without children, containing value. eg:
<ul>   blabla </ul> if want opposite, should add 1 more check - ie:
if (childnode.nodename = 'ul') ,    (childnode.childnodes.count = 0) ,    (vartostrdef(childnode.nodevalue, '') = '')   ...  
Comments
Post a Comment