Introducing the XML-Purifier gem
require 'xml-purifier'
raw_xml = File.open('ruby/summary.xml','r').read
# option for to: 'default', 'cdata', or 'escaped'
xml = XMLPurifier.new(xml: raw_xml, from: 'json', to: 'default').to_xml
# or
xml = XMLPurifier.new(xml: raw_xml, from: 'json', to: 'cdata').to_xml
# or
xml = XMLPurifier.new(xml: raw_xml, from: 'json', to: 'escaped').to_xml
output for default:
<summary> <type>input</type> <user>jrobertson</user> <id>14</id> <entry1> <title>titleddd</title> <body>ppy jelly</body> </entry1> <entry2> <title>titleeee</title> <body>ppy jelly2</body> </entry2> </summary>
output for cdata:
<summary> <type>input</type> <user>jrobertson</user> <id>14</id> <entry1><![CDATA[<title>titleddd</title><body>ppy jelly</body>]]></></entry1> <entry2><![CDATA[<title>titleeee</title><body>ppy jelly2</body>]]></></entry2> </summary>
output for escaped:
<summary> <type>input</type> <user>jrobertson</user> <id>14</id> <entry1>&lt;title&gt;titleddd&lt;/title&gt;&lt;body&gt;ppy jelly&lt;/body&gt;</entry1> <entry2>&lt;title&gt;titleeee&lt;/title&gt;&lt;body&gt;ppy jelly2&lt;/body&gt;</entry2> </summary>
file used: summary.xml
<summary> <type>input</type> <user>jrobertson</user> <id>14</id> <entry1>{"title":"titleddd","body":"ppy jelly"}</entry1> <entry2>{"title":"titleeee","body":"ppy jelly2"}</entry2> </summary>
The XML-Purifier gem can rid embedded JSON leaving only pure XML with JSON being replaced by native, cdata, or escaped XML.