CleanCode Perl Libraries
Multi-Lingual Library Maintainability
available: Perl available: Java not available: JavaScript not available: Certified
Class
not available: Testable
Class
not available: Standalone
Mode
not available: Diagnostic
Enabled

NAME

XML::DocumentPlus - Adds several convenience methods to a standard XML::DOM::Document object.

SYNOPSIS

    use XML::DocumentPlus;
    $doc = XML::DocumentPlus->new();
    $node = $doc->copyNode("<x>...</x>");
    $node = $doc->copyNode($foreignNode);
    $node = $doc->getSelectedElementByTagName("projectSpec", "myKey", "p2");
    ($ownerVal, $countVal) = $doc->getSelectedChildValues(
        "projectSpec","myKey","p2",["owner","count"] );
    $username = $doc->getSelectedNodeText("username");
    $doc->appendTextChild($node, "title", "of mice and men");

EXTENDS

XML::DOM::Document

REQUIRES

Perl5.005, XML::DOM, Data::Handy

DESCRIPTION

Wraps an XML::DOM::Document object inside a new object which adds several convenience methods.

CONSTRUCTOR

new

PACKAGE->new()

PACKAGE->new(docToWrap)

Creates a DocumentPlus object. With no argument, creates a new object which is simply an XML::DOM::Document object enhanced with the methods in this module. With an XML::DOM::Document argument, that object is simply coerced into a DocumentPlus object, adding the methods contained herein.

Parameters:

docToWrap - optional; XML::DOM::Document object

Returns:

a newly created object

METHODS

createNode

createNode(elemText)

Create a node tree from a string. This is a simple way to convert an XML fragment as a string into a DOM subtree.

Parameters:

elemText - textual representation of an XML element

Returns:

new Node representation of elemText in current document's context.

copyNode

copyNode(foreignNode)

Clone a foreign node (and descendants) into a compatible node. Only designed to handle elements, attributes, and text nodes. This is needed in Perl since there is no importNode method in the standard library.

Parameters:

foreignNode - DOM Node, Element, or Document object

Returns:

Element duplicating foreignNode created in current document's context.

getSelectedElementByTagName

getSelectedElementByTagName(tagName, keyField, keyValue)

Collects the output of the standard getElementsByTagName method, then filters it to return the single node with the specified keyValue. The unique key may be any attribute of an element; the keyField identifies which attribute to examine.

Example: With this XML...

        <root>
                <projectSpec myKey="p1">
                        <count>25</count>
                        <owner>fred</owner>
                </projectSpec>
                <projectSpec mode="25" myKey="p3">
                        <count/>
                        <owner>sarah</owner>
                </projectSpec>
                <projectSpec myKey="p2" other="green">
                        <count>25</count>
                        <owner>wizard</owner>
                </projectSpec>
        </root>

and this code...

  $elem = $xmlDoc->getSelectedElementByTagName("projectSpec", "myKey", "p2");

...then $elem contains the <projectSpec> subtree where myKey equals "p2". This provides a mechanism for getting one data subtree among a number of similar nodes. See also getSelectedChildValues for a variation on this technique, which returns actual data values from the subtree, rather than the subtree as a whole.

Parameters:

tagName - string; XML tag name

keyField - string; XML attribute name within the targetNodeName

keyValue - string; value to match in the keyField

Returns:

Selected element or undef if no matching element

getSelectedChildValues

getSelectedChildValues(targetNodeName, attrName, attrTargetVal, nodeListRef)

Get a list of values of text nodes (nodeListRef) that are children of a targetNodeName, where the value of the specified attribute (attrName) matches the attrTargetVal.

Referring back to the example XML in getSelectedElementByTagName, this code...

  ($ownerVal, $countVal) =
    $xml->getSelectedChildValues("projectSpec","myKey","p2",["owner","count"]);

...returns the contents of the owner and count child nodes (in that order) of the projectSpec node whose attribute myKey equals "p2".

See also getSelectedElementByTagName for a variation on this technique, which returns the subtree as a whole, rather than actual data values from within the subtree.

Parameters:

targetNodeName - string; XML tag name

attrName - string; XML attribute name within the targetNodeName

attrTargetVal - string; value to match in the attrName

nodeListRef - reference to array of tag names to extract from within targetNodeName

Returns:

Array of node values, one for each tag name in nodeListRef

getSelectedNodeText

getSelectedNodeText(tagName)

Returns text of first node matching tagName. This routine assumes the node contains only text and is unique. Typically useful in reading a data or configuration file.

Example: With this XML...

        <root>
                <param1>25</param1>
                <param2>fred</param2>
                <param3>true</param3>
        </root>

and this code...

        print $xmlDoc->getSelectedNodeText("param3");

...then the output will be the string "true".

Parameters:

tagName - string; XML tag name

Returns:

Returns text of node or empty string if no matching nodes.

appendTextChild

appendTextChild(parent, nodeName, nodeValue)

Adds an element named nodeName containing only the text nodeValue to the provided parent node.

Parameters:

parent - node object

nodeName - string; name of child node to add to <parent>

nodeValue - string; text content of node nodeName

Returns:

Input node <parent> (just as a convenience)

STANDALONE TESTING

A standalone test class to exercise the DocumentPlus class. Runs through a series of tests, indicating what is expected (roughly). You can work with the ParamMap class in isolation to get a feel for what it does, using the inner class XML::DocumentPlus::Test. Invoke the class as a main program, calling the function main from the command-line as in:

        perl -mXML::DocumentPlus -e "XML::DocumentPlus::Test::main"

BUGS

None

AUTHOR

Michael Sorens

VERSION

$Revision: 8 $ $Date: 2006-12-19 21:13:43 -0800 (Tue, 19 Dec 2006) $

SINCE

CleanCode 0.9

POD ERRORS

Hey! The above document had some coding errors, which are explained below:

Around line 126:

=back doesn't take any parameters, but you said =back -- end of CONSTRUCTOR section

Around line 446:

=back doesn't take any parameters, but you said =back -- end of FUNCTION section


CleanCode Perl Libraries Copyright © 2001-2013 Michael Sorens - Revised 2013.06.30 Get CleanCode at SourceForge.net. Fast, secure and Free Open Source software downloads