.\" Automatically generated by Pod::Man 2.1801 (Pod::Simple 3.05) .\" .\" 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" '' '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 turned on, 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. .ie \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . nr % 0 . rr F .\} .el \{\ . de IX .. .\} .\" .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2). .\" Fear. Run. Save yourself. No user-serviceable parts. . \" fudge factors for nroff and troff .if n \{\ . ds #H 0 . ds #V .8m . ds #F .3m . ds #[ \f1 . ds #] \fP .\} .if t \{\ . ds #H ((1u-(\\\\n(.fu%2u))*.13m) . ds #V .6m . ds #F 0 . ds #[ \& . ds #] \& .\} . \" simple accents for nroff and troff .if n \{\ . ds ' \& . ds ` \& . ds ^ \& . ds , \& . ds ~ ~ . ds / .\} .if t \{\ . ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u" . ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u' . ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u' . ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u' . ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u' . ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u' .\} . \" troff and (daisy-wheel) nroff accents .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V' .ds 8 \h'\*(#H'\(*b\h'-\*(#H' .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#] .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H' .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u' .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#] .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#] .ds ae a\h'-(\w'a'u*4/10)'e .ds Ae A\h'-(\w'A'u*4/10)'E . \" corrections for vroff .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u' .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u' . \" for low resolution devices (crt and lpr) .if \n(.H>23 .if \n(.V>19 \ \{\ . ds : e . ds 8 ss . ds o a . ds d- d\h'-1'\(ga . ds D- D\h'-1'\(hy . ds th \o'bp' . ds Th \o'LP' . ds ae ae . ds Ae AE .\} .rm #[ #] #H #V #F C .\" ======================================================================== .\" .IX Title "EasyOBJ 3pm" .TH EasyOBJ 3pm "2009-08-19" "perl v5.10.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::EasyOBJ \- Easy XML object navigation .SH "VERSION" .IX Header "VERSION" Version 1.12 .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 3 \& # open exisiting file \& my $doc = new XML::EasyOBJ(\*(Aqmy_xml_document.xml\*(Aq); \& my $doc = new XML::EasyOBJ(\-type => \*(Aqfile\*(Aq, \-param => \*(Aqmy_xml_document.xml\*(Aq); \& \& # create object from XML string \& my $doc = new XML::EasyOBJ(\-type => \*(Aqstring\*(Aq, \-param => $xml_source); \& \& # create new file \& my $doc = new XML::EasyOBJ(\-type => \*(Aqnew\*(Aq, \-param => \*(Aqroot_tag\*(Aq); \& \& # read from document \& my $text = $doc\->some_element($index)\->getString; \& my $attr = $doc\->some_element($index)\->getAttr(\*(Aqfoo\*(Aq); \& my $element = $doc\->some_element($index); \& my @elements = $doc\->some_element; \& \& # first "some_element" element \& my $elements = $doc\->some_element; \& # list of "some_element" elements \& my @elements = $doc\->some_element; \& \& # write to document \& $doc\->an_element\->setString(\*(Aqsome string\*(Aq) \& $doc\->an_element\->addString(\*(Aqsome string\*(Aq) \& $doc\->an_element\->setAttr(\*(Aqattrname\*(Aq, \*(Aqval\*(Aq) \& $doc\->an_element\->setAttr(\*(Aqattr1\*(Aq => \*(Aqval\*(Aq, \*(Aqattr2\*(Aq => \*(Aqval2\*(Aq) \& \& # access elements with non\-name chars and the underlying DOM \& my $element = $doc\->getElement(\*(Aqfoo\-bar\*(Aq)\->getElement(\*(Aqbar\-none\*(Aq); \& my $dom = $doc\->foobar\->getDomObj; \& \& # get elements without specifying the element name \& my @elements = $doc\->getElement(); \& my $sixth_element = $doc\->getElement(\*(Aq\*(Aq, 5); \& \& # remove elements/attrs \& $doc\->remElement(\*(Aqtagname\*(Aq, $index); \& $doc\->tag_name\->remAttr($attr); \& \& # remap builtin methods \& $doc\->remapMethod(\*(AqgetString\*(Aq, \*(Aqs\*(Aq); \& my $text = $doc\->some_element\->s; .Ve .SH "DESCRIPTION" .IX Header "DESCRIPTION" I wrote XML::EasyOBJ a couple of years ago because it seemed to me that the \s-1DOM\s0 wasn't very \*(L"perlish\*(R" and the \s-1DOM\s0 is difficult for us mere mortals that don't use it on a regular basis. As I only need to process \s-1XML\s0 on an occasionally I wanted an easy way to do what I needed to do without having to refer back to \s-1DOM\s0 documentation each time. .PP A quick fact list about XML::EasyOBJ: .PP .Vb 3 \& * Runs on top of XML::DOM \& * Allows access to the DOM as needed \& * Simple routines to reading and writing elements/attributes .Ve .SH "REQUIREMENTS" .IX Header "REQUIREMENTS" XML::EasyOBJ uses \s-1XML::DOM\s0. \s-1XML::DOM\s0 is available from \s-1CPAN\s0 (www.cpan.org). .SH "METHODS" .IX Header "METHODS" Below is a description of the methods avialable. .SS "new" .IX Subsection "new" You can create a new object from an \s-1XML\s0 file, a string of \s-1XML\s0, or a new document. The constructor takes a set of key value pairs as follows: .IP "\-type" 4 .IX Item "-type" The type is either \*(L"file\*(R", \*(L"string\*(R" or \*(L"new\*(R". \*(L"file\*(R" will create the object from a file source, \*(L"string\*(R" will create the object from a string of \s-1XML\s0 code, and \*(L"new\*(R" will create a new document object. .IP "\-param" 4 .IX Item "-param" This value depends on the \-type that is passed to the constructor. If the \-type is \*(L"file\*(R" this will be the filename to open and parse. If \-type is \*(L"string\*(R", this is a string of \s-1XML\s0 code. If \-type is \&\*(L"new\*(R", this is the name of the root element. .Sp Creating an object from an \s-1XML\s0 file: .Sp .Vb 1 \& my $doc = new XML::EasyOBJ(\-type => \*(Aqfile\*(Aq, \-param => \*(Aqmy_xml_document.xml\*(Aq); .Ve .Sp Creating an object from a string containing the \s-1XML\s0 source: .Sp .Vb 1 \& my $doc = new XML::EasyOBJ(\-type => \*(Aqstring\*(Aq, \-param => $xml_source); .Ve .Sp Creating a new \s-1XML\s0 document by passing the root tag name: .Sp .Vb 1 \& my $doc = new XML::EasyOBJ(\-type => \*(Aqnew\*(Aq, \-param => \*(Aqroot_tag\*(Aq); .Ve .IP "\-expref" 4 .IX Item "-expref" Passing a value of 1 will force the expansion of references when grabbing string data from the \s-1XML\s0 file. The default value is 0, not to expand references. .PP Obtionally you may also pass the filename to open as the first argument instead of passing the \-type and \-param parameters. This is backwards compatable with early version of XML::EasyOBJ which did not handle \-type and \-param parameters. .PP .Vb 1 \& my $doc = new XML::EasyOBJ(\*(Aqmy_xml_document.xml\*(Aq); .Ve .SS "makeNewNode( \s-1NEW_TAG\s0 )" .IX Subsection "makeNewNode( NEW_TAG )" Append a new element node to the current node. Takes the tag name as the parameter and returns the created node as a convienence. .PP .Vb 1 \& my $p_element = $doc\->body\->makeNewNode(\*(Aqp\*(Aq); .Ve .SS "remapMethod( \s-1CUR_METHOD\s0, \s-1NEW_METHOD\s0 )" .IX Subsection "remapMethod( CUR_METHOD, NEW_METHOD )" Allows you to change the name of any of the object methods. You might want to do this for convienience or to avoid a naming collision with an element in the document. .PP Two parameters need to be passed; the current name of the method and the new name. Returns 1 on a successful mapping and undef on failure. A failure can result if you don't pass two parameters if if the \*(L"copy from\*(R" method name does not exist. .PP .Vb 2 \& $doc\->remapMethod(\*(AqgetString\*(Aq, \*(Aqs\*(Aq); \& $doc\->s(); .Ve .PP After remapping you must use the new name if you with to remap the method again. You can call the remapMethod method from any place in the \s-1XML\s0 tree and it will always change the method globally. .PP In the following example \f(CW$val1\fR and \f(CW$val2\fR are equal: .PP .Vb 4 \& $doc\->some_element\->another_element\->(\*(AqgetString\*(Aq, \*(Aqs\*(Aq); \& my $val1 = $doc\->s(); \& $doc\->remapMethod(\*(Aqs\*(Aq, \*(AqgetString\*(Aq); \& my $val2 = $doc\->getString(); .Ve .SS "getString( )" .IX Subsection "getString( )" Recursively extracts text from the current node and all children element nodes. Returns the extracted text as a single scalar value. Expands entities based on if the \-expref flag was supplied during object creation. .SS "extractText( )" .IX Subsection "extractText( )" Same as \fIgetString()\fR but does not check the \-expref flag. Included for compatability with inital version of interface. .SS "setString( \s-1STRING\s0 )" .IX Subsection "setString( STRING )" Sets the text value of the specified element. This is done by first removing all text node children of the current element and then appending the supplied text as a new child element. .PP Take this \s-1XML\s0 fragment and code for example: .PP
This elment has text and child elements
.PP .Vb 1 \& $doc\->p\->setString(\*(AqThis is the new text\*(Aq); .Ve .PP This will change the fragment to this: .PPtextchildThis is the new text
.PP Because the and tags are not text nodes they are left unchanged, and the new text is added at the end of the specified element. .PP If you need more specific control on the change you should either use the \fIgetDomObj()\fR method and use the \s-1DOM\s0 methods directly or remove all of the child nodes and rebuild theelement from scratch. Also see the \fIaddString()\fR method. .SS "addString( \s-1STRING\s0 )" .IX Subsection "addString( STRING )" Adds to the the text value of the specified element. This is done by appending the supplied text as a new child element. .PP Take this \s-1XML\s0 fragment and code for example: .PP
This elment has text
.PP .Vb 1 \& $doc\->p\->addString(\*(Aq and elements\*(Aq); .Ve .PP This will change the fragment to this: .PPThis elment has text and elements
.SS "getAttr( \s-1ATTR_NAME\s0 )" .IX Subsection "getAttr( ATTR_NAME )" Returns the value of the named attribute. .PP .Vb 1 \& my $val = $doc\->body\->img\->getAttr(\*(Aqsrc\*(Aq); .Ve .SS "getTagName( )" .IX Subsection "getTagName( )" Returns the tag name of the specified element. This method is useful when you are enumerating child elements and do not know their element names. .PP .Vb 3 \& foreach my $element ( $doc\->getElement() ) { \& print $element\->getTagName(); \& } .Ve .SS "setAttr( \s-1ATTR_NAME\s0, \s-1ATTR_VALUE\s0, [\s-1ATTR_NAME\s0, \s-1ATTR_VALUE\s0]... )" .IX Subsection "setAttr( ATTR_NAME, ATTR_VALUE, [ATTR_NAME, ATTR_VALUE]... )" For each name/value pair passed the attribute name and value will be set for the specified element. .SS "remAttr( \s-1ATTR_NAME\s0 )" .IX Subsection "remAttr( ATTR_NAME )" Removes the specified attribute from the current element. .SS "remElement( \s-1TAG_NAME\s0, \s-1INDEX\s0 )" .IX Subsection "remElement( TAG_NAME, INDEX )" Removes a child element of the current element. The name of the child element and the index must be supplied. An index of 0 will remove the first occurance of the named element, 1 the second, 2 the third, etc. .SS "getElement( \s-1TAG_NAME\s0, \s-1INDEX\s0 )" .IX Subsection "getElement( TAG_NAME, INDEX )" Returns the node from the tag name and index. If no index is given the first child with that name is returned. Use this method when you have element names that include characters that are not legal as a perl method name. For example: .PP .Vb 5 \&