Mert Mertce
Mert Mertce

Reputation: 1203

rapidxml parses just one whole node

I am using RapidXML to parse a string of xml. There is my string:

std::string str("<?xml version=\"1.0\" encoding=\"UTF-8\"   
standalone=\"yes\"?><protocol version=\"1.5\"><srvResponse>
<dateTime>2016-10-18T08:51:50.657+01:00</dateTime><responseFrom ag=\"1\"     
/><idMessage>0</idMessage><rejectionCode>0</rejectionCode>
</srvResponse></protocol>");

And here is how I try to parse:

XML::xml_document<> doc;
doc.parse<0>((char*) str.c_str());
XML::xml_node<>* firstNode = doc.first_node();

However, what I get is that it is parsed as with just one node: protocol, ie, siblings, children of protocol are null, simply it does not have.

I think I am missing one principal thing.

Could you find and tell me?

Thank you.

Upvotes: 0

Views: 384

Answers (1)

Bob
Bob

Reputation: 31

You can save XML data to file, such as "1.xml", then you can do as following:

<?xml version="1.0" encoding="utf-8"?>
<protocol version="1.5">
  <srvResponse> 
        <dateTime>2016-10-18T08:51:50.657+01:00</dateTime>
        <responseFrom ag="1"/>
        <idMessage>0</idMessage>
        <rejectionCode>0</rejectionCode> 
  </srvResponse>
</protocol>


void ParseWithAtrribute(std::string strFilePath /*= "1.xml"*/)
{
    rapidxml::file<> docFile(strFilePath.c_str());
    rapidxml::xml_document<> doc;
    doc.parse<0>(docFile.data());

    rapidxml::xml_node<> *pRootNode = doc.first_node();
    if (pRootNode != NULL)
    {
        std::cout << pRootNode->name() << " " << pRootNode->value()<< std::endl; // protocol
        rapidxml::xml_attribute<> *pAttr = pRootNode->first_attribute();
        if (pAttr != NULL)
        {
            std::cout << pAttr->name() << " " << pAttr->value() << std::endl; // version
        }

        rapidxml::xml_node<> *pChildNode = pRootNode->first_node();
        if (pChildNode != NULL)
        {
            std::cout << pChildNode->name() << " " << pChildNode->value() << std::endl;
            rapidxml::xml_node<> * pSonNode = pChildNode->first_node();
            for(; pSonNode != NULL; pSonNode = pSonNode->next_sibling())
            {
                std::cout << pSonNode->name() << " " << pSonNode->value() << std::endl; 
                rapidxml::xml_attribute<> *pSonAttr = pSonNode->first_attribute();
                if (pSonAttr != NULL)
                {
                    std::cout << "  " << pSonAttr->name() << ":" << pSonAttr->value() << std::endl;
                }   
            }
        }
    }
}

Now we do not need to save xml data to file:

void ParseWithAtrribute()
{
    std::string str("<?xml version=\"1.0\" encoding=\"utf-8\"?> <protocol version=\"1.5\"> <srvResponse> <dateTime>2016-10-18T08:51:50.657+01:00</dateTime> <responseFrom ag=\"1\"/> <idMessage>0</idMessage> <rejectionCode>0</rejectionCode> </srvResponse> </protocol>");
    rapidxml::xml_document<> doc;
    doc.parse<0>((char *)(str.c_str()));

    rapidxml::xml_node<> *pRootNode = doc.first_node();
    if (pRootNode != NULL)
    {
        std::cout << pRootNode->name() << " " << pRootNode->value() << std::endl; // protocol
        rapidxml::xml_attribute<> *pAttr = pRootNode->first_attribute();
        if (pAttr != NULL)
        {
            std::cout << pAttr->name() << " " << pAttr->value() << std::endl; // version
        }

        rapidxml::xml_node<> *pChildNode = pRootNode->first_node();
        if (pChildNode != NULL)
        {
            std::cout << pChildNode->name() << " " << pChildNode->value() << std::endl;
            rapidxml::xml_node<> * pSonNode = pChildNode->first_node();
            for (; pSonNode != NULL; pSonNode = pSonNode->next_sibling())
            {
                std::cout << pSonNode->name() << " " << pSonNode->value() << std::endl;
                rapidxml::xml_attribute<> *pSonAttr = pSonNode->first_attribute();
                if (pSonAttr != NULL)
                {
                    std::cout << "  " << pSonAttr->name() << ":" << pSonAttr->value() << std::endl;
                }
            }
        }
    }
}

Upvotes: 1

Related Questions