检查可变数量的兄弟节点&不同的兄弟姐妹在Rapidxml [英] check for variable number of sibling nodes & different siblings in Rapidxml

查看:129
本文介绍了检查可变数量的兄弟节点&不同的兄弟姐妹在Rapidxml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在c ++中使用Rapidxml读取xml文件

I am using Rapidxml in c++ to read in a xml file

我有两个问题,基于以下示例

I have two questions based on the following example

<?xml version="1.0" encoding="utf-8"?>
<rootnode version="1.0" type="example">
  <childnode1 entry="1">
    <evendeepernode attr1="cat" attr2="dog"/>
    <evendeepernode attr1="lion" attr2="wolf"/>
  </childnode1>
  <childnode2 entry="1">
  </childnode2>
</rootnode>

1-如果相同类型的兄弟姐妹(evendeepernode)的数量是可变的。

1- if the number of same type of siblings(evendeepernode) is variable. How can I check for it?

2-如果有不同的兄弟姐妹(例如childnode1和childnode2),并且数量是可变的(例如,可以有多于1个childnode1和/或可以有多于1个childnode2的或其中一个可能不在那里)。如何检查?

2- if there are different siblings (e.g. childnode1 & childnode2 ) and there number is variable (e.g. there can be more than 1 childnode1's and/or there can be more than 1 childnode2's or one of them might not be there at all). how can I check for that?

推荐答案

OK我没有编译下面的代码,但它应该足够准确,你的需要。它应该至少说明您可以用于您的目的的方法和功能。

OK I have not compiled the code below but it should be accurate enough to modify it to your needs. It should at least illustrate the approach and the functions that you can use for your purposes. There may be better ways, but this will do what you need if you get no other replies.

对于像1-这样的问题,我使用类似于以下代码的代码:

For problems like 1- that you describe I use code similar to the following

xml_document<> doc;    
doc.parse<0>(xml_buffer);    // parse your string
xml_node<>* rootnode = doc.first_node("rootnode");  // Get first node
xml_node<>* childnode1 = rootnode->first_node("childnode1");     // childnode1

if (childnode1 != NULL) {
    // get first deeper node and loop over them all
    int number_of_siblings = 0;
    xml_node<>* deepernode = childnode1->first_node();
    while (deepernode != NULL) {
        // Do processing on this node

        // Your processing code here....

        // now get the next deepernode in this current level of nesting
        // pointer will be NULL when no more siblings 
        deepernode = deepernode->next_sibling();
        number_of_siblings++;
    }
    // Your xml had number_of_sibling nodes at this level
}

对于你的问题2-你可以使用同样的while循环遍历childnode1级别的兄弟节点。如果你需要检查兄弟姐妹的名字,你可以使用

For your question 2- You can use the same kind of while loop to loop through the sibling nodes at the childnode1 level. If you need to check the name of the sibling you can use

 string strNodeName = "childnode2";
 if (current_node->name() == strNodeName) {
     // current node is called childnode2 - do your processing.
 }

如果你不需要检查节点名称, rootnode下的子节点

if you dont need to examine node names, just use this to loop over all child nodes under rootnode

xml_document<> doc;    
doc.parse<0>(xml_buffer);    // parse your string
xml_node<>* rootnode = doc.first_node("rootnode");  // Get first node
xml_node<>* childnode = rootnode->first_node();     // get first childnode
int child_node_count = 0;
while (childnode != NULL) {
    // Do processing on this node

    // get sibling of current node (if there is one)    
    childnode = childnode->next_sibling();
    child_node_count++;
}
// child_node_count is now the number of child nodes under rootnode.

这篇关于检查可变数量的兄弟节点&amp;不同的兄弟姐妹在Rapidxml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆