Menu

about TinyXml's parse

Developer
Hydralisk
2006-07-07
2013-05-20
  • Hydralisk

    Hydralisk - 2006-07-07

    form 1.From this XML:
    @verbatim
    <foo>This is <b>text</b></foo>
    @endverbatim
    GetText() will return "This is ".

    form 2.From this XML:
    @verbatim
    <foo>This is <b color="255,255,0">colorful</b> text</foo>
    @endverbatim
    TinyXml return 3 element: "This is ", "colorful" and " text".

    form 3.From this XML:
    @verbatim
    <foo>This is <b color="255,255,0">colorful</b> text</foo>
    @endverbatim
    TinyXml only return 2 element: "This is " and "colorful".

    I wish to support form 3 in my application. I write code like this:
    for (elem = elem->FirstChildElement(); elem; elem = elem->NextSiblingElement())
    {
        if (elem->GetText())
            printf("%s\n", elem->GetText());
        for (elem1 = elem->FirstChildElement(); elem1; elem1 = elem1->NextSiblingElement())
        {
            if (elem1->GetText())
                printf("%s\n", elem1->GetText());
        }
    }
    The output is:
    This is
    colorful

    Is my code correct?

     
    • Nicola Civran

      Nicola Civran - 2006-07-07

      Sorry, but I see no difference between form 2 and form 3...

      Anyway, being the XML structure as follows:
      <ELEMENT "foo">
      ....<TEXT "This is ">
      ....<ELEMENT "b">
      ........<TEXT "colorful">
      ....<TEXT " text">
      there is no way to get 3rd text node content using GetText(). You have to change your approach (perhaps a recursive function is a better choice).

       

Log in to post a comment.