CleanCode Perl Libraries |
Home | Perl | Java | PowerShell | C# | SQL | Index | Tools | Download | What's New |
Multi-Lingual Library | Maintainability | ||||||||||||
Perl | Java | JavaScript | Certified Class |
Testable Class |
Standalone Mode |
Diagnostic Enabled |
XML::DocumentPlus - Adds several convenience methods to a standard XML::DOM::Document object.
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");
XML::DOM::Document
Perl5.005, XML::DOM, Data::Handy
Wraps an XML::DOM::Document object inside a new object which adds several convenience methods.
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.
docToWrap
- optional; XML::DOM::Document object
a newly created object
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.
elemText
- textual representation of an XML element
new Node representation of elemText
in current document's context.
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.
foreignNode
- DOM Node, Element, or Document object
Element duplicating foreignNode
created in current document's context.
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.
tagName
- string; XML tag name
keyField
- string; XML attribute name within the targetNodeName
keyValue
- string; value to match in the keyField
Selected element or undef if no matching element
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.
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
Array of node values, one for each tag name in nodeListRef
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".
tagName
- string; XML tag name
Returns text of node or empty string if no matching nodes.
appendTextChild(parent, nodeName, nodeValue)
Adds an element named nodeName
containing only the text nodeValue
to the provided parent
node.
parent
- node object
nodeName
- string; name of child node to add to <parent>
nodeValue
- string; text content of node nodeName
Input node <parent> (just as a convenience)
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"
None
Michael Sorens
$Revision: 8 $ $Date: 2006-12-19 21:13:43 -0800 (Tue, 19 Dec 2006) $
CleanCode 0.9
Hey! The above document had some coding errors, which are explained below:
=back doesn't take any parameters, but you said =back -- end of CONSTRUCTOR section
=back doesn't take any parameters, but you said =back -- end of FUNCTION section
Home | Perl | Java | PowerShell | C# | SQL | Index | Tools | Download | What's New |
CleanCode Perl Libraries | Copyright © 2001-2013 Michael Sorens - Revised 2013.06.30 |