A sample demonstrating reading and writing of XML without a namespace.
When creating some form of XML yourself, you should be OK most of the time without requiring namespaces. The sample explains how to do basic reading and writing of XML.
#include <wchar.h>
#include <map>
#include <string>
using nlib_ns::exi::ExiAllocator;
using nlib_ns::exi::ExiAllocatorEx;
using nlib_ns::exi::XmlStreamReader;
using nlib_ns::exi::XmlStreamWriter;
using nlib_ns::exi::XmlStreamReaderSettings;
using nlib_ns::exi::XmlStreamWriterSettings;
#define N(x) NLIB_EXI_LITERAL(x)
#define M(x) NLIB_EXI_UTF8(x)
const int kBufSize = 1024 * 64;
unsigned char g_writerbuf[kBufSize];
unsigned char g_readerbuf[kBufSize];
const int kDataBufSize = 1024;
unsigned char g_data_buf[kDataBufSize];
struct CityInfo {
int population;
};
bool SampleExec() {
nlib_printf(
"Write and Read an XML without xmlnamespace\n\n");
ExiAllocatorEx al_writer;
if (
nlib_is_error(al_writer.Init(g_writerbuf, kBufSize)))
return false;
MemoryOutputStream os(g_data_buf, kDataBufSize);
XmlStreamWriterSettings wsettings;
#ifndef USE_TEXT
#else
#endif
UniquePtr<XmlStreamWriter> w(XmlStreamWriter::Create(&os, wsettings, al_writer));
if (!w.get()) return false;
w->WriteStartDocument();
w->WriteStartElement(N("cities"));
w->WriteStartElement(N("city"));
w->WriteAttribute(N("name"), N("Tokyo"));
w->WriteStartElement(N("url"));
w->WriteCharacters(N("http://www.metro.tokyo.jp/"));
w->WriteEndElement();
w->WriteStartElement(N("知事"));
w->WriteCharacters(N("Shintaro Ishihara"));
w->WriteEndElement();
w->WriteStartElement(N("人口"));
w->WriteCharacters(N("13186835"));
w->WriteEndElement();
w->WriteEndElement();
w->WriteStartElement(N("city"));
w->WriteAttribute(N("name"), N("Kyoto"));
w->WriteStartElement(N("url"));
w->WriteCharacters(N("http://www.pref.kyoto.jp/"));
w->WriteEndElement();
w->WriteStartElement(N("知事"));
w->WriteCharacters(N("Keiji Yamada"));
w->WriteEndElement();
w->WriteStartElement(N("人口"));
w->WriteCharacters(N("2633795"));
w->WriteEndElement();
w->WriteEndElement();
w->WriteEndElement();
w->WriteEndDocument();
w->Flush();
const void* ptr_xml = os.data();
size_t size_xml = os.Pos();
#ifndef USE_TEXT
const char* format = "Binary XML";
#else
const char* format = "Text XML";
#endif
w->Close();
return false;
}
w.reset(NULL);
al_writer.Reset();
typedef std::map<const ExiChar*, CityInfo> CityDB;
CityDB db;
ExiAllocatorEx al_reader;
if (
nlib_is_error(al_reader.Init(g_readerbuf, kBufSize)))
return false;
MemoryInputStream is(ptr_xml, size_xml);
XmlStreamReaderSettings rsettings;
#ifndef USE_TEXT
#else
#endif
UniquePtr<XmlStreamReader> r(XmlStreamReader::Create(&is, rsettings, al_reader));
if (!r.get()) return false;
CityInfo* info = NULL;
while (r->HasNext()) {
XmlStreamReader::XmlStreamConstants e = r->Next();
switch (e) {
case XmlStreamReader::START_ELEMENT:
elem = r->GetLocalName();
if (
StrCmp(elem, N(
"city")) == 0) {
info = &db[r->GetAttributeValue(N(""), N("name"))];
}
break;
case XmlStreamReader::CHARACTERS:
if (info) {
if (
StrCmp(elem, N(
"url")) == 0) {
info->url = r->GetText();
}
else if (
StrCmp(elem, N(
"知事")) == 0) {
info->governor = r->GetText();
}
else if (
StrCmp(elem, N(
"人口")) == 0) {
if (err != 0) info->population = 0;
}
}
break;
case XmlStreamReader::START_DOCUMENT:
case XmlStreamReader::END_DOCUMENT:
case XmlStreamReader::END_ELEMENT:
break;
default:
break;
}
}
return false;
}
ConsoleOutputStream out_;
TextWriter out;
out.Init();
out.Open(&out_);
CityDB::const_iterator it;
for (it = db.begin(); it != db.end(); ++it) {
out.WriteFormat("%s: \n", M(it->first));
out.WriteFormat("\turl: %s\n", M(it->second.url));
out.WriteFormat("\tgovernor: %s\n", M(it->second.governor));
out.WriteFormat("\tpopulation: %d\n", it->second.population);
}
return true;
}
bool SampleMain(int, char**) {
NLIB_ASSERT(!ExiAllocator::GetAllocator());
bool result = SampleExec();
NLIB_ASSERT(!ExiAllocator::GetAllocator());
return result;
}
NLIB_MAINFUNC