【问题标题】:Parsing an XML document解析 XML 文档
【发布时间】:2016-12-27 20:29:38
【问题描述】:

我想用 c++ 解析 XML 文档,并能够识别特定标记中存在的文本。我检查了 TiyXML 和 PugiXML 之类的解析器,但它们似乎都没有单独识别标签。我怎样才能做到这一点?

【问题讨论】:

  • 编写一个 XML 解析器并不是一件容易的事。
  • 你试过什么没用?
  • 请详细说明任务,您希望达到什么目标?解析 xml 文件时,您需要知道具有所有可能属性的方案。可用的 xml 解析器中缺少什么?
  • 如果您展示一些示例 XML(带文本)并说出您想要提取的内容,这将非常有帮助。请注意,给定<tag1>abc<tag2>def</tag2>pqr</tag1>,所有解析器都将允许您获取“abc”和“pqr”。
  • 我希望得到 'tag1' 和 'tag2'。如何在 PugiXML 中完成?

标签: c++ xml parsing tags


【解决方案1】:

使用RapidXml,可以遍历节点和属性,识别其标签的文本。

#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.
        }
    }
}

【讨论】:

    【解决方案2】:

    获取所有带有pugixml的标签名称:

    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());
    

    【讨论】:

      猜你喜欢
      • 2020-07-10
      • 2016-05-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多