Pugixml - parse namespace with prefix mapping and without prefix mappig(Pugixml - 使用前缀映射和没有前缀 mappig 解析命名空间)
问题描述
我有一个客户端应用程序,它解析从 2 个不同服务器发送的 xml 响应.我称它们为服务器 A 和服务器 B.
I have a client application that parses xml responses that are sent from 2 different servers. I call them server A and server B.
服务器 A 响应其中一个请求,响应如下:
Server A responds to one of the request with a response as below:
<?xml version="1.0" encoding="UTF-8"?>
<D:multistatus xmlns:D="DAV:">
<D:response>
<D:href>/T12.txt</D:href>
<D:propstat>
<D:prop>
<local-modification-time xmlns="urn:abc.com:webdrive">1389692809</local-modification-time>
</D:prop>
<D:status>HTTP/1.1 200 OK</D:status>
</D:propstat>
</D:response>
</D:multistatus>
服务器 B 响应其中一个请求,响应如下:
Server B responds to one of the request with a response as below:
<?xml version="1.0" encoding="UTF-8"?>
<D:multistatus xmlns:D="DAV:">
<D:response>
<D:href>/T12.txt</D:href>
<D:propstat>
<D:prop>
<O:local-modification-time xmlns:O="urn:abc.com:webdrive">1389692809</O:local-modification-time>
</D:prop>
<D:status>HTTP/1.1 200 OK</D:status>
</D:propstat>
</D:response>
</D:multistatus>
如果您观察两个服务器之间的差异,serverA 不会发送命名空间和前缀之间的映射,但 serverB 会发送(查看 local-modification-time 标记).如何编写通用客户端解析逻辑,以通用处理这两种情况.任何示例代码都会有很大帮助.
If you observer the difference between the two servers, serverA does not send a mapping between namespace and prefix, but serverB does (look at local-modification-time tag). How can I write a generic client parsing logic, to handle both these scenarios generically. Any sample code would be of great help.
谢谢,-桑迪普
推荐答案
我认为最好的办法是忽略命名空间并通过本地名称查找节点.您可以像这样使用 XPath:
I think your best bet is to just ignore namespaces and find the node by local name. You can do it with XPath like this:
node.select_single_node(".[local-name()='local-modification-time']")
或者你可以像这样使用 C++:
Or you can just use C++ like this:
pugi::xml_node child_by_local_name(pugi::xml_node node, const char* name)
{
for (pugi::xml_node child = node.first_child(); child; child = child.next_sibling())
{
if (strcmp(child.name(), name) == 0)
return child;
const char* colon = strchr(child.name(), ':');
if (colon && strcmp(colon + 1, name) == 0)
return child;
}
return pugi::xml_node();
}
这篇关于Pugixml - 使用前缀映射和没有前缀 mappig 解析命名空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Pugixml - 使用前缀映射和没有前缀 mappig 解析命名空间


- C++ 协变模板 2021-01-01
- 近似搜索的工作原理 2021-01-01
- STL 中有 dereference_iterator 吗? 2022-01-01
- 静态初始化顺序失败 2022-01-01
- 一起使用 MPI 和 OpenCV 时出现分段错误 2022-01-01
- 从python回调到c++的选项 2022-11-16
- 如何对自定义类的向量使用std::find()? 2022-11-07
- 与 int by int 相比,为什么执行 float by float 矩阵乘法更快? 2021-01-01
- 使用/clr 时出现 LNK2022 错误 2022-01-01
- Stroustrup 的 Simple_window.h 2022-01-01