PBDOM_CHARACTERDATA:
GetTextTrim method
Description
The GetTextTrim method
returns the textual content of the current PBDOM_CHARACTERDATA
object with all surrounding whitespace characters removed.
Syntax
1 |
<span>pbdom_chardata_name</span>.GetTextTrim(<span></span>) |
Argument |
Description |
---|---|
pbdom_chardata_name |
The name of a PBDOM_CHARACTERDATA |
Return Values
String.
DOM Object |
Return Value |
||||
---|---|---|---|---|---|
PBDOM_TEXT |
The text data contained within the PBDOM_TEXT
If |
||||
PBDOM_CDATA |
The string data that is contained within
If
Note that the initial spaces |
||||
PBDOM_COMMENT |
Suppose there is the following comment:
Calling GetTextTrim on
Note |
Throws
EXCEPTION_PBDOM_OBJECT_INVALID_FOR_USE – If
this PBDOM_CHARACTERDATA is not a reference to an object
derived from PBDOM_CHARACTERDATA.
Examples
This example demonstrates:
-
Using an External DTD.
-
Using a parameter entity.
-
Using a single line statement to obtain the children
PBDOM_OBJECTs of an element. -
Obtaining the text of the three separate types of PBDOM_CHARACTERDATA
objects : PBDOM_TEXT, PBDOM_COMMENT, and PBDOM_CDATA. -
Obtaining the trimmed text of the same three separate
types of PBDOM_CHARACTERDATA objects. -
The difference between the two types of text retrieved
in 4 and 5.
The PowerScript code saves a string into an external file,
then creates a PBDOM_DOCUMENT pbdom_doc based
on the following DOM tree:
1 |
<!DOCTYPE abc SYSTEM "c:external_entity.dtd"><br><abc><br>   <data><br>       &text1;<br>       <!-- &text1;--><br>       <![CDATA[&text1;]]><br>   </data><br></abc> |
c:external_entity.dtd is
an external Document Type Definition file. Its contents are the
external subset of the Document Type Definition. The first line declares
a PARAMETER entity param_entity_ref that
contains the following replacement text:
1 |
&#32;&#32;&#32;PARAMETER ENTITY REFERENCE&#9;&#9;&#9; |
The next line declares a general entity text1 that
contains the following replacement text:
1 |
%param_entity_ref; |
When the entity text1 is used in an XML
document, it is expanded to the contents of the PARAMETER entity param_entity_ref.
The PowerScript code then obtains the root element, uses it
to obtain the data child element, and then
obtains an array of the child element’s own children. PBDOM
collects all the PBDOM_OBJECTs that are the children of
data and stores them in the PBDOM_OBJECT array pbdom_obj_array.
Next, the FOR loop iterates through all
the items in pbdom_obj_array and
stores each item in the PBDOM_CHARACTERDATA array pbdom_chardata.
This step is not required—the pbdom_obj_array can
be used to manipulate the data element’s children. It is
done to demonstrate that you can cast each item into a PBDOM_CHARACTERDATA
object by assigning it into a PBDOM_CHARACTERDATA array.
This is possible if and only if each PBDOM_OBJECT
is also derived from PBDOM_CHARACTERDATA. If a PBDOM_OBJECT
is not derived from PBDOM_CHARACTERDATA, the PowerBuilder
VM throws an exception.
The next FOR loop iterates through all
the items of the pbdom_chardata array and
calls the GetText and GetTextTrim methods
on each. Each of the returned strings from GetText and GetTextTrim is
delimited by “[” and “]” characters
so that the complete text content displays clearly in the message
boxes.
The first child of data is the PBDOM_TEXT &text1;
,
which expands to the string in param_entity_ref.
The entity references within this string are also expanded and the
Tab and Space characters display when GetText is
called. When GetTextTrim is called, PBDOM removes
the beginning and trailing whitespace characters and the resulting
string is simply PARAMETER ENTITY REFERENCE
.The
second child of data is the PBDOM_COMMENT <!-- &text1;-->
., and
the third child is the PBDOM_CDATA <![CDATA[&text1;]]>
.
The string &text1;
is
not considered to be an entity reference by PBDOM because W3C DOM
comments and CDATA sections cannot hold any entity references. Both GetText and GetTextTrim return
the string &text1;
.
There are no leading or trailing spaces to remove.
1 |
PBDOM_CHARACTERDATA      pbdom_chardata[]<br>PBDOM_OBJECT             pbdom_obj_array[]<br>integer                  iFileNum1<br>long                     l = 0<br>string strExternalDTD = "<!ENTITY % param_entity_ref ~"&#32;&#32;&#32;PARAMETER ENTITY REFERENCE&#9;&#9;&#9;~"><!ENTITY text1 ~"%param_entity_ref;~">"<br>string strXML = "<!DOCTYPE abc SYSTEM ~"c:external_entity.dtd~"><abc><data>&text1;<!-- &text1;--><![CDATA[&text1;]]></data></abc>"<br> <br>TRY<br> iFileNum1 = FileOpen("c:external_entity.dtd", &<br>   StreamMode!, Write!, LockWrite!, Replace!)<br> FileWrite(iFileNum1, strExternalDTD)<br> FileClose(iFileNum1)<br> <br> pbdombuilder_new = Create PBDOM_Builder<br> pbdom_doc = pbdombuilder_new.BuildFromString (strXML)<br> <br> pbdom_doc.GetRootElement(). &<br>    GetChildElement("data"). &<br>    GetContent(pbdom_obj_array)<br> <br> for l = 1 to UpperBound(pbdom_obj_array)<br>    pbdom_chardata[l] = pbdom_obj_array[l]<br> next <br> <br> for l = 1 to UpperBound(pbdom_chardata)<br>    MessageBox (pbdom_chardata[l]. &<br>       GetObjectClassString() + " GetText()", &<br>       "[" + pbdom_chardata[l].GetText() + "]")<br>    MessageBox (pbdom_chardata[l]. &<br>       GetObjectClassString() + " GetTextTrim()" , &<br>       "[" + pbdom_chardata[l].GetTextTrim() + "]")<br> next <br> <br> Destroy pbdombuilder_new<br> <br>CATCH (PBDOM_Exception except)<br> MessageBox ("Exception Occurred", except.Text)<br>END TRY |
Usage
If no textual value exists for the current PBDOM_CHARACTERDATA,
or if only whitespace characters exist, an empty string is returned.