The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.


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:

In short:

  • The response is represented as a JSON object; each nested element or attribute is represented as a name/value property of the object.

  • Attributes are converted to String properties.

  • Attribute names are prefixed with "@" so that they dont conflict with child elements of the same name.

  • Child elements are converted to Object properties.

  • Text values of tags are converted to $t properties.


  • If an element has a namespace alias, the alias and element are concatenated using "$". For example, ns:element becomes ns$element.


  • XML version and encoding attributes are converted to attribute version and encoding of the root element, respectively.



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);
        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:

  • The root hashref may only have a single hashref key. That key will become the xml document's root.

  • A hashref will be converted to an element.

  • An arraysref of hashrefs will be converted into multiple child elements. Their names will be set to the name of the arrayref's hash key.

  • If an attribute is prefixed by an "@", the "@" will be removed.

  • A hashkey named "$t" will be converted into text content for the current element.


  • If a namespace alias has a "$", it will be replaced using ":". For example, ns$element becomes ns:element.


  • The order of child elements and attributes cannot be determined.


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)


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.