如何在C++中实现通用DOM数据结构?

Riz*_*izo 10 c++ templates dom

我正在尝试编写一个非常简单的文档对象模型库实现,以便提供在我的其他项目中使用的通用数据结构.只是为了让事情变得简单,我定义只有三大类:node,elementattribute.节点由它的名称定义(例如所有html标签),并且基本上是元素的容器,可以是文本和子节点(存储在a中std::vector<node>).

我只是无法弄清楚如何定义整个树结构.

我需要为我所提到的分类的模板化接口.

用法示例:

element<string> txt1("Some text");

element< element<string> > div1("div", txt1);
Run Code Online (Sandbox Code Playgroud)

我不想在完全支持XML的情况下创建完整的DOM抽象级别.我只需要想法以类似DOM的方式组织信息.无需解析.

提前致谢!

Mer*_*ham 5

不要尝试根据每个节点有多少个父节点来强类型化,而是将代码组织为树结构:

class Element
{
public:
  std::string Name;
  std::map<std::string, std::string, std::less<std::string> > Attributes;
  std::list<Element> Children;
};
Run Code Online (Sandbox Code Playgroud)

您的公共界面可能看起来与此有很大不同。我只是想展示一般的类型布局。

您实际上并不需要节点或属性功能,除非您需要在集合中与元素一起迭代它们。对于 XML DOM 库来说,这是一个有用的功能,但如果您只是想创建一个数据结构,则不必严格遵循 DOM 设计。

事实上,如果您只是想要一个通用的数据结构,您可能只需要一个属性包:

#include<map>
#include<string>
#include<iostream>

class PropertyBag;
typedef std::map<std::string, PropertyBag> PropertyMap;

class PropertyBag : public PropertyMap
{
public:
  PropertyBag(const std::string& value)
    : value(value)
  {
  }

  PropertyBag& operator=(const std::string& value)
  {
    this->value = value;
    return *this;
  }

  operator std::string& () { return value; }

private:
  std::string value;

  friend PropertyMap::mapped_type& PropertyMap::operator[](const PropertyMap::key_type&);
  PropertyBag() { }
};

void SomeFunction(const std::string& value)
{
  std::cout << value << "\n";
}

int main(int argc, char* argv[])
{
  PropertyBag config("configuration root");
  config["child1"] = "value1";
  config["child1"]["subchild1"] = "value2";

  SomeFunction(config["child1"]);
  SomeFunction(config["child1"]["subchild1"]);
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

只是谈论语法,您还可以尝试使用重载operator()和/或链接方法来变得棘手:

PropertyBag& SomeMethod(const std::string& someParam)
{
  // do something here...
  return *this;
}

PropertyBag& operator()(const std::string& p1, const std::string& p2)
{
  // ...
  return *this;
}

// ...

Configuration config1("root")
  .SomeMethod("p1")
  .SomeMethod("p2");
Configuration config2("root")
  ("Something", "blah")
  ("sizzle", "V2");
Run Code Online (Sandbox Code Playgroud)

我想文本/代码重复越少越好。您的代码越接近 JSON 或 YAML 等语法就越好。

一旦 c++0x 发布,您可能会有更简单的选择。您还可以查看boost::assign 库,以获取在数据结构上使用的简单初始化语法。

您还可以查看boost::any 库以获取可用作值的数据类型,而不是字符串(支持插入任何值的类型安全方法,只要将其提取为相同类型即可)。