Write an XML content

The first thing to do when reading or writing a XML file/stream, is to declare the Document interface

Include exml

#include <exml/exml.hpp>

Declare document interface

Write an XML file

Write an xml tree is done like:

bool retGenerate = doc.store("generate.xml");

Write an XML Stream

Writing a stream is done like this:

std::string streamOut;
bool retGenerate = doc.generate(streamOut);

Operation on Tree

Add Node/Declaration:

Add an Node/Element:

exml::Element elem = exml::Element("exml");
doc.nodes.add(elem);

Remove a Node/Element:

// remove all node with this name
elem.nodes.remove("attr1");

Add an attribute (simple version):

elem.attributes.set("attr1", "value attr 1");

Add an attribute (complex version):

elem.attributes.add(exml::Attribute("attr2", "value attr 2"));

Remove an attribute:

elem.attributes.remove("attr1");

Object concept

the exml concept is to abstract the implementation of the internal system. All the element are maped on shared memory. Then if you asign an element to an other, it is the same. You need to clone it if you want to have new standalone element.

All example file

#include <test-debug/debug.hpp>
#include <exml/exml.hpp>
#include "write.hpp"
static void writeToFile() {
doc.nodes.add(exml::Element("node1"));
doc.nodes.add(exml::Element("node2"));
doc.nodes.add(exml::Comment("basic comment"));
TEST_INFO("store");
bool retGenerate = doc.store("generate.xml");
TEST_INFO("parse ret = " << retGenerate);
TEST_INFO("Debug display of the tree:");
doc.display();
}
static void writeToString() {
doc.nodes.add(exml::Element("node1"));
doc.nodes.add(exml::Element("node2"));
doc.nodes.add(exml::Comment("basic comment"));
TEST_INFO("generate");
std::string streamOut;
bool retGenerate = doc.generate(streamOut);
TEST_INFO("parse ret = " << retGenerate);
TEST_INFO("Debug display of the tree:");
doc.display();
}
static void writeAll() {
exml::Element elem = exml::Element("exml");
doc.nodes.add(elem);
elem.attributes.set("attr1", "value attr 1");
elem.attributes.add(exml::Attribute("attr2", "value attr 2"));
doc.display();
elem.attributes.remove("attr1");
elem.nodes.add(exml::Element("node1"));
elem.nodes.add(exml::Element("node2"));
elem.nodes.add(exml::Element("node1"));
// remove all node with this name
elem.nodes.remove("attr1");
}
void appl::write() {
writeToFile();
writeToString();
writeAll();
}