You are viewing a plain text version of this content. The canonical link for it is here.
Posted to general@xerces.apache.org by Nathan Troxler <n....@tic.ch> on 2000/04/01 10:15:02 UTC

Newby question: Get Text of a Node

Hello all!

I just began to use Apache XML for my C++ /MFC app.
So far I can read in my XML-File and read the data.

I used the sample code to get access to nodes, names and values.
I know how to access the attributes and corresonding values.

But I am a bit confused about getting the Text inside a Tag:

In the XML I have:
...
<NOTE>This is the text to read for the note</NOTE>
...

Currently I use the following, which seems to work, but seems also
to be a bit complicated. Is there an easier/safer way to get
the text ("This is the text to read for the note").

// Handler to extract the text of a Node with Name "NOTE"
//
void XMLParser::ParseVerbNote (DOM_Node& rNote)
{
 /* No, this obviously does not work!!!
 DOMString ds = rNote.getNodeValue();
 CString   cs = ds.rawBuffer();
  */

 CString note("");

 DOM_NodeList childs = rNote.getChildNodes();

// Works, but seems complicated! Is the direct text of a node alway childs.item(0) ???
 int cntCld = childs.getLength();
 if( cntCld != 0 ) {
  for(int j = 0; j < cntCld; j++ ) {
   DOM_Node child = childs.item(j);
   DOMString ds = child.getNodeValue();
   note = ds.rawBuffer();
  }
 }
  dp_bufAgt->SetVerbNote((LPCTSTR)note);  // writing to my internal data format
}

Thanks for any hints.

Nathan Troxler
n.troxler@tic.ch


Re: Newby question: Get Text of a Node

Posted by Andy Heninger <he...@us.ibm.com>.
The text of an element is contained in text nodes that are children of the element node.  It's always a little awkward to get it out.

Elements can have any number of children, so you may want to generalize your code a bit.   Even if there are no nested elements, comments and entity reference nodes can still appear.  You probably want to ignore comments altogether and recursively look for more text nodes among the children of any entity ref nodes.

It may be sligthly simpler to use Node.getFirstChild() and getNextSibling() methods for navigation, rather than the node list.

  -- Andy
  ----- Original Message ----- 
  From: Nathan Troxler 
  To: xerces-dev@xml.apache.org 
  Sent: Saturday, April 01, 2000 1:15 AM
  Subject: Newby question: Get Text of a Node


  But I am a bit confused about getting the Text inside a Tag:
   
  In the XML I have:
  ...
  <NOTE>This is the text to read for the note</NOTE>
  ...
   
  Currently I use the following, which seems to work, but seems also
  to be a bit complicated. Is there an easier/safer way to get
  the text ("This is the text to read for the note").
   
  // Handler to extract the text of a Node with Name "NOTE"
  //
  void XMLParser::ParseVerbNote (DOM_Node& rNote)
  {
   /* No, this obviously does not work!!!
   DOMString ds = rNote.getNodeValue();
   CString   cs = ds.rawBuffer();
    */
   
   CString note("");
   
   DOM_NodeList childs = rNote.getChildNodes();
   
  // Works, but seems complicated! Is the direct text of a node alway childs.item(0) ???
   int cntCld = childs.getLength();
   if( cntCld != 0 ) {
    for(int j = 0; j < cntCld; j++ ) {
     DOM_Node child = childs.item(j);
     DOMString ds = child.getNodeValue();
     note = ds.rawBuffer();
    }
   }
    dp_bufAgt->SetVerbNote((LPCTSTR)note);  // writing to my internal data format
  }

  Thanks for any hints.
   
  Nathan Troxler
  n.troxler@tic.ch