nlib
exi/textparser/textparser.cpp

テキストの(通常の)XMLをパースするサンプルです。

whatsnew.cppwhatsnewに格納されたXML文字列をパースしてtitle要素のテキストを取り出し、それらを新しいXMLに書きだします。

XmlStreamReaderSettings及びXmlStreamWriterSettingsprocessorメンバをXML_PROCESSOR_TEXTに設定してXmlStreamReader又はXmlStreamWriterを作成することによりテキストのXMLを読み書きすることができます。 XMLはUTF-8で記述されている必要があります。

以下がサンプルのソースコードになります。

/*--------------------------------------------------------------------------------*
Project: CrossRoad
Copyright (C)Nintendo All rights reserved.
These coded instructions, statements, and computer programs contain proprietary
information of Nintendo and/or its licensed developers and are protected by
national and international copyright laws. They may not be disclosed to third
parties or copied or duplicated in any form, in whole or in part, without the
prior written consent of Nintendo.
The content herein is highly confidential and should be handled accordingly.
*--------------------------------------------------------------------------------*/
using nlib_ns::exi::ExiAllocatorEx;
using nlib_ns::exi::XmlStreamReader;
using nlib_ns::exi::XmlStreamReaderSettings;
using nlib_ns::exi::XmlStreamWriter;
using nlib_ns::exi::XmlStreamWriterSettings;
#define N(x) NLIB_EXI_LITERAL(x)
#define M(x) NLIB_EXI_UTF8(x)
extern const char* whatsnew;
const int kBufSize = 1024 * 128;
unsigned char g_readerbuf[kBufSize]; // Working memory for XmlStreamReader
unsigned char g_writerbuf[kBufSize]; // Working memory for XmlStreamWrite
const int kDataBufSize = 1024;
unsigned char g_data_buf[kDataBufSize];
bool SampleExec() {
// Work memories for reading/writing are different in this sample.
// You can use the same working memory.
ExiAllocatorEx al_read, al_write;
if (al_read.Init(g_readerbuf, kBufSize) != 0) return false;
if (al_write.Init(g_writerbuf, kBufSize) != 0) return false;
ConsoleOutputStream out_;
TextWriter out;
out.Init();
out.Open(&out_);
MemoryInputStream istr(whatsnew, nlib_strlen(whatsnew));
XmlStreamReaderSettings is;
UniquePtr<XmlStreamReader> r(XmlStreamReader::Create(&istr, is, al_read));
if (!r) return false;
ReallocOutputStream ostr;
XmlStreamWriterSettings os;
UniquePtr<XmlStreamWriter> w(XmlStreamWriter::Create(&ostr, os, al_write));
if (!w) return false;
w->WriteStartDocument();
w->WriteStartElement(N("titles"));
nlib_printf("titles read: \n");
bool is_target_element = false;
while (r->HasNext()) {
XmlStreamReader::XmlStreamConstants e = r->Next();
switch (e) {
case XmlStreamReader::START_ELEMENT:
is_target_element = StrCmp(r->GetLocalName(), N("title")) == 0;
break;
case XmlStreamReader::CHARACTERS:
if (is_target_element) {
w->WriteStartElement(N("title"));
w->WriteCharacters(r->GetText());
w->WriteEndElement();
out.WriteFormat("%s\n", M(r->GetText()));
}
break;
default:
break;
}
}
w->WriteEndElement();
w->WriteEndDocument();
if (nlib_is_error(*r)) {
nlib_printf("error in reader\n");
return false;
}
if (nlib_is_error(*w)) {
nlib_printf("error in writer\n");
return false;
}
r.reset();
w.reset();
nlib_printf("\nwritten xml:\n");
ReallocOutputStream::CharPtrType cstr;
ostr.Flush();
if (nlib_is_error(ostr.ReleaseAsCstring(&cstr))) return false;
out.WriteFormat("%s\n", cstr.get());
return true;
}
bool SampleMain(int, char**) {
bool rval = SampleExec();
return rval;
}
NLIB_MAINFUNC