NAME

XML::MyXML - A simple-to-use XML module, for parsing and creating XML documents

VERSION

Version 0.093

SYNOPSIS

use XML::MyXML qw(tidy_xml xml_to_object);
use XML::MyXML qw(:all);

my $xml = "<item><name>Table</name><price><usd>10.00</usd><eur>8.50</eur></price></item>";
print tidy_xml($xml);

my $obj = xml_to_object($xml);
print "Price in Euros = " . $obj->path('price/eur')->value;

$obj->simplify is hashref { item => { name => 'Table', price => { usd => '10.00', eur => '8.50' } } }
$obj->simplify({ internal => 1 }) is hashref { name => 'Table', price => { usd => '10.00', eur => '8.50' } }

EXPORT

tidy_xml, xml_to_object, object_to_xml, simple_to_xml, xml_to_simple, check_xml

DESCRIPTION

A simple-to-use XML module, for parsing and creating XML documents

FEATURES & LIMITATIONS

This module can handle XML comments, CDATA sections, and XML entities (the standard five as well as numeric (dec & hex) ones)

But it will ignore (won't parse) <!DOCTYPE...> and <?...?> special markup

Parsed documents must have the UTF-8 encoding, as all XML documents produced by this module will

Attribute values may not contain the > character

FUNCTION FLAGS

Some functions or methods in this module accept flags, listed under each function in the documentation. They are optional, default to zero, and can be used as follows: &function_name( $param1, { flag1 => 1, flag2 => 1 } ). What they do when set is this:

strip : the function will strip initial and ending whitespace from all text values returned

file : the function will expect the path to a file containing an XML document to parse, instead of an XML string

complete : the function's XML output will include an XML declaration in the beginning

soft : the function will return undef instead of dying in case of a error during XML parsing

internal : the function will only return the contents of an element in a hashref instead of the element itself (see "SYNOPISIS" for example)

FUNCTIONS

tidy_xml($raw_xml)

Returns the XML string in a tidy format (with tabs & newlines)

Optional flags: file, complete

xml_to_object($raw_xml)

Creates an 'XML::MyXML::Object' object from the raw XML provided

Optional flags: file, soft

object_to_xml($object)

Creates an XML string from the 'XML::MyXML::Object' object provided

Optional flags: complete

simple_to_xml($simple_array_ref)

Produces a raw XML string from either an array reference, a hash reference or a mixed structure such as these examples:

{ thing => { name => 'John', location => { city => 'New York', country => 'U.S.A.' } } }
[ thing => [ name => 'John', location => [ city => 'New York', country => 'U.S.A.' ] ] ]
{ thing => { name => 'John', location => [ city => 'New York', city => 'Boston', country => 'U.S.A.' ] } }

Optional flags: complete

xml_to_simple($raw_xml)

Produces a very simple hash object from the raw XML string provided. An example hash object created thusly is this: { thing => { name => 'John', location => { city => 'New York', country => 'U.S.A.' } } }

Since the object created is a hashref, duplicate keys will be discarded. WARNING: This function only works on very simple XML strings, i.e. children of an element may not consist of both text and elements (child elements will be discarded in that case)

Optional flags: internal, strip, file, soft

check_xml($raw_xml)

Returns 1 if the $raw_xml string is valid XML (valid enough to be used by this module), and 0 otherwise.

Optional flags: file

OBJECT METHODS

$obj->path("subtag1/subsubtag2/.../subsubsubtagX")

Returns the element specified by the path as an XML::MyXML::Object object. When there are more than one tags with the specified name in the last step of the path, it will return all of them as an array. In scalar context will only return the first one.

$obj->value

When the element represented by the $obj object has only text contents, returns those contents as a string. If the $obj element has no contents, value will return an empty string.

Optional flags: strip

$obj->attr('attrname')

Returns the value of the 'attrname' attribute of top element. Returns undef if attribute does not exist.

$obj->tag

Returns the tag of the $obj element (after stripping it from namespaces). E.g. if $obj represents an <rss:item> element, $obj->tag will just return the name iitem'. Returns undef if $obj doesn't represent a tag.

$obj->simplify

Returns a very simple hashref, like the one returned with &XML::MyXML::xml_to_simple. Same restrictions and warnings apply.

Optional flags: internal, strip

$obj->to_xml

Returns the XML string of the object, just like calling &object_to_xml( $obj )

Optional flags: complete

$obj->to_tidy_xml

Returns the XML string of the object in tidy form, just like calling &tidy_xml( &object_to_xml( $obj ) )

Optional flags: complete

AUTHOR

Alexander Karelas, <karjala at karjala.org>

BUGS

Please report any bugs or feature requests to bug-xml-myxml at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=XML-MyXML. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

SUPPORT

You can find documentation for this module with the perldoc command.

perldoc XML::MyXML

You can also look for information at:

ACKNOWLEDGEMENTS

COPYRIGHT & LICENSE

Copyright 2006-2007 Alexander Karelas, all rights reserved.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.