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)2012-2016 Nintendo. All rights reserved.
These coded instructions, statements, and computer programs contain
proprietary information of Nintendo of America Inc. and/or Nintendo
Company Ltd., and are protected by Federal copyright law. 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.
*---------------------------------------------------------------------------*/
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 BUF_SIZE = 1024 * 128;
unsigned char g_BufRead[BUF_SIZE]; // Working memory for XmlStreamReader
unsigned char g_BufWrite[BUF_SIZE]; // Working memory for XmlStreamWrite
const int DATABUF_SIZE = 1024;
unsigned char g_DataBuf[DATABUF_SIZE];
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_BufRead, BUF_SIZE) != 0) return false;
if (al_write.Init(g_BufWrite, BUF_SIZE) != 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