Home Overview API  Download  About 
 

 

MiniXML allows you to generate and parse XML content within your own applications. To see some example code, check out the Overview section.

This page presents the complete MiniXML API (programming interface). This includes the interfaces to the MiniXMLDoc, MiniXMLElement and MiniXMLNode classes. As a third party programmer, using MiniXML in your own code, you will only need to be concerned with the MiniXMLDoc and MiniXMLElement classes.

Note: This text was originally written with only PHP in mind. It is currently being revised but the API is practically identical for Perl (see the XML::Mini, XML::Mini::Document and XML::Mini::Element modules' pod for details), the main differences lying in the class names and the lack of a funky =& reference assignment operator in perl.

 

MiniXMLDoc

The MiniXMLDoc class is the programmer's handle to MiniXML functionality.

A MiniXMLDoc instance is created in every program that uses MiniXML. With the MiniXMLDoc object, you can access the root MiniXMLElement, find/fetch/create elements and read in or output XML strings.

Methods

MiniXMLDoc
createElement
fromString
fromFile
fromArray
getElement
getElementByPath
getRoot
getValue
setRoot
toArray
toString

Method MiniXMLDoc
Description Constructor, creates and inits a MiniXMLDoc object.
Return An instance of MiniXMLDoc
Notes  

 

Method getRoot
Description Fetches the MiniXMLDoc's root element
Return Returns a reference to the document's root element
Notes The root element is an instance of MiniXMLElement

 

Method setRoot NEWROOT
Description Set the document root to the NEWROOT MiniXMLElement object
Return  
Notes  

 

Method createElement NAME [VALUE]
Description

Creates a new MiniXMLElement with name NAME. This element is an orphan (has no assigned parent) and will be lost unless it is appended (using MiniXMLElement::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.

Return Returns a reference to the newly created element
Notes You should use the =& reference assignment operator

 

Method getElement NAME
Description

Searches the document for an element with name NAME.

MiniXMLElement with name NAME,
if found, NULL otherwise.

Return Returns a reference to the first MiniXMLElement with name NAME if found, NULL otherwise
Notes

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 MiniXMLElement::getElement()
(each child will then proceed similarly, checking all it's immediate children in order and then asking them to getElement())

 

Method getElementByPath PATH
Description

Attempts to find and 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 see the Note below.

Return Returns the MiniXMLElement reference if found, NULL otherwise.
Notes

be careful:

In the example above,
$part =& $xmlDocument->getElementByPath('partRateRequest/partList/partNum');

will return the partNum element with the value "DA42". Other partNums are inaccessible by getElementByPath() as they all share the same path - Use MiniXMLElement::getAllChildren() on the partList parent instead.

 

Method fromArray ARRAY [OPTIONS]
Description

Initialise the MiniXMLDoc (and its root MiniXMLElement object) based on the contents of ARRAY. See the oveview for an example. The ARRAY is interpreted as follows - for each KEY => VALUE pair in the array, an element with name KEY is created. If the associated VALUE is

  • a string, the KEY element simply contains the string.
  • an associative array (a hash ref in Perl), the KEY element contains child elements for each key/value pair within
  • a "regular" array (one with unspecified keys defaulting to a continuous sequence of numbers begining with 0 - an array ref in Perl), a KEY element is created for each value in the array, it's contents determined by the type of value itself.

The OPTIONS array MAY be passed, in order to specify additional options, such as which KEYs to treat as attributes (instead of creating children elements)

Return  
Notes

The best way to understand this method is to check the examples in the MiniXML docs/ directory and to play with it while checking the output using toString(). This method is named fromHash() in the XML::Mini Perl implementation.

 

Method fromString XMLSTRING
Description

Initialise the MiniXMLDoc (and its root MiniXMLElement object) using the XML string XMLSTRING.

Return Returns the number of immediate children the root MiniXMLElement now has.
Notes  

 

Method fromFile XMLFILE
Description

Initialise the MiniXMLDoc (and its root MiniXMLElement object) using the XML found in file XMLFILE.

Return Returns the number of immediate children the root MiniXMLElement now has.
Notes Reads in the file and performs a call to fromString()

 

Method toArray
Description Converts this MiniXMLDoc object to an associative array and returns it.
Return An array, probably containing other nested arrays, which represents the data from the MiniXMLDoc object. Both child elements and attributes are converted to array KEY => VALUE pairs
Notes  

 

Method toString
Description Converts this MiniXMLDoc object to a string and returns it.
Return String of XML representing this document
Notes  

 

Method getValue
Description Utility function, call the root MiniXMLElement's getValue() method
Return Return's a string representing the value (contents) of the entire document (all meta-data is removed)
Notes  

 

 

 

MiniXMLElement

Although the main handle to the XML document is the MiniXMLDoc object, much of the functionality and manipulation involves interaction with MiniXMLElement objects.

A MiniXMLElement has:

  • a name
  • a list of 0 or more attributes (which have a name and a value)
  • a list of 0 or more children (MiniXMLElement or MiniXMLNode objects)
  • a parent (optional, only if MINIXML_AUTOSETPARENT > 0)

MiniXMLElements are represented as
<tag attribute1="value1"> ...contents... </tag> pairs
or as
<tag attribute1="value1"/> unary tags (that have no content)
in XML.

Methods

MiniXMLElement
appendChild
attribute
avoidLoops
comment
createChild
getAllChildren
getElement
getElementByPath
getValue
insertChild
name
numChildren
numeric
parent
prependChild
removeAllChildren
removeChild
text
toString

Method MiniXMLElement ELEMENTNAME
Description MiniXMLElement constructor, creates an element with name ELEMENTNAME
Return A shiny new instance of MiniXMLElement
Notes The ELEMENTNAME need not be unique - it is the tag name in the XML <tag></tag>s

 

Method name [NEWNAME]
Description

If a NEWNAME string is passed, the MiniXMLElement's name is set to NEWNAME.

Return Returns the element's name
Notes  

 

Method

attribute NAME [SETTO [SETTOALT]]

Description

The attribute method is used to get and set the MiniXMLElement's attributes (ie the name/value pairs contained within the tag, <tagname attrib1="value1" attrib2="value2">)

If the optional SETTO is passed, the attribute's value is set to SETTO.

If the optional SETTOALT is passed and SETTO is false, the attribute's value is set to SETTOALT. This is usefull in cases when you wish to set the attribute to a default value if no SETTO is present, eg
$myelement->attribute('href', $theHref, 'http://psychogenic.com')
will default to 'http://psychogenic.com'.

Return Returns the value associated with attribute NAME if set, NULL otherwise.
Notes  

 

Method text [SETTO [SETTOALT]]
Description

The text() method is used to get or append text data to this element (it is appended to the child list as a new MiniXMLNode object).

If SETTO is passed, a new node is created, filled with SETTO and appended to the list of this element's children.

If the optional SETTOALT is passed and SETTO is false, the new node's value is set to SETTOALT. See the attribute() method for an example use.

Return Returns a string composed of all child MiniXMLNodes' contents.
Notes all the children MiniXMLNodes' contents - including numeric nodes are included in the return string.

 

Method numeric [SETTO [SETTOALT]]
Description

The numeric method is used to get or append numeric data to this element (it is appended to the child list as a MiniXMLNode object).

If SETTO is passed, a new node is created, filled with SETTO and appended to the list of this element's children.

If the optional SETTOALT is passed and SETTO is null, the new node's value is set to SETTOALT. See the attribute() method for an example use.

Return Returns a space seperated string composed all child MiniXMLNodes' numeric contents
Notes ONLY numeric contents are included from the list of child MiniXMLNodes.

 

Method comment CONTENTS
Description

The comment() method allows you to add a new MiniXMLElement (actually, an instance of the derived MiniXMLCommentELement class) to this element's list of children.

Comments will return a <!-- CONTENTS --> string when their toString() method is called.

Return Returns a reference to the newly appended element
Notes  

 

Method getValue
Description

Returns a string containing the value of all the element's child MiniXMLNodes (and all the MiniXMLNodes contained within it's child MiniXMLElements, recursively).

Return A string of the element's (and it's childrens') contents (no meta-data is included)
Notes

For example the P element in:

<P> 
 Hello my
  <I>
  very
   <B>
    nice
   </B>
  </I>
 Friend
</P>

Will return "Hello my very nice Friend" when getValue() is called.

 

Method getElement NAME
Description

Searches the element and it's children for an element with name NAME.

Return Returns a reference to the first MiniXMLElement with name NAME, if found, NULL otherwise
Notes

The search is performed like this, returning the first element that matches:

  • Check this element for a match
  • Check this element's immediate children (in order) for a match.
  • Ask each immediate child (in order) to MiniXMLElement::getElement() in turn (each child will then proceed similarly, checking all it's immediate children in order and then asking them to getElement() thus performing the search recursively all the way to the tree's leaf nodes.)

 

Method getElementByPath RELPATH
Description

Attempts to find and return a reference to the (first) element at RELPATH where RELPATH is the relative path in the structure from this 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 =& $partRateRequest->getElementByPath('vendor/accessid');

Will return what you expect (the accessid element with attributes user = "myusername" and password = "mypassword") but see the Note below.

Return Returns the MiniXMLElement at RELPATH if found, NULL otherwise
Notes

be careful:

In the example above,
$part =& $xmlDocument->getElementByPath('partRateRequest/partList/partNum');

will return the partNum element with the value "DA42". Other partNums are inaccessible by getElementByPath() as they all share the same path - Use MiniXMLElement::getAllChildren() on the partList parent instead.

 

Method numChildren
Description Returns the number of immediate (MiniXMLElement) children for this element
Return The number of immediate children for this element
Notes nodes - ie text and numeric data - are not counted in the value returned by numChildren

 

Method getAllChildren [NAME]
Description

Creates and returns a reference to an array of all this element's immediate MiniXMLElement children.

Return (reference to) an array of the element's MiniXMLElement children. The array will be empty of no children are present.
Notes Although the MiniXMLElement may contain MiniXMLNodes (ie appended text and numeric data) as children, these are not part of the returned list

 

Method createChild ELEMENTNAME [VALUE]
Description

Creates a new MiniXMLElement instance and appends it to the list of this element's children. The new child element's name is set to ELEMENTNAME.

If the optional VALUE (string or numeric) parameter is passed, the new element's text/numeric content will be set using VALUE.

Return Returns a reference to the new child element
Notes

Don't forget to use the =& (reference assignment) operator
when calling createChild:

$newChild =& $myElement->createChild('newChildName');

 

Method prependChildren CHILD
Description

prependChild is used to prepend an existing MiniXMLElement object to this element's list. The child is inserted in the first position of the element's list of children (it will be output first in the XML)

Return Returns a reference to the prepended child element.
Notes

Be careful not to create loops in the hierarchy, eg

$parent->prependChild($child);
$child->prependChild($subChild);
$subChild->prependChild($parent);

If you want to be sure to avoid loops, set the MINIXML_AVOIDLOOPS define to 1 or use the avoidLoops() method (will apply to all children added with createChild())

 

Method appendChild CHILDELEMENT
Description

appendChild is used to append an existing MiniXMLElement object to this element's list

Return Returns a reference to the appended child element
Notes

Be careful not to create loops in the hierarchy, eg

$parent->appendChild($child);
$child->appendChild($subChild);
$subChild->appendChild($parent);

If you want to be sure to avoid loops, set the MINIXML_AVOIDLOOPS define to 1 or use the avoidLoops() method (will apply to all children added with createChild())

 

Method insertChild CHILD INDEX
Description

insertChild is used to insert an existing MiniXMLElement object to this element's list. The child is inserted in at the position INDEX within element's list of children

Return Returns a reference to the inserted child element.
Notes

insertChild treats the list of child elements as an array - that means that the first position is at INDEX = 0 and the last position is numChildren() - 1.
In addition, if INDEX is greater than numChildren(), the element will be positioned at the end of the list (same effect as appendChild())

 

Method removeChild CHILD
Description

Removes child element CHILD from the list of this element's children. The method has no effect if CHILD is not found.

Return Returns a reference to the removed child element.
Notes

 

 

Method removeAllChildren
Description

Removes all children assigned to this element.

Return Returns an array of removed child elements.
Notes

 

 

Method parent [NEWPARENT]
Description

The parent() method is used to get/set the element's parent.

If the NEWPARENT parameter is passed, sets the parent to NEWPARENT (NEWPARENT must be an instance of MiniXMLElement)

Return Returns a reference to the parent MiniXMLElement if set, NULL otherwise.
Notes This method is mainly used internally and you wouldn't normally need to use it. It get's called on element appends when MINIXML_AUTOSETPARENT or MINIXML_AVOIDLOOPS or avoidLoops() > 1

 

Method avoidLoops [SETTO]
Description

The avoidLoops() method is used to get or set the avoidLoops flag for this element.

When avoidLoops is true, children with parents already set can NOT be appended to any other elements. This is overkill but it is a quick and easy way to avoid infinite loops in the heirarchy.

If the optional SETTO is passed, avoidLoops is set to SETTO for this element.

Return Returns the current value of the avoidLoops flag for the element
Notes The avoidLoops default behavior is configured with the MINIXML_AVOIDLOOPS define but can be set on individual elements (and automagically all the element's children) with the avoidLoops() method

 

Method toString [SPACEOFFSET]
Description

toString returns an XML string based on the element's attributes and content (recursively doing the same for all children).

The optional SPACEOFFSET parameter sets the number of spaces to use after newlines for elements at this level (adding 1 space per level in depth). SPACEOFFSET defaults to 0.

Return Returns an XML string
Notes  

 

MiniXMLNode

MiniXMLNodes are used as atomic containers for numerical and text data and act as leaves in the XML tree.

They have no name or children.

They always exist as children of MiniXMLElements. For example,
<B>this text is bold</B>
Would be represented as a MiniXMLElement named 'B' with a single child, a MiniXMLNode object which contains the string 'this text is bold'.

a MiniXMLNode has

  • a parent
  • data (text OR numeric)

 

Methods

MiniXMLNode objects are used internally as children of MiniXMLElements. As a programmer using MiniXML, you shouldn't need to access the nodes directly. If you are interested, the MiniXMLNode interface is fully documented in the source code.

 

 

 

Home    |   Overview   |   API   |   Download   |   About

SourceForge Logo