Search code examples
delphiomnixml

How to delete specific node in omnixml delphi


i've read this answer but i don't know how to use that sample in my case. I have an xml file

 <Archive>                                  
  <Source>                               
     <Name>321</Name>                   
     <BatchID>123</BatchID>    
  </Source>                              
  <DataList>                             
     <Data>            
        <PN>AAAA</PN>
        <FN>1111</FN>
     </Data>
     <Data>            
        <PN>BBBB</PN>
        <FN>2222</FN>
     </Data>
  </DataList>                            
</Archive>

How can i delete the Node that has PN=BBBB?


I'm so sorry, i think i'm not clear in my question, my bad, My Question is how to delete this section:

 <Data>            
    <PN>BBBB</PN>
    <FN>2222</FN>
 </Data>

not only this section

<PN>BBBB</PN>

The Answer: Thanks to Runner, i modified a little bit of his code

  DeleteNode := XMLDoc.DocumentElement.SelectSingleNode('/Archive/DataList/Data[PN="BBBB"]');
  DeleteNode.ParentNode.RemoveChild(DeleteNode);

Solution

  • One way:

      DeleteNode := OmniXML.DocumentElement.SelectSingleNode('//[PN=''BBBB'']');
      DeleteNode.ParentNode.RemoveChild(DeleteNode);
    

    You can search for it any other way. Note that the above example will only select first node occurence. But I recommend you look at SimpleStorage

    This is a set of interfaces that greatly simplify working with OmniXML. The above example would then be:

    SimpleStorage.Remove('//[PN=''BBBB'']');
    

    SimpleStorage also simplifies almost every other aspect of using the OmniXML and XML in general.