0.0
No commit activity in last 3 years
No release in over 3 years
xml-purifier
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies
 Project Readme

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