Menu

CopyNode

Anonymous
2014-09-09
2015-05-26
  • Anonymous

    Anonymous - 2014-09-09

    In OmniUtils there was a method: CopyNode to copy complete XML node tree from one node to an other. I can not find one i OXML. Could someone please supply a sample on how to achieve the same?

    Best
    Jens G

     
  • Ondrej Pokorny

    Ondrej Pokorny - 2014-09-10

    There is a function CloneNode in TXMLNode. For whole node tree use "aDeep = true". Don't forget to append the result node wherever you want with AppendChild.

     
  • Anonymous

    Anonymous - 2015-05-23

    I need to clone node to another doc.
    But I receive error from compilator "project1.lpr(43,38) Error: Incompatible type for arg no. 2: Got "IXMLDocument", expected "TXMLDocument"" on CloneNode line. I used IXMLDocument as in OXml examples project and site.


    program project1;
    
    {$mode objfpc}{$H+}
    
    uses
      {$IFDEF UNIX}{$IFDEF UseCThreads}
      cthreads,
      {$ENDIF}{$ENDIF}
      Classes
      { you can add units after this },
      OXmlCDOM;
    
    var
      x1: OXmlCDOM.IXMLDocument;
      x2: OXmlCDOM.IXMLDocument;
      x1NodeRoot, x2NodeRoot, x1Node, x2Node, x1NodeText: OXmlCDOM.TXMLNode;
    begin
      x1 := OXmlCDOM.CreateXMLDoc;
      x2 := OXmlCDOM.CreateXMLDoc;
    
      x1NodeRoot := x1.CreateElement('root');
      x1.SetDocumentElement(x1NodeRoot);
      x1NodeRoot := x1.DocumentElement;
    
      x2NodeRoot := x2.CreateElement('root');
      x2.SetDocumentElement(x2NodeRoot);
      x2NodeRoot := x2.DocumentElement;
    
      x1Node := x1.CreateElement('element');
      x1Node.Attributes['attrib'] := 'attrib_text';
      x1NodeText := x1.CreateTextNode('node_text');
      x1Node.AppendChild(x1NodeText);
      x1NodeRoot.AppendChild(x1Node);
    
      WriteLn('x1');
      WriteLn(x1.XML);
      ReadLn;
    
      WriteLn('x2');
      WriteLn(x2.XML);
      ReadLn;
    
      x2Node := x1Node.CloneNode(true, x2);
    
    end.
    

    Then I try to change IXMLDocument to TXMLDocument. And Clone works.


    program project1;
    
    {$mode objfpc}{$H+}
    
    uses
      {$IFDEF UNIX}{$IFDEF UseCThreads}
      cthreads,
      {$ENDIF}{$ENDIF}
      Classes
      { you can add units after this },
      OXmlCDOM;
    
    var
      x1: OXmlCDOM.TXMLDocument;
      x2: OXmlCDOM.TXMLDocument;
      x1NodeRoot, x2NodeRoot, x1Node, x2Node, x1NodeText: OXmlCDOM.TXMLNode;
    begin
      x1 := OXmlCDOM.TXMLDocument.Create('root');
      x2 := OXmlCDOM.TXMLDocument.Create('root');
    
      x1NodeRoot := x1.DocumentElement;
      x2NodeRoot := x2.DocumentElement;
    
      x1Node := x1.CreateElement('node');
      x1Node.Attributes['attrib'] := 'attrib_text';
      x1NodeText := x1.CreateTextNode('node_text');
      x1Node.AppendChild(x1NodeText);
      x1NodeRoot.AppendChild(x1Node);
    
      WriteLn('x1');
      WriteLn(x1.XML);
      ReadLn;
    
      x2Node := x1Node.CloneNode(true, x2);
      x2NodeRoot.AppendChild(x2Node);
    
      WriteLn('x2');
      WriteLn(x2.XML);
      ReadLn;
    end.
    

    What is difference between IXMLDocument and TXMLDocument? What is right to use?
    How to clone node when using IXMLDocument?

     
  • Ondrej Pokorny

    Ondrej Pokorny - 2015-05-25

    I added TXMLNode.CloneNode2 with IXMLDocument as parameter (rev 81).

    https://sourceforge.net/p/oxml/code/81/

     
  • Anonymous

    Anonymous - 2015-05-25

    Can you rename CloneNode2 to default CloneNode? I tell about overload modifier.

        function CloneNode(const aDeep: Boolean; const aToDocument: TXMLDocument = nil): TXMLNode;
        function CloneNode(const aDeep: Boolean; const aToDocument: IXMLDocument = nil): TXMLNode;overload;
    

    I do not like redundant "2".

     
  • Anonymous

    Anonymous - 2015-05-25

    First I thought that the compiler may be confused about an overloaded interface/class parameter but it looks like the compiler can choose the right function. So I renamed it.

    https://sourceforge.net/p/oxml/code/84

     

Anonymous
Anonymous

Add attachments
Cancel