Example Code


libXMLement:

Here's a basic example of how to create an element
// Example #1:
// Create an anchor element

#include <Element.h>

int main()
{
   // Create a new Element with the name "a"
   Element a("a");

   // All Element's have the operator<< overloaded
   // So, to output, all you have to do is this:
   cout << a << endl;

   return 0;
}


Now, add some attributes to make this a real link:
// Example #2:
// Create an anchor element and
// add some attributes

#include <Element.h>

int main()
{
   // Create a new Element with the name "a"
   Element a("a");

   // All elements use a Map class
   // based on the C++ standard map class to hold
   // their attributes and values.
   // This makes it extremely easy and intuitive to
   // make or modify the element's attributes

   // add an href, a title, and even some style
   a["href"] = "http://www.buhbird.com";
   a["title"] = "Click here for my homepage";
   a["style"] = "background-color: lightyellow";

   // All Element's have the operator<< overloaded
   // So, to output, all you have to do is this:
   cout << a << endl;

   return 0;
}


And some text would be nice:
// Example #3:
// Create an anchor element, add some attributes
// and some text

#include <Element.h>
#include <libXMLTree.h>

// So we don't have to keep prefixing
// libXMLTree:: to it's functions
using namespace libXMLTree;

int main()
{
  // Create a new Element with the name "a"
  Element a("a");

  // Add an href, a title, and even some style
  a["href"] = "http://www.buhbird.com";
  a["title"] = "Click here for my homepage";
  a["style"] = "background-color: lightyellow";

  // Add some text to the link
  // (This Add() function comes from libXMLTree,
  //  see that library's documentation for more info)

  // This function will add a text node to the given
  // element
  Add(&a, "www.buhbird.com");

  // All Element's have the operator<< overloaded
  // So, to output, all you have to do is this:
  cout << a << endl;

  return 0;
}


On second thought, let's use an image instead:
// Example #4:
// Create an anchor element, add some attributes
// and replace the text with an image

#include <EmptyElement.h>
#include <libXMLTree.h>

// So we don't have to keep prefixing
// libXMLTree:: to it's functions
using namespace libXMLTree;

int main()
{
  // Create a new Element with the name "a"
  Element a("a");

  // Add an href, a title, and even some style
  a["href"] = "http://www.buhbird.com";
  a["title"] = "Click here for my homepage";
  a["style"] = "background-color: lightyellow";

  // This function will add a text node to the given
  // element
  Add(&a, "www.buhbird.com");

  // I changed my mind, instead of the text,
  // I want a picture
 
  // So, let's first create the img element
  // Remeber it is and Empty Element <img/>
  EmptyElement img("img");
 
  // Give it some attributes
  img["width"] = "71";
  img["height"] = "33";
  img["alt"] = "http://www.buhbird.com";
  img["src"] = "http://www.buhbird.com/~kevin/docs/examples/link.png";
  img["border"] = "0";

  // So, let's delete the text
  // (Delete() is also in libXMLTree)
  Delete(&a, "www.buhbird.com");

  // And add the image instead
  Add(&a, &img);

  // All Element's have the operator<< overloaded
  // So, to output, all you have to do is this:
  cout << a << endl;

  return 0;
}


Here's what that code outputs.