2017-11-01 18 views
1

を抽出しますか?例えば:再帰的に私は、XML文書を持つXML属性

structure(
    list(
    ONE = structure(
     list(
     A = "", 
     B = structure(
      list(
      `1` = "", 
      `2` = "" 
      ), 
      .Names = c("1", "2") 
     ), 
     C = ""), 
     .Names = c("A", "B", "C") 
    ) 
), 
    .Names = "ONE" 
) 
## $ONE 
## $ONE$A 
## [1] "" 
## 
## $ONE$B 
## $ONE$B$`1` 
## [1] "" 
## 
## $ONE$B$`2` 
## [1] "" 
## 
## $ONE$C 
## [1] "" 

EDITS:私は動作しますが、不格好なようですその下にソリューションで到着した変更の目標出力

答えて

0

takeTheChildren <- function(x, search) { 
    # extracting the nth node (search) from the nodeset x 
    lapply(search, xml2::xml_child, x = x) 
} 

hierBuilder <- function(nodes) { 

    if (!requireNamespace("xml2", quietly = TRUE)) { 
    stop("`xml2` needed for this function to work. Please install it.", call. = FALSE) 
    } 

    # if we reach the leaf level of any of the node sets, 
    # just return an empty string 
    if (length(nodes) == 0L) { 
    return("") 
    } 

    # extract the names of each of the current top level nodes 
    names(nodes) <- sapply(nodes, xml2::xml_attr, attr = 'name') 

    # count the children each of the current top level node has, make a sequence 
    seq_ix <- lapply(nodes, function(node) { 
    seq(xml2::xml_children(node)) 
    }) 

    # make a list of individual child nodes under each of the current top level 
    # nodes, while preserving the hierarchy 
    children <- mapply(takeTheChildren, x = nodes, search = seq_ix, SIMPLIFY = FALSE) 

    # recurse on the current node's children 
    return(lapply(children, hierBuilder)) 
} 

迷惑な要件は、再帰が機能するために、我々はリストとして初期xml_docまたはxml_nodesetを通過しなければならないということである。

hierBuilder(list(ex)) 
## $ONE 
## $ONE$A 
## [1] "" 
## 
## $ONE$B 
## $ONE$B$`1` 
## [1] "" 
## 
## $ONE$B$`2` 
## [1] "" 
## 
## $ONE$C 
## [1] "" 
関連する問題