check for variable number of sibling nodes & different siblings in Rapidxml

痞子三分冷 提交于 2019-12-11 05:25:36

问题


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- if the number of same type of siblings(evendeepernode) is variable. How can I check for it?

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?


回答1:


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.

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
}

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

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.


来源:https://stackoverflow.com/questions/11783171/check-for-variable-number-of-sibling-nodes-different-siblings-in-rapidxml

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!