11

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.

menjaraz
  • 7,551
  • 4
  • 41
  • 81
none
  • 4,669
  • 14
  • 62
  • 102

2 Answers2

23

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;
menjaraz
  • 7,551
  • 4
  • 41
  • 81
Mike Christensen
  • 88,082
  • 50
  • 208
  • 326
  • No need to run it , it needs to be written to create xml file. – none Jan 26 '12 at 09:24
  • 1
    Note that there can be encoding problems. For example, if the embedded xml uses , and the outer xml uses UTF-8, a XML parser must still process the inner xml as UTF-8. – mjn Mar 31 '12 at 11:47
3

if the content in data doesn't have to be read rigth away you can encode it in for example Base64 or UUEncode.

Then you can extract, decode and parse the data

Daniel Luyo
  • 1,326
  • 13
  • 19
  • 3
    That's definitely the hard (and slower) way to do it, since you still have to store the encoded text in a `CDATA` section. – Ken White Jan 25 '12 at 20:33
  • 4
    If your embedded XML may also contain CDATA then I would think you must encode it. I would say that this is the preferred way due to that reason. – Darian Miller Jan 25 '12 at 21:00
  • @DarianMiller appears to be spot on there. – David Heffernan Jan 25 '12 at 21:31
  • 1
    @Darian, good point. Glad I just commented and didn't vote. :) Thanks for mentioning that - I'd missed it. – Ken White Jan 25 '12 at 21:40
  • 1
    @Darian, it is possible to "escape" a CDATA delimiter. `<![CDATA[abcdef]]>` is the same as `<![CDATA[abc]]><![CDATA[def]]>`. Using this fact, we can split any instance of `]]>` in the string across two CDATA blocks, so it won't be picked up as a delimiter. That is, just replace every occurrence of `]]>` with `]]]]><[CDATA[>`. – Nick Barnes Jan 26 '12 at 02:08
  • +1 this solution avoids encoding problems, if inner and outer XML use different encodings – mjn Mar 31 '12 at 11:49