A markup derived object, which can be one of the following, an element, pcdata, comment, or procinstr.
pcdat
A string argument which represents PCDATA to insert as content.
pos
An iterator which specifies the position of the markup object(s) to insert.
num
A positive integer amount which specifies the number of copies of mkup to insert.
beg
An iterator which specifies the beginning range of markup objects to insert.
end
An iterator which specifies the end of the range of markup objects to insert.
Returns
In the first four varities, returns an iterator which points to the inserted markup object.
Remarks
This family of operations are of the most used operations in the library. These operations offer a variety of ways to insert content into the
document tree. The operations can be called on element objects, and also iterators and descendant
iterators which point to element objects. Note that for any insertions to be successful, the insertion must be valid not
violate nesting rules for the particular doctype of the document.
The first variety inserts a markup object at the end of the current children. This operation is equivalent to the push_back() operation.
The second variety inserts PCDATA into the called element. The passed PCDATA is enclosed within a pcdata object before being
inserted into the called element.
The third variety inserts a markup object at the child position pos. This variety allows the caller to specify where the markup object
will be inserted in relation to other markup children which might already be present in the called element.
The forth variety inserts PCDATA at the child position pos. This variety allows the caller to specify where the PCDATA will be inserted
in relation to other markup children which might already be present in the called element. The passed PCDATA is enclosed within a pcdata
object before being inserted into the called element.
The fifth variety inserts num copies of mkup at the position pos.
The sixth variety inserts the markup objects in the range [beg, end), starting at the position pos. The markup objects in the specified
range can be children of the called element, or any other element.
Complexity
Logrithmic
Example
1st Variety
#include "xhtml_doc.h"
#include <iostream>
int main(int argc, char* argv[])
{
using namespace Xport;
// create an xhtml strict document
document doc;
element html_elem(html);
// insert head element in html
markup::iterator it = html_elem.insert(element(head));
// insert title element in head
it->insert(element(Xport::title));
// insert body element in html
html_elem.insert(element(body));
// insert html element in the document
doc << html_elem;
formatter fmtr(std::cout);
fmtr << doc;
return 0;
}
2nd Variety
#include "xhtml_doc.h"
#include <iostream>
int main(int argc, char* argv[])
{
using namespace Xport;
// create an xhtml strict document
document doc(root_doc);
doc.title()->insert("Sample document");
element para(p);
para.insert("This document displays the usage of element::insert(pcdata);");
para.insert("pcdata can be ");
para << (element(i) << "intermixed");
para.insert(" with inline elements. ");
para << "Inserting pcdata is often times simpler using the inserter << operation";
*doc.body() << para;
formatter fmtr(std::cout);
fmtr.option(max_line_length, 60);
fmtr << doc;
return 0;
}
3rd Variety
#include "xhtml_doc.h"
#include <iostream>
int main(int argc, char* argv[])
{
using namespace Xport;
// create an xhtml strict document
document doc(root_doc);
markup::iterator it = doc.body()->insert(element(ul));
*it << (element(li) << "List item 1");
*it << (element(li) << "List item 2");
*it << (element(li) << "List item 3");
// insert list item 1.5 between list items 1 and 2
markup::iterator lit = it->begin();
++lit;
it->insert(lit, (element(li) << "List item 1.5"));
formatter fmtr(std::cout);
fmtr.option(max_line_length, 60);
fmtr << doc;
return 0;
}
4th Variety
#include "xhtml_doc.h"
#include <iostream>
int main(int argc, char* argv[])
{
using namespace Xport;
// create an xhtml strict document
document doc(root_doc);
markup::iterator it = doc.body()->insert(element(p));
*it << "Test line 1. ";
*it << "Test line 2. ";
*it << "Test line 3. ";
*it << "Although all 3 lines above are considered pcdata, they exists as separate objects, since they were inserted separately.\n";
// insert pcdata between lines 1 and 2
markup::iterator pcit = it->begin();
++pcit;
it->insert(pcit, "Test line 1.5 ");
formatter fmtr(std::cout);
fmtr.option(max_line_length, 60);
fmtr << doc;
return 0;
}
5th Variety
#include "xhtml_doc.h"
#include <iostream>
int main(int argc, char* argv[])
{
using namespace Xport;
// create an xhtml strict document
document doc(root_doc);
markup::iterator it = doc.body()->insert(element(ul));
element litem(li);
litem << "List item";
it->insert(it->begin(), 6, litem);
formatter fmtr(std::cout);
fmtr.option(max_line_length, 60);
fmtr << doc;
return 0;
}
6th Variety
#include "xhtml_doc.h"
#include "xport_brochure.h"
#include <iostream>
#include <algorithm>
// function object for std::find_if
struct elem_has_id
{
elem_has_id(const std::string& id_str) : elem_id(id_str) {}
bool operator()(const Xport::markup& mkup) { return mkup.attribute(Xport::id) == elem_id; }
std::string elem_id;
};
int main(int argc, char* argv[])
{
using namespace Xport;
// create an xhtml strict document
document doc(root_doc);
// create xport brochure
xport_brochure broch;
broch.create(doc);
// find 'class_categories' list
markup::const_descendant_iterator dit = std::find_if(doc.descendant_begin(), doc.descendant_end(), elem_has_id("class_categories"));
if (dit != doc.descendant_end()) {
// create a new doc
document doc2(root_doc);
// copy the li elements to the new doc
markup::iterator it = doc2.body()->insert(element(ul));
it->insert(it->begin(), dit->begin(), dit->end());
formatter fmtr(std::cout);
fmtr.option(max_line_length, 60);
fmtr << doc2;
}
return 0;
}
Datasoft Solutions is proud to present their landmark product, Work Time Studio. Utilizing the tree container library, Work Time Studio provides
unparalleled features in personal time, project and task management
software. If you're looking for a way to increase your productivity,
visit Work Time Studio's product website, and start being more productive
today.