|
NAMEXML::XML2JSON - Convert XML into JSON (and back again) using XML::LibXMLSYNOPSISuse 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); DESCRIPTIONI used Google for inspiration: http://code.google.com/apis/gdata/json.htmlIn short:
Namespace
XML
METHODSnewCreates 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. convertTakes 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); xml2jsonThis is an alias for convert.obj2jsonTakes a perl data object as input. Return a string of equivalent JSON.dom2objTakes an XML::LibXML::Document object as input. Returns an equivalent perl data structure.xml2objTakes an xml string as input. Returns an equivalent perl data structure.sanitizeTakes 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. json2xmlTakes 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); json2objTakes a json string as input. Returns an equivalent perl data structure.obj2domTakes 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:
obj2xmlThis method takes the same arguments as obj2dom. Returns the XML as a string.CAVEATSThe order of child elements is not always preserved. This is because the conversion to json makes use of hashes in the resulting json.AUTHORKen Prows - perl(AT)xev.netCOPYRIGHT & LICENSECopyright (C) 2007-2008 Ken ProwsThis library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
Visit the GSP FreeBSD Man Page Interface. |