3 XML::LibXML::Text - XML::LibXML Class for Text Nodes
10 # Only methods specific to Text nodes are listed here,
11 # see XML::LibXML::Node manpage for other methods
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 );
27 Unlike the DOM specification, XML::LibXML implements the text node as the base
28 class of all character data node. Therefor there exists no CharacterData class.
29 This allows one to apply methods of text nodes also to Comments and
35 The class inherits from L<<<<<< XML::LibXML::Node >>>>>>. The documentation for Inherited methods is not listed here.
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.
43 $text = XML::LibXML::Text->new( $content );
45 The constructor of the class. It creates an unbound text node.
50 $nodedata = $text->data;
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
66 will have the same result and are not different entities.
69 =item setData($string)
71 $text->setData( $text_content );
73 This function sets or replaces text content to a node. The node has to be of
74 the type "text", "cdata" or "comment".
77 =item substringData($offset,$length)
79 $text->substringData($offset, $length);
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.
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.
88 =item appendData($string)
90 $text->appendData( $somedata );
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 >>>>>>.
96 =item insertData($offset,$string)
98 $text->insertData($offset, $string);
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
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 >>>>>>.
107 =item deleteData($offset, $length)
109 $text->deleteData($offset, $length);
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
116 =item deleteDataString($string, [$all])
118 $text->deleteDataString($remstring, $all);
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 :)
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.
126 =item replaceData($offset, $length, $string)
128 $text->replaceData($offset, $length, $string);
130 The DOM style version to replace node data.
133 =item replaceDataString($oldstring, $newstring, [$all])
135 $text->replaceDataString($old, $new, $flag);
137 The more programmer friendly version of replaceData() :)
139 Instead of giving offsets and length one can specify the exact string (I<<<<<< $oldstring >>>>>>) to be replaced. Additionally the I<<<<<< $all >>>>>> flag allows to replace all occurrences of I<<<<<< $oldstring >>>>>>.
142 =item replaceDataRegEx( $search_cond, $replace_cond, $reflags )
144 $text->replaceDataRegEx( $search_cond, $replace_cond, $reflags );
146 This method replaces the node's data by a C<<<<<< simple >>>>>> regular expression. Optional, this function allows to pass some flags that will
147 be added as flag to the replace statement.
149 I<<<<<< NOTE: >>>>>> This is a shortcut for
153 my $datastr = $node->getData();
154 $datastr =~ s/somecond/replacement/g; # 'g' is just an example for any flag
155 $node->setData( $datastr );
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.
177 2001-2007, AxKit.com Ltd.
179 2002-2006, Christian Glahn.
181 2006-2009, Petr Pajas.