You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用R从指定XML节点中提取link与id属性

Got it! Here's a straightforward way to pull the link and id attributes from your XML node using R, matching exactly the output format you're looking for:

Step 1: Prepare the XML and Load Required Package

First, we'll use the XML package (the go-to for XML processing in R). If you haven't installed it yet, run the install line first:

# Install and load the XML package
if (!require(XML)) {
  install.packages("XML")
  library(XML)
}

# Define your XML node as a string
xml_content <- '<item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="itemWithRetweets" link="http://twitter.com/MEDClementz/statuses/1001775473305817090" id="1001775473305817090" />'

Step 2: Parse XML and Extract Attributes

Next, we'll parse the XML string and use xmlGetAttr() to grab the specific attributes you need:

# Parse the XML content
parsed_xml <- xmlParse(xml_content)
root_node <- xmlRoot(parsed_xml)

# Extract link and id values
link_value <- xmlGetAttr(root_node, "link")
id_value <- xmlGetAttr(root_node, "id")

Step 3: Print in Desired Format

Finally, we'll format the output to match exactly what you requested:

# Output the results
cat(sprintf("link [1] %s\nid [1] %s", link_value, id_value))

Expected Output

When you run the code above, you'll get this output:

link [1] http://twitter.com/MEDClementz/statuses/1001775473305817090
id [1] 1001775473305817090

内容的提问来源于stack exchange,提问作者Yvonne Dong

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 08:46:23