gpt4 book ai didi

c++ - 标准化 XML 文件标签命名,然后处理值

转载 作者:行者123 更新时间:2023-11-28 06:18:38 28 4
gpt4 key购买 nike

我正在处理很多 XML 文件,每个文件都有自己的命名约定:

文件a.xml

<entry>
<id>1</id>
<information>Testing</information>
<url>www.google.com</url>
</entry>
...

文件b.xml

<entry>
<id>1</id>
<content>Testing</content>
<website>www.google.com</website>
</entry>
...

文件c.xml

<entry>
<id>1</id>
<data>Testing</data>
<url>www.google.com</url>
</entry>
...

在我的代码中,我使用映射来定义什么标签包含什么信息,例如,对于 Fileb.xml:

const std::map<int,std::string> tagMap {
{"id","id"},
{"description","content"}
};

然后,当我从我的文件中获取一个 XML 节点时,我循环遍历我的标签映射,它查找指定的标签名称并将它们分配给它们所在的内容类型。

for (auto& kv : tagMap) {
// This aims to grab the content of the file tag and names it correctly based on it's content
kv.first=eb.second.child_value(kv.second.c_str());
}

根据我找到的标签,即“描述”,我想对其进行处理。正确的方法是什么,我是否应该为我想处理的每个标签设置一个 if 命令,比如这样?

if (kv.first="id") {
//Do something on the ID, i.e check if it is unique
}

if (kv.first="description") {
// DO something on the description, i.e trim, validate
}

我有点担心我对此采取了错误的方法:

  1. 当标签符合我的命名约定时,我似乎定义了标签{"id","id"}

  2. 由于在实际实现中我会从大约 10 个 XML 标签中抓取内容,因此对每个要抓取的标签进行循环似乎是错误的。

最佳答案

你的 if-approach 很好(尽管你可以使用 dispatch)但是你错误地使用了 map (循环而不是搜索)

const std::map<int,std::string> tagMap {
{"description","content"}
// ...
// no id -> id
};
string tagName = ... // get this from xml
if (tagMap.count(tagName))
tagName = tagMap[tagName];

if (tagName == "id") { ... }
else if (tagName == "content") { ... }

关于c++ - 标准化 XML 文件标签命名,然后处理值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29744211/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com