0

The code is got from the http://www.grinninglizard.com/tinyxml2docs/_example-3.html

 static const char* xml =
        "<?xml version=\"1.0\"?>"
        "<!DOCTYPE PLAY SYSTEM \"play.dtd\">"
        "<PLAY>"
        "<TITLE>A Midsummer <b>Night's Dream</b></TITLE>"
        "</PLAY>";
XMLDocument doc;
doc.Parse(xml);
XMLElement* titleElement = doc.FirstChildElement("PLAY")->FirstChildElement("TITLE");
XMLText* textNode = titleElement->FirstChild()->ToText();
auto title = textNode->Value();

The title should contain A Midsummer <b>Night's Dream</b> as the introduction explain. But the result of code is still A Midsummer. Dose anyone can tell me what's wrong with my code, or some other function to achieve the parse.

mpromonet
  • 11,326
  • 43
  • 62
  • 91

2 Answers2

1

Here's a short but complete program that gets both parts of the string and the element in which the second part is found:

#include <stdio.h>
#include <stdlib.h>

#include "../tinyxml2.h"
using namespace tinyxml2;

int main(int argc, char *argv[])
{
    static const char* xml =
        "<?xml version=\"1.0\"?>"
        "<!DOCTYPE PLAY SYSTEM \"play.dtd\">"
        "<PLAY>"
        "<TITLE>A Midsummer Night's <b>Dream</b></TITLE>"
        "</PLAY>";

    XMLDocument doc;            /* DOM tree */

    doc.Parse(xml);         /* Parse the XML */

    XMLElement* titleElement =
    doc.FirstChildElement( "PLAY" )->FirstChildElement( "TITLE" );

    const char* title = titleElement->GetText();    /* cheap approach */
    printf( "Name of play (part 1): %s\n", title );

    XMLNode *tp = titleElement->FirstChild();
    printf("part 1 = %s\n", tp->Value());   /* A Midsummer Night's */

    tp = tp->NextSibling();         /* point to the <b> element */
    printf("element for part 2 = %s\n", tp->Value());   /* b */
    tp = tp->FirstChild();          /* point to b's text node */
    printf("part 2 = %s\n", tp->Value());   /* Dream */
}

The output is:

Name of play (part 1): A Midsummer Night's 
part 1 = A Midsummer Night's 
element for part 2 = b
part 2 = Dream

As was noted in an earlier answer, with the XML provided, the text is not presented as a single string. To get all the components requires traversing more of the tree.

0

Night's Dream is not in the element <TITLE> but in the element <b> that is inside <TITLE>.

If you would like to include <b>Night's Dream</b> in the text of the element , you should escape it (see http://en.wikipedia.org/wiki/List_of_XML_and_HTML_character_entity_references) :

static const char* xml =
        "<?xml version=\"1.0\"?>"
        "<!DOCTYPE PLAY SYSTEM \"play.dtd\">"
        "<PLAY>"
        "<TITLE>A Midsummer &lt;b&gt;Night's Dream&lt;/b&gt;</TITLE>"
        "</PLAY>";
mpromonet
  • 11,326
  • 43
  • 62
  • 91