.\" Automatically generated by Pod::Man 4.14 (Pod::Simple 3.42) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "XML::Mini::Document 3pm" .TH XML::Mini::Document 3pm "2022-10-15" "perl v5.34.0" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" XML::Mini::Document \- Perl implementation of the XML::Mini Document API. .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 1 \& use XML::Mini::Document; \& \& \& use Data::Dumper; \& \& \& ###### PARSING XML ####### \& \& # create a new object \& my $xmlDoc = XML::Mini::Document\->new(); \& \& # init the doc from an XML string \& $xmlDoc\->parse($XMLString); \& \& # You may use the toHash() method to automatically \& # convert the XML into a hash reference \& my $xmlHash = $xmlDoc\->toHash(); \& \& print Dumper($xmlHash); \& \& \& # You can also manipulate the elements like directly, like this: \& \& # Fetch the ROOT element for the document \& # (an instance of XML::Mini::Element) \& my $xmlRoot = $xmlDoc\->getRoot(); \& \& # play with the element and its children \& # ... \& my $topLevelChildren = $xmlRoot\->getAllChildren(); \& \& foreach my $childElement (@{$topLevelChildren}) \& { \& # ... \& } \& \& \& ###### CREATING XML ####### \& \& # Create a new document from scratch \& \& my $newDoc = XML::Mini::Document\->new(); \& \& # This can be done easily by using a hash: \& my $h = { \& \*(Aqspy\*(Aq => { \& \*(Aqid\*(Aq => \*(Aq007\*(Aq, \& \*(Aqtype\*(Aq => \*(AqSuperSpy\*(Aq, \& \*(Aqname\*(Aq => \*(AqJames Bond\*(Aq, \& \*(Aqemail\*(Aq => \*(Aqmi5@london.uk\*(Aq, \& \*(Aqaddress\*(Aq => \*(AqWherever he is needed most\*(Aq, \& }, \& }; \& \& $newDoc\->fromHash($h); \& \& \& \& # Or new XML can also be created by manipulating \& #elements directly: \& \& my $newDocRoot = $newDoc\->getRoot(); \& \& # create the header \& my $xmlHeader = $newDocRoot\->header(\*(Aqxml\*(Aq); \& # add the version \& $xmlHeader\->attribute(\*(Aqversion\*(Aq, \*(Aq1.0\*(Aq); \& \& my $person = $newDocRoot\->createChild(\*(Aqperson\*(Aq); \& \& my $name = $person\->createChild(\*(Aqname\*(Aq); \& $name\->createChild(\*(Aqfirst\*(Aq)\->text(\*(AqJohn\*(Aq); \& $name\->createChild(\*(Aqlast\*(Aq)\->text(\*(AqDoe\*(Aq); \& \& my $eyes = $person\->createChild(\*(Aqeyes\*(Aq); \& $eyes\->attribute(\*(Aqcolor\*(Aq, \*(Aqblue\*(Aq); \& $eyes\->attribute(\*(Aqnumber\*(Aq, 2); \& \& # output the document \& print $newDoc\->toString(); .Ve .PP This example would output : .PP .Vb 12 \& \& \& \& \& John \& \& \& Doe \& \& \& \& .Ve .SH "DESCRIPTION" .IX Header "DESCRIPTION" The XML::Mini::Document class is the programmer's handle to XML::Mini functionality. .PP 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 \s-1XML\s0 strings. .SS "new [\s-1XMLSTRING\s0]" .IX Subsection "new [XMLSTRING]" Creates a new instance of XML::Mini::Document, optionally calling fromString with the passed \s-1XMLSTRING\s0 .SS "getRoot" .IX Subsection "getRoot" Returns a reference the this document's root element (an instance of XML::Mini::Element) .SS "setRoot \s-1NEWROOT\s0" .IX Subsection "setRoot NEWROOT" setRoot \s-1NEWROOT\s0 Set the document root to the \s-1NEWROOT\s0 XML::Mini::Element object. .SS "isElement \s-1ELEMENT\s0" .IX Subsection "isElement ELEMENT" Returns a true value if \s-1ELEMENT\s0 is an instance of XML::Mini::Element, false otherwise. .SS "isNode \s-1NODE\s0" .IX Subsection "isNode NODE" Returns a true value if \s-1NODE\s0 is an instance of XML::MiniNode, false otherwise. .SS "createElement \s-1NAME\s0 [\s-1VALUE\s0]" .IX Subsection "createElement NAME [VALUE]" Creates a new XML::Mini::Element with name \s-1NAME.\s0 .PP This element is an orphan (has no assigned parent) and will be lost unless it is appended (\fBXML::Mini::Element::appendChild()\fR) to an element at some point. .PP If the optional \s-1VALUE\s0 (string or numeric) parameter is passed, the new element's text/numeric content will be set using \s-1VALUE.\s0 Returns a reference to the newly created element. .SS "getElement \s-1NAME\s0 [\s-1POSITON\s0]" .IX Subsection "getElement NAME [POSITON]" Searches the document for an element with name \s-1NAME.\s0 .PP Returns a reference to the first XML::Mini::Element with name \s-1NAME,\s0 if found, \s-1NULL\s0 otherwise. .PP \&\s-1NOTE:\s0 The search is performed like this, returning the first element that matches: .PP .Vb 4 \& \- Check the Root Element\*(Aqs 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\*(Aqs immediate \& children in order and then asking them to getElement()) .Ve .PP If a numeric \s-1POSITION\s0 parameter is passed, \fBgetElement()\fR will return only the POSITIONth element of name \s-1NAME\s0 (starting at 1). Thus, on document .PP .Vb 12 \& \& \& \& bob \& \& \& jane \& \& \& ralph \& \& .Ve .PP \&\f(CW$people\fR\->getElement('person') will return the element containing the text node \&'bob', while \f(CW$people\fR\->getElement('person', 3) will return the element containing the text 'ralph'. .SS "getElementByPath \s-1PATH\s0 [\s-1POSITIONARRAY\s0]" .IX Subsection "getElementByPath PATH [POSITIONARRAY]" Attempts to return a reference to the (first) element at \s-1PATH\s0 where \s-1PATH\s0 is the path in the structure from the root element to the requested element. .PP For example, in the document represented by: .PP .Vb 10 \& \& \& \& \& \& \& DA42 \& \& \& D99983FFF \& \& \& ss\-839uent \& \& \& \& \& $accessid = $xmlDocument\->getElementByPath(\*(AqpartRateRequest/vendor/accessid\*(Aq); .Ve .PP Will return what you expect (the accessid element with attributes user = \*(L"myusername\*(R" and password = \*(L"mypassword\*(R"). .PP \&\s-1BUT\s0 be careful: .PP .Vb 1 \& my $accessid = $xmlDocument\->getElementByPath(\*(AqpartRateRequest/partList/partNum\*(Aq); .Ve .PP will return the partNum element with the value \*(L"\s-1DA42\*(R".\s0 To access other partNum elements you must either use the \s-1POSITIONSARRAY\s0 or the \fBgetAllChildren()\fR method on the partRateRequest element. .PP \&\s-1POSITIONSARRAY\s0 functions like the \s-1POSITION\s0 parameter to \fBgetElement()\fR, 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 .PP .Vb 1 \& my $thirdPart = $xmlDocument\->getElementByPath(\*(AqpartRateRequest/partList/partNum\*(Aq, 1, 1, 3); .Ve .PP 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'). .PP Returns the XML::Mini::Element reference if found, \s-1NULL\s0 otherwise. .SS "parse \s-1SOURCE\s0" .IX Subsection "parse SOURCE" Initialise the XML::Mini::Document (and its root XML::Mini::Element) using the \&\s-1XML\s0 from file \s-1SOURCE.\s0 .PP \&\s-1SOURCE\s0 may be a string containing your \s-1XML\s0 document. .PP In addition to parsing strings, possible SOURCEs are: .PP .Vb 2 \& # a file location string \& $miniXMLDoc\->parse(\*(Aq/path/to/file.xml\*(Aq); \& \& # an open file handle \& open(INFILE, \*(Aq/path/to/file.xml\*(Aq); \& $miniXMLDoc\->parse(*INFILE); \& \& # an open FileHandle object \& my $fhObj = FileHandle\->new(); \& $fhObj\->open(\*(Aq/path/to/file.xml\*(Aq); \& $miniXML\->parse($fhObj); .Ve .PP In all cases where \s-1SOURCE\s0 is a file or file handle, XML::Mini takes care of slurping the contents and closing the handle. .SS "fromHash \s-1HASHREF\s0 [\s-1OPTIONS\s0]" .IX Subsection "fromHash HASHREF [OPTIONS]" Parses a \*(L"hash representation\*(R" of your \s-1XML\s0 structure. For each key => value pair within the hash ref, XML::Mini will create an element of name 'key' : .PP .Vb 1 \& \- with the text contents set to \*(Aqvalue\*(Aq if \*(Aqvalue\*(Aq is a string \& \& \- for each element of \*(Aqvalue\*(Aq if value is an ARRAY REFERENCE \& \& \- with suitable children for each subkey => subvalue if \*(Aqvalue\*(Aq is a HASH REFERENCE. .Ve .PP For instance, if \fBfromHash()\fR is passed a simple hash ref like: .PP .Vb 1 \& my $h = { \& \& \*(Aqspy\*(Aq => { \& \*(Aqid\*(Aq => \*(Aq007\*(Aq, \& \*(Aqtype\*(Aq => \*(AqSuperSpy\*(Aq, \& \*(Aqname\*(Aq => \*(AqJames Bond\*(Aq, \& \*(Aqemail\*(Aq => \*(Aqmi5@london.uk\*(Aq, \& \*(Aqaddress\*(Aq => \*(AqWherever he is needed most\*(Aq, \& }, \& }; .Ve .PP then : .PP .Vb 2 \& $xmlDoc\->fromHash($h); \& print $xmlDoc\->toString(); .Ve .PP will output .PP .Vb 7 \& \& mi5@london.uk \& James Bond \&
Wherever he is needed most
\& SuperSpy \& 007 \&
.Ve .PP The optional \s-1OPTIONS\s0 parameter may be used to specify which keys to use as attributes (instead of creating subelements). For example, calling .PP .Vb 7 \& my $options = { \& \*(Aqattributes\*(Aq => { \& \*(Aqspy\*(Aq => \*(Aqid\*(Aq, \& \*(Aqemail\*(Aq => \*(Aqtype\*(Aq, \& \*(Aqfriend\*(Aq => [\*(Aqname\*(Aq, \*(Aqage\*(Aq], \& } \& }; \& \& \& my $h = { \& \& \*(Aqspy\*(Aq => { \& \*(Aqid\*(Aq => \*(Aq007\*(Aq, \& \*(Aqtype\*(Aq => \*(AqSuperSpy\*(Aq, \& \*(Aqname\*(Aq => \*(AqJames Bond\*(Aq, \& \*(Aqemail\*(Aq => { \& \*(Aqtype\*(Aq => \*(Aqprivate\*(Aq, \& \*(Aq\-content\*(Aq => \*(Aqmi5@london.uk\*(Aq, \& \& }, \& \*(Aqaddress\*(Aq => { \& \*(Aqtype\*(Aq => \*(Aqresidential\*(Aq, \& \*(Aq\-content\*(Aq => \*(AqWherever he is needed most\*(Aq, \& }, \& \& \*(Aqfriend\*(Aq => [ \& { \& \*(Aqname\*(Aq => \*(Aqclaudia\*(Aq, \& \*(Aqage\*(Aq => 25, \& \*(Aqtype\*(Aq => \*(Aqclose\*(Aq, \& }, \& \& { \& \*(Aqname\*(Aq => \*(Aqmonneypenny\*(Aq, \& \*(Aqage\*(Aq => \*(Aq40something\*(Aq, \& \*(Aqtype\*(Aq => \*(Aqtease\*(Aq, \& }, \& \& { \& \*(Aqname\*(Aq => \*(AqQ\*(Aq, \& \*(Aqage\*(Aq => \*(Aq10E4\*(Aq, \& \*(Aqtype\*(Aq => \*(Aqpain\*(Aq, \& } \& ], \& \& }, \& }; \& \& \& $xmlDoc\->fromHash($h, $options); \& print $xmlDoc\->toString(); .Ve .PP will output something like: .PP .Vb 10 \& \& James Bond \& mi5@london.uk \&
\& residential \& Wherever he is needed most \&
\& SuperSpy \& \& close \& \& \& tease \& \& \& pain \& \&
.Ve .PP 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. .PP \&\s-1NOTE:\s0 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. .PP See \s-1ALSO:\s0 the documentation for the related \fBtoHash()\fR method. .PP Still \s-1TODO:\s0 Create some better docs for this! For the moment you can take a peek within the test suite of the source distribution. .SS "fromString \s-1XMLSTRING\s0" .IX Subsection "fromString XMLSTRING" Initialise the XML::Mini::Document (and it's root XML::Mini::Element) using the \&\s-1XML\s0 string \s-1XMLSTRING.\s0 .PP Returns the number of immediate children the root XML::Mini::Element now has. .SS "fromFile \s-1FILENAME\s0" .IX Subsection "fromFile FILENAME" Initialise the XML::Mini::Document (and it's root XML::Mini::Element) using the \&\s-1XML\s0 from file \s-1FILNAME.\s0 .PP Returns the number of immediate children the root XML::Mini::Element now has. .SS "toString [\s-1DEPTH\s0]" .IX Subsection "toString [DEPTH]" Converts this XML::Mini::Document object to a string and returns it. .PP The optional \s-1DEPTH\s0 may be passed to set the space offset for the first element. .PP If the optional \s-1DEPTH\s0 is set to \f(CW$XML::Mini::NoWhiteSpaces\fR no \en or whitespaces will be inserted in the xml string (ie it will all be on a single line with no spaces between the tags. .PP Returns a string of \s-1XML\s0 representing the document. .SS "toFile \s-1FILENAME\s0 [\s-1SAFE\s0]" .IX Subsection "toFile FILENAME [SAFE]" Stringify and save the \s-1XML\s0 document to file \s-1FILENAME\s0 .PP If \s-1SAFE\s0 flag is passed and is a true value, toFile will do some extra checking, refusing to open the file if the filename matches m|/\e.\e./| or m|#;`\e*| or if \s-1FILENAME\s0 points to a softlink. In addition, if \s-1SAFE\s0 is '\s-1NOOVERWRITE\s0', toFile will fail if the \s-1FILENAME\s0 already exists. .SS "toHash" .IX Subsection "toHash" Transform the \s-1XML\s0 structure internally represented within the object (created manually or parsed from a file or string) into a \s-1HASH\s0 reference and returns the href. .PP For instance, if this \s-1XML\s0 is \fBparse()\fRd: .PP .PP .Vb 6 \& \& mi5@london.uk \& James Bond \&
Wherever he is needed most
\& SuperSpy \&
\& \& \& I am not a man, I am a free number \& Number 6 \& prisoner@aol.com \&
6 Prison Island Road, Prison Island, Somewhere
\&
.Ve .PP
.PP The hash reference returned will look like this (as output by Data::Dumper): .PP .Vb 1 \& \*(Aqpeople\*(Aq => { \& \& \*(Aqperson\*(Aq => [ \& { \& \*(Aqemail\*(Aq => \*(Aqmi5@london.uk\*(Aq, \& \*(Aqname\*(Aq => \*(AqJames Bond\*(Aq, \& \*(Aqtype\*(Aq => \*(AqSuperSpy\*(Aq, \& \*(Aqaddress\*(Aq => \*(AqWherever he is needed most\*(Aq, \& \*(Aqid\*(Aq => \*(Aq007\*(Aq \& }, \& { \& \*(Aqemail\*(Aq => { \& \*(Aqtype\*(Aq => \*(Aqprivate\*(Aq, \& \*(Aq\-content\*(Aq => \*(Aqprisoner@aol.com\*(Aq \& }, \& \*(Aqcomment\*(Aq => \*(AqI am not a man, I am a free number\*(Aq, \& \*(Aqnumber\*(Aq => \*(Aq6\*(Aq, \& \*(Aqname\*(Aq => \*(AqNumber 6\*(Aq, \& \*(Aqaddress\*(Aq => \*(Aq6 Prison Island Road, Prison Island, Somewhere\*(Aq, \& \*(Aqid\*(Aq => \*(Aq006\*(Aq \& } \& ] \& } .Ve .SS "getValue" .IX Subsection "getValue" Utility function, call the root XML::Mini::Element's \fBgetValue()\fR .SS "dump" .IX Subsection "dump" Debugging aid, dump returns a nicely formatted dump of the current structure of the XML::Mini::Document object. .SH "CAVEATS" .IX Header "CAVEATS" It is impossible to parse \*(L"cross-nested\*(R" tags using regular expressions (i.e. sequences of the form ...). However, if you have the Text::Balanced module installed (it is installed by default with Perl 5.8), such sequences will be handled flawlessly. .PP Even if you do not have the Text::Balanced module available, it is still possible to generate this type of \s-1XML\s0 \- the problem only appears when parsing. .SH "AUTHOR" .IX Header "AUTHOR" Copyright (C) 2002\-2008 Patrick Deegan, Psychogenic Inc. .PP Programs that use this code are bound to the terms and conditions of the \s-1GNU GPL\s0 (see the \s-1LICENSE\s0 file). If you wish to include these modules in non-GPL code, you need prior written authorisation from the authors. .PP This library is released under the terms of the \s-1GNU GPL\s0 version 3, making it available only for free programs (\*(L"free\*(R" here being used in the sense of the \s-1GPL,\s0 see http://www.gnu.org for more details). Anyone wishing to use this library within a proprietary or otherwise non-GPLed program \s-1MUST\s0 contact psychogenic.com to acquire a distinct license for their application. This approach encourages the use of free software while allowing for proprietary solutions that support further development. .SS "\s-1LICENSE\s0" .IX Subsection "LICENSE" .Vb 3 \& XML::Mini::Document module, part of the XML::Mini XML parser/generator package. \& Copyright (C) 2002\-2008 Patrick Deegan \& All rights reserved \& \& XML::Mini is free software: you can redistribute it and/or modify \& it under the terms of the GNU General Public License as published by \& the Free Software Foundation, either version 3 of the License, or \& (at your option) any later version. \& \& XML::Mini is distributed in the hope that it will be useful, \& but WITHOUT ANY WARRANTY; without even the implied warranty of \& MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the \& GNU General Public License for more details. \& \& You should have received a copy of the GNU General Public License \& along with XML::Mini. If not, see . .Ve .SH "SEE ALSO" .IX Header "SEE ALSO" XML::Mini, XML::Mini::Element .PP http://minixml.psychogenic.com