2017-11-01 11 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] "" 

편집 : 나는 작동하지만 어설픈 보이는 아래의 솔루션에 도착했습니다

답변

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] ""