tutorial_attribute.cpp

A short program to learn all attribute feature

With this example, we see how to put and extract data from the attribute object. We also learn how to deal with namespace.

#include <iostream>
#include <ximol/ximol.hpp>

using namespace std;
using namespace ximol;

// main function
int main()
{
    double d1=3.01,d2=4.1;
    xml::attributes att;

    //---------------
    // SET NAMESPACES
    //---------------
    // set the default namespace
    set_namespace(att,"http://default_namespace.com/"); 

    // set a namespace shortcut
    set_namespace(att,"ns1","http://namespace_1.com/"); 
    // set a namespace shortcut
    set_namespace(att,"ns2","http://namespace_2.com/"); 

    //---------------
    // GET NAMESPACES
    //---------------
    // get the default namespace
    cout << "default namespace=" << str<std::string>::cast(get_namespace(att)) << endl;

    // get the uri
    cout << "namespace 1 =" << str<std::string>::cast(get_namespace(att,"ns1")) << endl;
    // get the uri
    cout << "namespace 2 =" << str<std::string>::cast(get_namespace(att,"ns2")) << endl;
    // error
    cout << "namespace 3 =" << str<std::string>::cast(get_namespace(att,"ns3")) << endl;

    // An empty string is the short cut for the default namespace
    cout << "default namespace=" << str<std::string>::cast(get_namespace(att,"")) << endl;

    // get the short namespace
    cout << "http://namespace_1.com/=" << str<std::string>::cast(get_short_namespace(att,"http://namespace_1.com/")) << endl;
    // get the short namespace
    cout << "http://namespace_2.com/=" << str<std::string>::cast(get_short_namespace(att,"http://namespace_2.com/")) << endl;

    // error
    cout << "http://namespace_3.com/=" << str<std::string>::cast(get_short_namespace(att,"http://namespace_3.com/")) << endl;

    //---------------
    // SET ATTRIBUTES
    //---------------
    // insert the first value
    // this attribute has the default namespace
    insert_attribute(att,"the_first_one","the first one value");

    // try to replace this value 
    // Warning it does not work because, the attribute nalready exist
    // You must use set_attribute in this case
    insert_attribute(att,"the_first_one","the first one value replaced");

    // OK, this code is the one
    set_attribute(att,"the_first_one","the first one value replaced by a set_attributes");

    // serialization of attribute
    // You could add any attribute type
    // the attribute value is str<xstring>::cast(d1)
    set_attribute(att,"a_double",d1); 

    //---------------
    // GET ATTRIBUTES
    //---------------
    // get the string from attribute
    cout << "the_first_one=" << str<std::string>::cast(get_raw_attribute(att,"the_first_one")) << endl;

    // get the attribute
    get_attribute(att,"a_double",d2);
    cout << "d2=" << d2 << endl;
    
    // extract attribute
    d2=1+extract_attribute<double>::get(att,"a_double");
    cout << "d2=" << d2 << endl;

    //------------------------------
    // SET ATTRIBUTES WITH NAMESPACE
    //------------------------------
    // without namespace, the namespace is the default namespace
    insert_attribute(att,"test_ns_1","the test_ns_1 value");
    set_attribute(att,"test_ns_2","the test_ns_2 value");

    // you could set the namespace 
    // with the short cut
    set_attribute(att,"ns1","test_ns_3","the test_ns_3 value");

    // you could set the namespace 
    // with the real name
    set_attribute(att,"http://namespace_1.com/","test_ns_4","the test_ns_5 value");

    // with namespace, you could avoid conflict
    set_attribute(att,"conflict_name","the value with default namespace");
    set_attribute(att,"ns1","conflict_name","the value with namespace 1");
    set_attribute(att,"ns2","conflict_name","the value with namespace 2");

    //------------------------------
    // GET ATTRIBUTES WITH NAMESPACE
    //------------------------------
    // In this part, we will use only extract_attribute, but
    // you could use all other function
    cout << "test_ns_1=" << extract_attribute<string>::get(att,"test_ns_1") << endl;;
    cout << "test_ns_2=" << extract_attribute<string>::get(att,"","test_ns_2") << endl;;

    // you could set the namespace 
    // with the short cut
    cout << "test_ns_3=" << extract_attribute<string>::get(att,"ns1","test_ns_3") << endl;;

    // you could set the namespace 
    // with the real name
    cout << "test_ns_4=" << extract_attribute<string>::get(att,"http://namespace_1.com/","test_ns_4") << endl;

    // error
    cout << "test_ns_4=" << extract_attribute<string>::get(att,"test_ns_4") << endl;;

    //// with namespace, you could avoid conflict
    cout << "ns1:conflict_name=" << extract_attribute<string>::get(att,"http://namespace_1.com/","conflict_name") << endl;
    cout << "ns2:conflict_name=" << extract_attribute<string>::get(att,"ns2","conflict_name") << endl;
    cout << "conflict_name=" << extract_attribute<string>::get(att,"conflict_name") << endl;

    // print the attributes
    cout << "attributes=" << att << endl;
    return 0;
};


Donate to the XiMoL project SourceForge.net Logo If you have any questions about XiMoL, you could write to tournois@users.sourceforge.net.