Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用RJSDMX R从API获取XML数据_Java_R_Xml_Api - Fatal编程技术网

Java 使用RJSDMX R从API获取XML数据

Java 使用RJSDMX R从API获取XML数据,java,r,xml,api,Java,R,Xml,Api,我正在尝试将XML数据导入到R中。 具体而言,世界综合贸易解决方案(WITS)旨在获得所有国家之间的双边贸易水平。 在WITS网站上,它说连接到他们API的官方软件包是RJSDMX,但我下载数据时遇到了困难 包使用示例: WITS API文档: 这是我目前的代码: library(RJSDMX) org <- "WITS" # Get trade flows using the RJSDMX package # https://rstudio-pubs-stati

我正在尝试将XML数据导入到R中。 具体而言,世界综合贸易解决方案(WITS)旨在获得所有国家之间的双边贸易水平。 在WITS网站上,它说连接到他们API的官方软件包是
RJSDMX
,但我下载数据时遇到了困难

包使用示例:

WITS API文档:

这是我目前的代码:

library(RJSDMX)
org <- "WITS"

# Get trade flows using the RJSDMX package
# https://rstudio-pubs-static.s3.amazonaws.com/593878_b305636e73314eba87615097421034b3.html
flow <- getFlows("WITS")
flow$`WBG_WITS,DF_WITS_TradeStats_Trade,1.0`

#parse "WITS" dataflow
fas_dataflow <- strsplit(names(flow[3]),",")
idx <- 1
#if Case 2 found, then get the second element
ifelse(length(fas_dataflow[[1]]) > 1, idx <-2, idx <-1)
fas_dataflow <- fas_dataflow[[1]][idx]

#get list of dimensions of "Average duration of unemployment" DSD
fas_dimensions <- getDimensions(org, fas_dataflow)

#use flatten to remove first level of indices
fas_codelist <- map(.x = names(flatten(fas_dimensions)),
                    flow = fas_dataflow,
                    provider = org,
                    .f = getCodes) %>% set_names(names(flatten(fas_dimensions)))
getCodes(org, fas_dataflow, "FREQ")
getCodes(org, fas_dataflow, "REPORTER")
getCodes(org, fas_dataflow, "PARTNER")
getCodes(org, fas_dataflow, "PRODUCTCODE")
getCodes(org, fas_dataflow, "INDICATOR")
# this doesn't work    
emoney_trx_id <- as_tibble(sdmxdf(getTimeSeries(org,"A.MPRT-PRTNR-SHR")))
# neither does this
emoney_trx_id <- as_tibble(sdmxdf(getTimeSeries(org,"A....MPRT-PRTNR-SHR")))
# and neither does this
emoney_trx_id <- as_tibble(sdmxdf(getTimeSeries(org,"A....MPRT-PRTNR-SHR.reported")))
# or this
emoney_trx_id <- as_tibble(sdmxdf(getTimeSeries(org,"A.MPRT-PRTNR-SHR.reported")))
库(RJSDMX)

orgEDIT:我可以通过API文档页面上的URL直接获得WITS双边贸易流。另见

生成的代码如下所示:

# Set link to website
link1 <-"http://wits.worldbank.org/API/V1/SDMX/V21/datasource/tradestats-trade/reporter/all/year/2018/partner/all/indicator/MPRT-PRTNR-SHR"

# Get data from webpage
data_prices <- getURL(link1)

# Parse XML data
xmlfile <- xmlParse(data_prices)

# Get place nodes
places <- getNodeSet(xmlfile, "//Series")


# Get values for each place
values <- lapply(places, function(x){
  # Get current place id
  pid <- xmlAttrs(x)
  
  # Get values for each gas type for current place
  newrows <- lapply(xmlChildren(x), function(y){
    # Get type and update time values
    attrs <- xmlAttrs(y)
    
    # Get price value
    price <- xmlValue(y)
    names(price) <- "price"
    
    # Return values
    return(c(pid, attrs, price))
  })
  # Combine rows to single list
  newrows <- do.call(rbind, newrows)
  
  # Return rows
  return(newrows)
})

# Combine all values into a single dataframe
df <- as.data.frame(do.call(rbind, values), stringsAsFactors = FALSE)
#设置网站链接

链接1完成!对不起!现在这成为一个有用的资源。恭喜你解决了自己的问题。