| 1 |
=head1 NAME |
| 2 |
|
| 3 |
XML::LibXML::Text - XML::LibXML Class for Text Nodes |
| 4 |
|
| 5 |
=head1 SYNOPSIS |
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
use XML::LibXML; |
| 10 |
# Only methods specific to Text nodes are listed here, |
| 11 |
# see the XML::LibXML::Node manpage for other methods |
| 12 |
|
| 13 |
$text = XML::LibXML::Text->new( $content ); |
| 14 |
$nodedata = $text->data; |
| 15 |
$text->setData( $text_content ); |
| 16 |
$text->substringData($offset, $length); |
| 17 |
$text->appendData( $somedata ); |
| 18 |
$text->insertData($offset, $string); |
| 19 |
$text->deleteData($offset, $length); |
| 20 |
$text->deleteDataString($remstring, $all); |
| 21 |
$text->replaceData($offset, $length, $string); |
| 22 |
$text->replaceDataString($old, $new, $flag); |
| 23 |
$text->replaceDataRegEx( $search_cond, $replace_cond, $reflags ); |
| 24 |
|
| 25 |
=head1 DESCRIPTION |
| 26 |
|
| 27 |
Unlike the DOM specification, XML::LibXML implements the text node as the base |
| 28 |
class of all character data node. Therefore there exists no CharacterData |
| 29 |
class. This allows one to apply methods of text nodes also to Comments and |
| 30 |
CDATA-sections. |
| 31 |
|
| 32 |
|
| 33 |
=head1 METHODS |
| 34 |
|
| 35 |
The class inherits from L<<<<<< XML::LibXML::Node >>>>>>. The documentation for Inherited methods is not listed here. |
| 36 |
|
| 37 |
Many functions listed here are extensively documented in the DOM Level 3 specification (L<<<<<< http://www.w3.org/TR/DOM-Level-3-Core/ >>>>>>). Please refer to the specification for extensive documentation. |
| 38 |
|
| 39 |
=over 4 |
| 40 |
|
| 41 |
=item new |
| 42 |
|
| 43 |
$text = XML::LibXML::Text->new( $content ); |
| 44 |
|
| 45 |
The constructor of the class. It creates an unbound text node. |
| 46 |
|
| 47 |
|
| 48 |
=item data |
| 49 |
|
| 50 |
$nodedata = $text->data; |
| 51 |
|
| 52 |
Although there exists the C<<<<<< nodeValue >>>>>> attribute in the Node class, the DOM specification defines data as a separate |
| 53 |
attribute. C<<<<<< XML::LibXML >>>>>> implements these two attributes not as different attributes, but as aliases, |
| 54 |
such as C<<<<<< libxml2 >>>>>> does. Therefore |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
$text->data; |
| 59 |
|
| 60 |
and |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
$text->nodeValue; |
| 65 |
|
| 66 |
will have the same result and are not different entities. |
| 67 |
|
| 68 |
|
| 69 |
=item setData($string) |
| 70 |
|
| 71 |
$text->setData( $text_content ); |
| 72 |
|
| 73 |
This function sets or replaces text content to a node. The node has to be of |
| 74 |
the type "text", "cdata" or "comment". |
| 75 |
|
| 76 |
|
| 77 |
=item substringData($offset,$length) |
| 78 |
|
| 79 |
$text->substringData($offset, $length); |
| 80 |
|
| 81 |
Extracts a range of data from the node. (DOM Spec) This function takes the two |
| 82 |
parameters $offset and $length and returns the sub-string, if available. |
| 83 |
|
| 84 |
If the node contains no data or $offset refers to an non-existing string index, |
| 85 |
this function will return I<<<<<< undef >>>>>>. If $length is out of range C<<<<<< substringData >>>>>> will return the data starting at $offset instead of causing an error. |
| 86 |
|
| 87 |
|
| 88 |
=item appendData($string) |
| 89 |
|
| 90 |
$text->appendData( $somedata ); |
| 91 |
|
| 92 |
Appends a string to the end of the existing data. If the current text node |
| 93 |
contains no data, this function has the same effect as C<<<<<< setData >>>>>>. |
| 94 |
|
| 95 |
|
| 96 |
=item insertData($offset,$string) |
| 97 |
|
| 98 |
$text->insertData($offset, $string); |
| 99 |
|
| 100 |
Inserts the parameter $string at the given $offset of the existing data of the |
| 101 |
node. This operation will not remove existing data, but change the order of the |
| 102 |
existing data. |
| 103 |
|
| 104 |
The $offset has to be a positive value. If $offset is out of range, C<<<<<< insertData >>>>>> will have the same behaviour as C<<<<<< appendData >>>>>>. |
| 105 |
|
| 106 |
|
| 107 |
=item deleteData($offset, $length) |
| 108 |
|
| 109 |
$text->deleteData($offset, $length); |
| 110 |
|
| 111 |
This method removes a chunk from the existing node data at the given offset. |
| 112 |
The $length parameter tells, how many characters should be removed from the |
| 113 |
string. |
| 114 |
|
| 115 |
|
| 116 |
=item deleteDataString($string, [$all]) |
| 117 |
|
| 118 |
$text->deleteDataString($remstring, $all); |
| 119 |
|
| 120 |
This method removes a chunk from the existing node data. Since the DOM spec is |
| 121 |
quite unhandy if you already know C<<<<<< which >>>>>> string to remove from a text node, this method allows more perlish code :) |
| 122 |
|
| 123 |
The functions takes two parameters: I<<<<<< $string >>>>>> and optional the I<<<<<< $all >>>>>> flag. If $all is not set, I<<<<<< undef >>>>>> or I<<<<<< 0 >>>>>>, C<<<<<< deleteDataString >>>>>> will remove only the first occurrence of $string. If $all is I<<<<<< TRUE >>>>>>C<<<<<< deleteDataString >>>>>> will remove all occurrences of I<<<<<< $string >>>>>> from the node data. |
| 124 |
|
| 125 |
|
| 126 |
=item replaceData($offset, $length, $string) |
| 127 |
|
| 128 |
$text->replaceData($offset, $length, $string); |
| 129 |
|
| 130 |
The DOM style version to replace node data. |
| 131 |
|
| 132 |
|
| 133 |
=item replaceDataString($oldstring, $newstring, [$all]) |
| 134 |
|
| 135 |
$text->replaceDataString($old, $new, $flag); |
| 136 |
|
| 137 |
The more programmer friendly version of replaceData() :) |
| 138 |
|
| 139 |
Instead of giving offsets and length one can specify the exact string (I<<<<<< $oldstring >>>>>>) to be replaced. Additionally the I<<<<<< $all >>>>>> flag allows one to replace all occurrences of I<<<<<< $oldstring >>>>>>. |
| 140 |
|
| 141 |
|
| 142 |
=item replaceDataRegEx( $search_cond, $replace_cond, $reflags ) |
| 143 |
|
| 144 |
$text->replaceDataRegEx( $search_cond, $replace_cond, $reflags ); |
| 145 |
|
| 146 |
This method replaces the node's data by a C<<<<<< simple >>>>>> regular expression. Optional, this function allows one to pass some flags that |
| 147 |
will be added as flag to the replace statement. |
| 148 |
|
| 149 |
I<<<<<< NOTE: >>>>>> This is a shortcut for |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
my $datastr = $node->getData(); |
| 154 |
$datastr =~ s/somecond/replacement/g; # 'g' is just an example for any flag |
| 155 |
$node->setData( $datastr ); |
| 156 |
|
| 157 |
This function can make things easier to read for simple replacements. For more |
| 158 |
complex variants it is recommended to use the code snippet above. |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
=back |
| 163 |
|
| 164 |
=head1 AUTHORS |
| 165 |
|
| 166 |
Matt Sergeant, |
| 167 |
Christian Glahn, |
| 168 |
Petr Pajas |
| 169 |
|
| 170 |
|
| 171 |
=head1 VERSION |
| 172 |
|
| 173 |
2.0134 |
| 174 |
|
| 175 |
=head1 COPYRIGHT |
| 176 |
|
| 177 |
2001-2007, AxKit.com Ltd. |
| 178 |
|
| 179 |
2002-2006, Christian Glahn. |
| 180 |
|
| 181 |
2006-2009, Petr Pajas. |
| 182 |
|
| 183 |
=cut |
| 184 |
|
| 185 |
|
| 186 |
=head1 LICENSE |
| 187 |
|
| 188 |
This program is free software; you can redistribute it and/or modify it under |
| 189 |
the same terms as Perl itself. |
| 190 |
|