The XML::Mini::Document class is the programmer's handle to XML::Mini functionality.
A XML::Mini::Document instance is created in every program that uses XML::Mini. With the
XML::Mini::Document object, you can access the root XML::Mini::Element, find/fetch/create elements and
read in or output XML strings.
new[XMLSTRING]
Creates a new instance of XML::Mini::Document, optionally calling fromString with the passed XMLSTRING
getRoot
Returns a reference the this document's root element (an instance of XML::Mini::Element)
setRootNEWROOT
setRoot NEWROOT Set the document root to the NEWROOT XML::Mini::Element object.
isElementELEMENT
Returns a true value if ELEMENT is an instance of XML::Mini::Element, false otherwise.
isNodeNODE
Returns a true value if NODE is an instance of XML::MiniNode, false otherwise.
createElementNAME[VALUE]
Creates a new XML::Mini::Element with name NAME.
This element is an orphan (has no assigned parent) and will be lost unless it is appended
(XML::Mini::Element::appendChild()) to an element at some point.
If the optional VALUE (string or numeric) parameter is passed, the new element's text/numeric content
will be set using VALUE. Returns a reference to the newly created element.
getElementNAME[POSITON]
Searches the document for an element with name NAME.
Returns a reference to the first XML::Mini::Element with name NAME, if found, NULL otherwise.
NOTE: The search is performed like this, returning the first element that matches:
- Check the Root Element's immediate children (in order) for a match.
- Ask each immediate child (in order) to XML::Mini::Element::getElement()
(each child will then proceed similarly, checking all it's immediate
children in order and then asking them to getElement())
If a numeric POSITION parameter is passed, getElement() will return only the POSITIONth element of name
NAME (starting at 1). Thus, on document
<?xml version="1.0"?>
<people>
<person>
bob
</person>
<person>
jane
</person>
<person>
ralph
</person>
</people>
$people->getElement('person') will return the element containing the text node 'bob', while
$people->getElement('person', 3) will return the element containing the text 'ralph'.
getElementByPathPATH[POSITIONARRAY]
Attempts to return a reference to the (first) element at PATH where PATH is the path in the structure
from the root element to the requested element.
For example, in the document represented by:
<partRateRequest>
<vendor>
<accessid user="myusername" password="mypassword" />
</vendor>
<partList>
<partNum>
DA42
</partNum>
<partNum>
D99983FFF
</partNum>
<partNum>
ss-839uent
</partNum>
</partList>
</partRateRequest>
$accessid = $xmlDocument->getElementByPath('partRateRequest/vendor/accessid');
Will return what you expect (the accessid element with attributes user = "myusername" and password =
"mypassword").
BUT be careful:
my $accessid = $xmlDocument->getElementByPath('partRateRequest/partList/partNum');
will return the partNum element with the value "DA42". To access other partNum elements you must either
use the POSITIONSARRAY or the getAllChildren() method on the partRateRequest element.
POSITIONSARRAY functions like the POSITION parameter to getElement(), but instead of specifying the
position of a single element, you must indicate the position of all elements in the path. Therefore, to
get the third part number element, you would use
my $thirdPart = $xmlDocument->getElementByPath('partRateRequest/partList/partNum', 1, 1, 3);
The additional 1,1,3 parameters indicate that you wish to retrieve the 1st partRateRequest element in the
document, the 1st partList child of partRateRequest and the 3rd partNum child of the partList element (in
this instance, the partNum element that contains 'ss-839uent').
Returns the XML::Mini::Element reference if found, NULL otherwise.
parseSOURCE
Initialise the XML::Mini::Document (and its root XML::Mini::Element) using the XML from file SOURCE.
SOURCE may be a string containing your XML document.
In addition to parsing strings, possible SOURCEs are:
# a file location string
$miniXMLDoc->parse('/path/to/file.xml');
# an open file handle
open(INFILE, '/path/to/file.xml');
$miniXMLDoc->parse(*INFILE);
# an open FileHandle object
my $fhObj = FileHandle->new();
$fhObj->open('/path/to/file.xml');
$miniXML->parse($fhObj);
In all cases where SOURCE is a file or file handle, XML::Mini takes care of slurping the contents and
closing the handle.
fromHashHASHREF[OPTIONS]
Parses a "hash representation" of your XML structure. For each key => value pair within the hash ref,
XML::Mini will create an element of name 'key' :
- with the text contents set to 'value' if 'value' is a string
- for each element of 'value' if value is an ARRAY REFERENCE
- with suitable children for each subkey => subvalue if 'value' is a HASH REFERENCE.
For instance, if fromHash() is passed a simple hash ref like:
my $h = {
'spy' => {
'id' => '007',
'type' => 'SuperSpy',
'name' => 'James Bond',
'email' => 'mi5@london.uk',
'address' => 'Wherever he is needed most',
},
};
then :
$xmlDoc->fromHash($h);
print $xmlDoc->toString();
will output
<spy>
<email> mi5@london.uk </email>
<name> James Bond </name>
<address> Wherever he is needed most </address>
<type> SuperSpy </type>
<id> 007 </id>
</spy>
The optional OPTIONS parameter may be used to specify which keys to use as attributes (instead of
creating subelements). For example, calling
my $options = {
'attributes' => {
'spy' => 'id',
'email' => 'type',
'friend' => ['name', 'age'],
}
};
my $h = {
'spy' => {
'id' => '007',
'type' => 'SuperSpy',
'name' => 'James Bond',
'email' => {
'type' => 'private',
'-content' => 'mi5@london.uk',
},
'address' => {
'type' => 'residential',
'-content' => 'Wherever he is needed most',
},
'friend' => [
{
'name' => 'claudia',
'age' => 25,
'type' => 'close',
},
{
'name' => 'monneypenny',
'age' => '40something',
'type' => 'tease',
},
{
'name' => 'Q',
'age' => '10E4',
'type' => 'pain',
}
],
},
};
$xmlDoc->fromHash($h, $options);
print $xmlDoc->toString();
will output something like:
<spy id="007">
<name> James Bond </name>
<email type="private"> mi5@london.uk </email>
<address>
<type> residential </type>
Wherever he is needed most
</address>
<type> SuperSpy </type>
<friend age="25" name="claudia">
<type> close </type>
</friend>
<friend age="40something" name="monneypenny">
<type> tease </type>
</friend>
<friend age="10E4" name="Q">
<type> pain </type>
</friend>
</spy>
As demonstrated above, you can use the optional href to specify tags for which attributes (instead of
elements) should be created and you may nest hash and array refs to create complex structures.
NOTE: Whenever a hash references is used you lose the sequence in which the elements are placed - only
the array references (which create a list of identically named elements) can preserve their order.
See ALSO: the documentation for the related toHash() method.
Still TODO: Create some better docs for this! For the moment you can take a peek within the test suite
of the source distribution.
fromStringXMLSTRING
Initialise the XML::Mini::Document (and it's root XML::Mini::Element) using the XML string XMLSTRING.
Returns the number of immediate children the root XML::Mini::Element now has.
fromFileFILENAME
Initialise the XML::Mini::Document (and it's root XML::Mini::Element) using the XML from file FILNAME.
Returns the number of immediate children the root XML::Mini::Element now has.
toString[DEPTH]
Converts this XML::Mini::Document object to a string and returns it.
The optional DEPTH may be passed to set the space offset for the first element.
If the optional DEPTH is set to $XML::Mini::NoWhiteSpaces no \n or whitespaces will be inserted in the
xml string (ie it will all be on a single line with no spaces between the tags.
Returns a string of XML representing the document.
toFileFILENAME[SAFE]
Stringify and save the XML document to file FILENAME
If SAFE flag is passed and is a true value, toFile will do some extra checking, refusing to open the file
if the filename matches m|/\.\./| or m|#;`\*| or if FILENAME points to a softlink. In addition, if SAFE
is 'NOOVERWRITE', toFile will fail if the FILENAME already exists.
toHash
Transform the XML structure internally represented within the object (created manually or parsed from a
file or string) into a HASH reference and returns the href.
For instance, if this XML is parse()d:
<people>
<person id="007">
<email> mi5@london.uk </email>
<name> James Bond </name>
<address> Wherever he is needed most </address>
<type> SuperSpy </type>
</person>
<person id="006" number="6">
<comment> I am not a man, I am a free number </comment>
<name> Number 6 </name>
<email type="private"> prisoner@aol.com </email>
<address> 6 Prison Island Road, Prison Island, Somewhere </address>
</person>
</people>
The hash reference returned will look like this (as output by Data::Dumper):
'people' => {
'person' => [
{
'email' => 'mi5@london.uk',
'name' => 'James Bond',
'type' => 'SuperSpy',
'address' => 'Wherever he is needed most',
'id' => '007'
},
{
'email' => {
'type' => 'private',
'-content' => 'prisoner@aol.com'
},
'comment' => 'I am not a man, I am a free number',
'number' => '6',
'name' => 'Number 6',
'address' => '6 Prison Island Road, Prison Island, Somewhere',
'id' => '006'
}
]
}
getValue
Utility function, call the root XML::Mini::Element's getValue()dump
Debugging aid, dump returns a nicely formatted dump of the current structure of the XML::Mini::Document
object.