GSP
Quick Navigator

Search Site

Unix VPS
A - Starter
B - Basic
C - Preferred
D - Commercial
MPS - Dedicated
Previous VPSs
* Sign Up! *

Support
Contact Us
Online Help
Handbooks
Domain Status
Man Pages

FAQ
Virtual Servers
Pricing
Billing
Technical

Network
Facilities
Connectivity
Topology Map

Miscellaneous
Server Agreement
Year 2038
Credits
 

USA Flag

 

 

Man Pages
XML::XML2JSON(3) User Contributed Perl Documentation XML::XML2JSON(3)

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:

  • 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.

Namespace

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

XML

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:

module

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

private_elements

An arraryref of element names that should be removed after calling the sanitize method. Children of the elements will be removed as well.

empty_elements

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.

private_attributes

An arrayref of attribute names that should be removed after calling the sanitize method.

attribute_prefix

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.

content_key

This is the name of the hash key that text content will be added to. This is "$t" by default.

force_array

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.

pretty

If set to true, output will be formatted to be easier to read whenever possible.

debug

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:

  • 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.

Namespace

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

Caveats:

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)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.

2012-01-22 perl v5.32.1

Search for    or go to Top of page |  Section 3 |  Main Index

Powered by GSP Visit the GSP FreeBSD Man Page Interface.
Output converted with ManDoc.