
XML::XML2JSON - Convert XML into JSON (and back again) using XML::LibXML

use XML::XML2JSON;
my $XML = '<test><element foo="bar"/></test>';
my $XML2JSON = XML::XML2JSON->new();
my $JSON = $XML2JSON->convert($XML);
print $JSON;
my $RestoredXML = $XML2JSON->json2xml($JSON);

I used Google for inspiration: http://code.google.com/apis/gdata/json.html
In short:
Namespace
XML

Creates a new XML::XML2JSON object.
It supports the following arguments:
This is the JSON module that you want to use. By default it will use the first one it finds, in the following order: JSON::Syck, JSON::XS, JSON, JSON::DWIW
An arraryref of element names that should be removed after calling the sanitize method. Children of the elements will be removed as well.
An arrayref of element names that should have their attributes and text content removed after calling the sanitize method. This leaves any children of the elements intact.
An arrayref of attribute names that should be removed after calling the sanitize method.
All attributes will be prefixed by this when converting to JSON. This is "@" by default. You can set this to "", but if you do, any attributes that conflict with a child element name will be lost.
This is the name of the hash key that text content will be added to. This is "$t" by default.
If set to true, child elements that appear only once will be added to a one element array. If set to false, child elements that appear only once will be assesible as a hash value.
The default is false.
If set to true, output will be formatted to be easier to read whenever possible.
If set to true, will print warn messages to describe what it is doing.
Takes an XML string as input. Returns a string of sanitized JSON.
Calling this method is the same as:
my $Obj = $XML2JSON->xml2obj($XML);
$XML2JSON->sanitize($Obj);
my $JSON = $XML2JSON->obj2json($Obj);
This is an alias for convert.
Takes a perl data object as input. Return a string of equivalent JSON.
Takes an XML::LibXML::Document object as input. Returns an equivalent perl data structure.
Takes an xml string as input. Returns an equivalent perl data structure.
Takes a perl hashref as input. (You would normally pass this method the object returned by the xml2obj method.)
This method does not return anything. The object passed into it is directly modified.
Since JSON is often returned directly to a client's browser, there are cases where sensitive data is left in the response.
This method allows you to filter out content that you do not want to be included in the JSON.
This method uses the private_elements, empty_elements and private_attributes arguments which are set when calling the "new" method.
Takes a JSON string as input. Returns a string of equivalent XML.
Calling this method is the same as:
my $Obj = $Self->json2obj($JSON);
my $XML = $Self->obj2xml($Obj);
Takes a json string as input. Returns an equivalent perl data structure.
Takes a perl data structure as input. (Must be a hashref.) Returns an XML::LibXML::Document object.
This method expects the object to be in the same format as would be returned by the xml2obj method.
In short:
Namespace
Caveats:
This method takes the same arguments as obj2dom. Returns the XML as a string.

The order of child elements is not always preserved. This is because the conversion to json makes use of hashes in the resulting json.

Ken Prows - perl(AT)xev.net

Copyright (C) 2007-2008 Ken Prows
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.