Poco::XML::DOMParser を紹介します。
さらに、Poco::XML::NodeIterator, Poco::XML::NodeFilter, Poco::XML::NamedNodeMap も紹介します。
DOMParserTest.cpp
・Poco::XML::DOMParser をインスタンス化し、
setFeature(Poco::XML::DOMParser::FEATURE_WHITESPACE, false)
で空白文字だけのエレメントを無視するように指定。
・parseString() で、const な文字列を読み込み。
・読み込んだ文字列の Poco::XML::Document を Poco::XML::NodeIterator でイテレートして各ノードの
名前とバリューを表示し、attribute が存在すればそれも表示。
#include <Poco/DOM/DOMParser.h> #include <Poco/DOM/Document.h> #include <Poco/DOM/NodeIterator.h> #include <Poco/DOM/NodeFilter.h> #include <Poco/DOM/AutoPtr.h> #include <Poco/DOM/NamedNodeMap.h> #include <Poco/Exception.h> #ifndef POCO_VERSION #include <Poco/Version.h> #endif #include <string> #include "ScopedLogMessage.h" #include "PrepareConsoleLogger.h" const std::string kXMLString = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n" "<html xmlns=\"http://www.w3.org/1999/xhtml\">\n" " <head>\n" " <title>Sample HTML</title>\n" " <meta content=\"text/html; charset=utf-8\" http-equiv=\"content-type\"/>\n" " <link href=\"css/styles.css\" rel=\"stylesheet\" type=\"text/css\"/>\n" " </head>\n" " <body>This is a sample.</body>\n" "</html>\n"; void TestDOMParser(ScopedLogMessage& msg, const std::string& xml) { msg.Message("--- original xml ---\n" + xml); try { Poco::XML::DOMParser parser; #if (POCO_VERSION < 0x01040000) parser.setFeature(Poco::XML::DOMParser::FEATURE_WHITESPACE, false); #else parser.setFeature(Poco::XML::DOMParser::FEATURE_FILTER_WHITESPACE, false); #endif Poco::XML::AutoPtr<Poco::XML::Document> pDoc = parser.parseString(xml); msg.Message("--- parsed ---"); Poco::XML::NodeIterator itr(pDoc, Poco::XML::NodeFilter::SHOW_ALL); Poco::XML::Node* pNode = itr.nextNode(); while(pNode) { std::string leading; Poco::XML::Node* pParentNode = pNode->parentNode(); while(0 != pParentNode) { pParentNode = pParentNode->parentNode(); leading += " "; } msg.Message(leading + pNode->nodeName() + ":" + pNode->nodeValue()); if(pNode->hasAttributes()) { for(unsigned long i=0; i<pNode->attributes()->length(); ++i) { msg.Message( leading + " " + pNode->attributes()->item(i)->nodeName() + "=\"" + pNode->attributes()->item(i)->nodeValue() + "\""); } } pNode = itr.nextNode(); } } catch(Poco::Exception& exc) { msg.Message(exc.displayText()); } } int main(int /*argc*/, char** /*argv*/) { PrepareConsoleLogger logger(Poco::Logger::ROOT, Poco::Message::PRIO_INFORMATION); ScopedLogMessage msg("DOMParserTest ", "start", "end"); TestDOMParser(msg, kXMLString); return 0; } |
Results of execution
・#document と #text は、それぞれ Poco::XML::Document と Poco::XML::Text がデフォルトで持って
いる名称。
[0] DOMParserTest start [0] --- original xml --- <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Sample HTML</title> <meta content="text/html; charset=utf-8" http-equiv="content-type"/> <link href="css/styles.css" rel="stylesheet" type="text/css"/> </head> <body>This is a sample.</body> </html> [0] --- parsed --- [0] #document: [0] html: [0] head: [0] title: [0] #text:Sample HTML [0] meta: [0] content="text/html; charset=utf-8" [0] http-equiv="content-type" [0] link: [0] href="css/styles.css" [0] rel="stylesheet" [0] type="text/css" [0] body: [0] #text:This is a sample. [0] DOMParserTest end |
Downloads
・ここをクリックすると、makefile や VC++ プロジェクトなど一式がダウンロードできます。
(2013.05.31 updated)
・2010年8月6日からのダウンロード数:1342
Subversion
・フリーの Subversion ホスティングサービス Assemblaで、ソースコードを管理しています。
![]() |
Copyright © 2010 Round Square Inc. All rights reserved. |
---|
0 Comments.