[Gambas-user] gb.xml feature request

Adrien Prokopowicz adrien.prokopowicz at ...626...
Tue Apr 7 00:01:02 CEST 2015


Le Sun, 05 Apr 2015 04:58:05 +0200, adamnt42 at ...626...  
<adamnt42 at ...626...> a écrit:

> @Adrien Prokopowicz ?
>
> I ask for an enhancement to XMLNode (and descendants). Currently the  
> TextContent property returns the concatenated text of the currenode and  
> all child nodes thereof.  What I want to get is the text of the current  
> node only, ignoring any child nodes.
>
>   Dim sText as String
>   Dim hElt as XMLElement ' = some node
>   sText = hElt.GetLocalTextContent()
>
> i.e. a new method to return just the local node text.
>
> I think this requires modifying the serializer class  
> GBGetXMLTextContent() method to avoid descending in to the child nodes  
> but it is far beyond my C++ abilities and the attempts I made are too  
> embarrassing to reveal. :-(
>
> tia
> bruce
>

XML elements do not actually have text. All they have are child nodes,
that can be XML text nodes (represented by the XmlTextNode class).

Let's say you have the following XML structure :

<p>Hello <em>world</em> ! :-)</p>
|    ^   |     ^      |  ^      |
|    |   |   XmlText  |  |      |
| XmlText|=XmlElement=| XmlText |
|                               |
|========= XmlElement ==========|

(XmlText should be XmlTextNode, but it doesn't fit on the diagram)

By definition (according to the W3C), the TextContent property of
an XML element contains the text content of all the child nodes
(and therefore all descendants if some child nodes are elements).

If I understood correctly, you would like to only retreive the text
"Hello  ! :-)" in the case of the above structure.
What you should do is iterating over the element's nodes, and
retreiving only the text content of the XmlTextNodes, which is easily
feasible in pure Gambas, like so :

   Dim hElement As XmlElement '= some element
   Dim hNode As XmlNode
   Dim sText As String

   For Each hNode In hElement.ChildNodes
     If hNode Is XmlTextNode Then sText &= hNode.TextContent
   Next

   Print sText 'Prints : "Hello  ! :-)"

Regards,
-- 
Adrien Prokopowicz




More information about the User mailing list