2016-12-27 7 views
-1

XML文書をC++で解析し、特定のタグにどのテキストが存在するかを特定できます。私はTiyXMLやPugiXMLのようなパーサーをチェックしましたが、それらのタグのどれもが別々に識別されていないようです。どうすればこれを達成できますか?XML文書の解析

+5

XMLパーサを書くには、簡単な作業ではありません。 – DeiDei

+2

これはうまくいかなかったのですが何を試しましたか? – Galik

+1

あなたは何をしたいのですか? xmlファイルを解析するときは、可能なすべての属性を持つスキームを知る必要があります。利用可能なXMLパーサーには何がないのですか? – paweldac

答えて

0

RapidXmlを使用すると、ノードと属性をトラバースしてタグのテキストを識別できます。 pugixmlを持つすべてのタグ名を取得するには

#include <iostream> 
#include <rapidxml.hpp> 
#include <rapidxml_utils.hpp> 
#include <rapidxml_iterators.hpp> 

int main() 
{ 
    using namespace rapidxml; 

    file<> in ("input.xml"); // Load the file in memory. 
    xml_document<> doc; 
    doc.parse<0>(in.data()); // Parse the file. 

    // Traversing the first-level elements. 
    for (node_iterator<> first=&doc, last=0; first!=last; ++first) 
    { 
     std::cout << first->name() << '\n'; // Write tag. 

     // Travesing the attributes of the element. 
     for (attribute_iterator<> attr_first=*first, attr_last=0; 
       attr_first!=attr_last; ++attr_first) 
     { 
      std::cout << attr_first->name() << '\n'; // Write tag. 
     } 
    } 
} 
0

void dumpTags(const pugi::xml_node& node) { 
    if (!node.empty()) { 
    std::cout << node.name() << std::endl; 
    for (pugi::xml_node child=node.first_child(); child; child=child.next_sibling()) 
     dumpTags(child); 
    } 
} 

pugi::xml_document doc; 
pugi::xml_parse_result result = doc.load("<tag1>abc<tag2>def</tag2>pqr</tag1>"); 
dumpTags(doc.first_child());