Purifies XML from embedded JSON within an XML document.
Ruby
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
lib
MIT-LICENSE
README.md
gem-public_cert.pem
xml-purifier.gemspec

README.md

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>&amp;lt;title&amp;gt;titleddd&amp;lt;/title&amp;gt;&amp;lt;body&amp;gt;ppy jelly&amp;lt;/body&amp;gt;</entry1>
  <entry2>&amp;lt;title&amp;gt;titleeee&amp;lt;/title&amp;gt;&amp;lt;body&amp;gt;ppy jelly2&amp;lt;/body&amp;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.

Resources