/** * 找到所有子小题 */ function searchSubNode(src_arr, p_node) { var ret_arr = [p_node.getContent()] var sub_arr = []; for (var i = 0; i < src_arr.length; i++) { var node_i = src_arr[i]; if ("" === node_i) { continue; } var has_p = node_i.hasP(); if (has_p === p_node.getVSubName()) { // 如果has_p属性和父亲节点的vsubname节点值相等 src_arr.splice(i, 1, "");// 删除第i个元素 var has_sub = node_i.hasSub(); if ("no" === has_sub) { var content = node_i.getContent(); sub_arr.push(content); } else { sub_arr = searchSubNode(src_arr, node_i); } } } ret_arr.push(sub_arr); return ret_arr; }
以上是递归求出所有的子小题
以下是示例结果集:
[
"<vsubcontent value=\"no\"><vsubname>a</vsubname><vsubvalue>1</vsubvalue></vsubcontent>",
"<vsubcontent value=\"no\"><vsubname>b</vsubname><vsubvalue>2</vsubvalue></vsubcontent>",
[
"<vsubcontent value=\"yes\"><vsubname>hh</vsubname><vsubvalue>3</vsubvalue></vsubcontent>",
[
"<vsubcontent value=\"yes\"><vsubname value=\"hh\">aa</vsubname><vsubvalue>3.1</vsubvalue></vsubcontent>",
[
"<vsubcontent value=\"no\"><vsubname value=\"aa\">aaa</vsubname><vsubvalue>3.1.1</vsubvalue></vsubcontent>",
"<vsubcontent value=\"no\"><vsubname value=\"aa\">bbb</vsubname><vsubvalue>3.1.2</vsubvalue></vsubcontent>"
],
"<vsubcontent value=\"no\"><vsubname value=\"hh\">bb</vsubname><vsubvalue>3.2</vsubvalue></vsubcontent>"
],
]
]
哎,我得学学算法了!