Search code examples
xmldelphixml-serializationdelphi-2009omnixml

store xml inside xml


Using OmniXML package, is it possible to store XML code inside another XML file that has its own XML data?

Like this:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<root>
<data>
   <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
      <otherxml>data</otherxml>
</data>

where inside the tag data, everything should be data. Is there an escape char that prevent the parser from parsing the next data into the XML data structure?

Or Does OmniXML comes with support for serialization for this situation?

Any other simple ideas are also welcome.


Solution

  • You can use CDATA:

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <root>
    <data>
       <![CDATA[
       <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
          <otherxml>data</otherxml>
       ]]>
    </data>
    

    Note when you get the value for data, it will be as a string so you'd have to run it through a new XML parser.

    Here is an example code for omniXML:

    var
      xml:IXMLDocument;
      Node:IXMLNode;
    begin
      xml := CreateXMLDoc;    
      xml.SelectSingleNode('/root/data',Node);
      ShowMessage(GetNodeCData(Node,'data',''));
    end;