C++ tinyxml2::Attribute()














































C++ tinyxml2::Attribute()




Attribute():-

Given an attribute name,Attribute() returns the value for the attribute of that name,or null if none exists.


Syntax:-


const char * tinyxml2::XMLElement::Attribute(const char * name, const char * value=0)


Example:- attribute.cpp


#include "tinyxml2.h"
#include <iostream>
using namespace std;
int main(){

    static const char* xml =
    "<?xml version=\"1.0\" encoding=\"utf-8\" ?>"
    "<entries>"
    "<entry name=\"OutPut \" >Simple Block of Code to demonstrate Working of Attribute() func. of tinyxml2</entry>"
    "<entry name=\"Article About Attribute() Function\" >Attribute() returns the value for the attribute of that name, or null if none exists.</entry>"
    "</entries>";
    tinyxml2::XMLDocument doc;
    doc.Parse(xml);
    tinyxml2::XMLHandle docHandle(&doc);
    tinyxml2::XMLElement *entry = docHandle.FirstChildElement("entries").ToElement();

    if(entry){
        for(tinyxml2::XMLNode *node = entry->FirstChildElement(); node; node = node->NextSibling()){
            tinyxml2::XMLElement *e = node->ToElement();
            const char *name = e->Attribute("name");
            if(name) cout<<name<<": ";
            cout<<e->GetText()<<endl;
            }
        }
    return 0;
}


Output:-





Comments