Revisión | 15 (tree) |
---|---|
Tiempo | 2011-05-03 12:01:38 |
Autor | (del#42492) |
サンプルを整理し、動作を確認した。
@@ -15,7 +15,7 @@ | ||
15 | 15 | void endElement (const XMLCh* const uri, const XMLCh* const localname, |
16 | 16 | const XMLCh* const qname); |
17 | 17 | |
18 | - void characters(const XMLCh* const chars, const unsigned int length); | |
18 | + void characters(const XMLCh* const chars, const XMLSize_t length); | |
19 | 19 | }; |
20 | 20 | |
21 | 21 | #endif |
@@ -9,6 +9,7 @@ | ||
9 | 9 | using namespace xercesc; |
10 | 10 | |
11 | 11 | #include <xercesc_utils/XMLCh2Char.hpp> |
12 | +#include <xercesc_utils/XMLChPtr.hpp> | |
12 | 13 | using namespace xercesc_utils; |
13 | 14 | |
14 | 15 | /** |
@@ -16,7 +17,7 @@ | ||
16 | 17 | */ |
17 | 18 | void SampleHandler::startElement(const XMLCh* const uri, const XMLCh* const localname, |
18 | 19 | const XMLCh* const qname, const Attributes& attrs) { |
19 | - cout << "start : " << XMLCh2Char(localname) << endl; | |
20 | + cout << "start : " << XMLCh2Char(localname) << endl; | |
20 | 21 | } |
21 | 22 | |
22 | 23 | /** |
@@ -24,17 +25,12 @@ | ||
24 | 25 | */ |
25 | 26 | void SampleHandler::endElement(const XMLCh* const uri, const XMLCh* const localname, |
26 | 27 | const XMLCh* const qname) { |
27 | - cout << "end : " << XMLCh2Char(localname) << endl; | |
28 | + cout << "end : " << XMLCh2Char(localname) << endl; | |
28 | 29 | } |
29 | 30 | |
30 | 31 | /** |
31 | 32 | * タグ内の文字列を検出したら呼び出されるメソッドです。 |
32 | 33 | */ |
33 | -void SampleHandler::characters(const XMLCh* const chars, const unsigned int length) { | |
34 | - XMLCh* buffer=new XMLCh[XMLString::stringLen(chars)+1]; | |
35 | - XMLString::copyString(buffer, chars); | |
36 | - XMLString::trim(buffer); | |
37 | - | |
38 | - cout << "content: " << XMLCh2Char(buffer) << endl; | |
39 | - delete[] buffer; | |
34 | +void SampleHandler::characters(const XMLCh* const chars, const XMLSize_t length) { | |
35 | + cout << "content: " << XMLCh2Char(XMLChPtr(chars).trim()) << endl; | |
40 | 36 | } |
@@ -9,27 +9,27 @@ | ||
9 | 9 | using namespace xercesc; |
10 | 10 | |
11 | 11 | int main() { |
12 | - // Xerces-C++を初期化する | |
13 | - try { | |
14 | - XMLPlatformUtils::Initialize(); | |
15 | - } catch(...) { | |
16 | - cerr << "Xerces-C++の初期化に失敗しました。" << endl; | |
17 | - return 1; | |
18 | - } | |
12 | + // Xerces-C++を初期化する | |
13 | + try { | |
14 | + XMLPlatformUtils::Initialize(); | |
15 | + } catch(...) { | |
16 | + cerr << "Xerces-C++の初期化に失敗しました。" << endl; | |
17 | + return 1; | |
18 | + } | |
19 | 19 | |
20 | - SAX2XMLReader* parser=XMLReaderFactory::createXMLReader(); | |
20 | + SAX2XMLReader* parser = XMLReaderFactory::createXMLReader(); | |
21 | 21 | |
22 | - try { | |
23 | - SampleHandler handler; | |
24 | - parser->setContentHandler(&handler); | |
25 | - parser->parse("example.xml"); | |
26 | - } catch(...) { | |
27 | - cerr << "ファイルの解析に失敗しました。" << endl; | |
28 | - } | |
22 | + try { | |
23 | + SampleHandler handler; | |
24 | + parser->setContentHandler(&handler); | |
25 | + parser->parse("data/example.xml"); | |
26 | + } catch(...) { | |
27 | + cerr << "ファイルの解析に失敗しました。" << endl; | |
28 | + } | |
29 | 29 | |
30 | - delete parser; | |
30 | + delete parser; | |
31 | 31 | |
32 | - XMLPlatformUtils::Terminate(); | |
32 | + XMLPlatformUtils::Terminate(); | |
33 | 33 | |
34 | - return 0; | |
34 | + return 0; | |
35 | 35 | } |
@@ -0,0 +1,8 @@ | ||
1 | +<?xml version="1.0" encoding="Shift_JIS"?> | |
2 | + | |
3 | +<root-element> | |
4 | + <element1>sample1</element1> | |
5 | + <element2> | |
6 | + <element3>sample3</element3> | |
7 | + </element2> | |
8 | +</root-element> |
@@ -17,26 +17,28 @@ | ||
17 | 17 | */ |
18 | 18 | class SampleHandler : public xercesc::DefaultHandler { |
19 | 19 | private: |
20 | - xercesc_utils::XMLCh2CharTranscoder transcoder_; | |
20 | + xercesc_utils::XMLCh2CharTranscoder transcoder_; | |
21 | 21 | |
22 | - /** | |
23 | - * XMLCh*をShift_JISのchar*に変換します。 | |
24 | - */ | |
25 | - xercesc_utils::CharPtr transcode(const XMLCh* const buffer) const { | |
26 | - return transcoder_.transcode(buffer); | |
27 | - } | |
22 | + /** | |
23 | + * XMLCh*をShift_JISのchar*に変換します。 | |
24 | + */ | |
25 | + xercesc_utils::CharPtr transcode(const XMLCh* const buffer) const { | |
26 | + return transcoder_.transcode(buffer); | |
27 | + } | |
28 | 28 | |
29 | 29 | public: |
30 | - SampleHandler() : transcoder_("Shift_JIS") { | |
31 | - } | |
30 | + SampleHandler() : transcoder_("Shift_JIS") { | |
31 | + } | |
32 | 32 | |
33 | 33 | public: |
34 | - void startElement(const XMLCh* const uri, const XMLCh* const localname, | |
35 | - const XMLCh* const qname, const xercesc::Attributes& attrs); | |
36 | - void endElement (const XMLCh* const uri, const XMLCh* const localname, | |
37 | - const XMLCh* const qname); | |
34 | + void startElement( | |
35 | + const XMLCh* const uri, const XMLCh* const localname, | |
36 | + const XMLCh* const qname, const xercesc::Attributes& attrs); | |
37 | + void endElement( | |
38 | + const XMLCh* const uri, const XMLCh* const localname, | |
39 | + const XMLCh* const qname); | |
38 | 40 | |
39 | - void characters(const XMLCh* const chars, const unsigned int length); | |
41 | + void characters(const XMLCh* const chars, const XMLSize_t length); | |
40 | 42 | }; |
41 | 43 | |
42 | 44 | #endif |
@@ -7,6 +7,7 @@ | ||
7 | 7 | |
8 | 8 | #include "SampleHandler.hpp" |
9 | 9 | |
10 | +#include <string> | |
10 | 11 | #include <iostream> |
11 | 12 | using namespace std; |
12 | 13 |
@@ -20,21 +21,24 @@ | ||
20 | 21 | */ |
21 | 22 | void SampleHandler::startElement(const XMLCh* const uri, const XMLCh* const localname, |
22 | 23 | const XMLCh* const qname, const Attributes& attrs) { |
23 | - cout << "start : " << transcode(localname) << endl; | |
24 | + cout << "start : " << transcode(localname) << endl; | |
24 | 25 | } |
25 | 26 | |
26 | 27 | /** |
27 | 28 | * タグの終了を検出したら呼び出されるメソッドです。 |
28 | 29 | */ |
29 | -void SampleHandler::endElement(const XMLCh* const uri, const XMLCh* const localname, | |
30 | - const XMLCh* const qname) { | |
31 | - cout << "end : " << transcode(localname) << endl; | |
30 | +void SampleHandler::endElement( | |
31 | +const XMLCh* const uri, const XMLCh* const localname, | |
32 | +const XMLCh* const qname) { | |
33 | + std::string name = transcode(localname); | |
34 | + | |
35 | + cout << "end : " << name << endl; | |
32 | 36 | } |
33 | 37 | |
34 | 38 | /** |
35 | 39 | * タグ内の文字列を検出したら呼び出されるメソッドです。 |
36 | 40 | */ |
37 | -void SampleHandler::characters(const XMLCh* const chars, const unsigned int length) { | |
38 | - CharPtr test = transcode(chars); | |
39 | - cout << "content: " << transcode(XMLChPtr(chars).trim()) << endl; | |
41 | +void SampleHandler::characters(const XMLCh* const chars, const XMLSize_t length) { | |
42 | + CharPtr test = transcode(chars); | |
43 | + cout << "content: " << transcode(XMLChPtr(chars).trim()) << endl; | |
40 | 44 | } |
@@ -16,29 +16,29 @@ | ||
16 | 16 | using namespace xercesc; |
17 | 17 | |
18 | 18 | int main() { |
19 | - // Xerces-C++を初期化する | |
20 | - try { | |
21 | - XMLPlatformUtils::Initialize(); | |
22 | - } catch(...) { | |
23 | - cerr << "Xerces-C++の初期化に失敗しました。" << endl; | |
24 | - return 1; | |
25 | - } | |
19 | + // Xerces-C++を初期化する | |
20 | + try { | |
21 | + XMLPlatformUtils::Initialize(); | |
22 | + } catch(...) { | |
23 | + cerr << "Xerces-C++の初期化に失敗しました。" << endl; | |
24 | + return 1; | |
25 | + } | |
26 | 26 | |
27 | - { | |
28 | - SampleHandler handler; | |
27 | + { | |
28 | + SampleHandler handler; | |
29 | 29 | |
30 | - SAX2XMLReader* parser=XMLReaderFactory::createXMLReader(); | |
31 | - parser->setContentHandler(&handler); | |
32 | - parser->setErrorHandler(&handler); | |
30 | + SAX2XMLReader* parser=XMLReaderFactory::createXMLReader(); | |
31 | + parser->setContentHandler(&handler); | |
32 | + parser->setErrorHandler(&handler); | |
33 | 33 | |
34 | - try { | |
35 | - parser->parse("example.xml"); | |
36 | - } catch(...) { | |
37 | - cerr << "ファイルの解析に失敗しました。" << endl; | |
38 | - } | |
34 | + try { | |
35 | + parser->parse("data/example.xml"); | |
36 | + } catch(...) { | |
37 | + cerr << "ファイルの解析に失敗しました。" << endl; | |
38 | + } | |
39 | 39 | |
40 | - delete parser; | |
41 | - } | |
40 | + delete parser; | |
41 | + } | |
42 | 42 | |
43 | 43 | XMLPlatformUtils::Terminate(); |
44 | 44 |
@@ -0,0 +1,8 @@ | ||
1 | +<?xml version="1.0" encoding="UTF-8"?> | |
2 | + | |
3 | +<root-element> | |
4 | + <element1>サンプル1</element1> | |
5 | + <element2> | |
6 | + <element3>サンプル3</element3> | |
7 | + </element2> | |
8 | +</root-element> |