Menu

getting element text

Anonymous
2004-03-10
2004-03-15
  • Anonymous

    Anonymous - 2004-03-10

    how do get the string "john: from the folowing xml?

    <person>
        <name>john</name>
    </person>
    the following code prints the tagname and not the contents.

    TiXmlDocument doc;
    doc.Parse("<person><name>john</name></person>");
    TiXmlElement *root = doc.RootElement();
    cout << root->Value() << endl;
    TiXmlNode *name = root->FirstChild();
    cout << name->Value() << endl;

    doing name->ToText() instead returns null

    any help would be appreciated.

     
    • B Sizer

      B Sizer - 2004-03-10

      Did you read the documentation? The bit about "How TinyXml works" should show you the node type you're looking for.

       
    • Anonymous

      Anonymous - 2004-03-11

      i guess i didn't read it well enough. I didn't realize the text was a child. Why not have a function to get the (text)content of an element?

       
    • Yves Berquin

      Yves Berquin - 2004-03-11

      You can use the following function to get the text child. It returns an empty C-string or the text if it exists. All possible checks are being made before.

      const char * GetTextChild (TiXmlElement * mother)
      {
         if (! mother || ! (mother -> FirstChild ()) || ! (mother -> FirstChild () -> ToText ()) || ! (mother -> FirstChild () -> ToText () -> Value ()))
            return "";
         return mother -> FirstChild () -> ToText () -> Value ();
      }

       
    • Lee Thomason

      Lee Thomason - 2004-03-15

      The reason that tinyxml doesn't have a "getText" method on TiXmlElement is this:

      <element>This is <b>bold</b> and this is not.</element>

      There is no simple text call - that I can think of - that can correctly return the text of "element", because it has 2 or 3 text strings in it, depending on your point of view. What would element.getText() return? "This is"? "This is and this is not?" And how do you get to the "bold"?

      Actually, a good solution may be to enhance "TiXmlHandle" so that the Text() method returns a char* instead of a Text Element. Then you would have super convenient text access, without the strangeness of the case above.

      lee

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.