在 R 中编辑 XML 文件 [英] Editing XML files in R

查看:39
本文介绍了在 R 中编辑 XML 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含以下元素的 xml 文档:

<taxon id="test"></分类单元>ACAGTTGACACCCTT</序列>

并想解析taxon"标签内的新字符序列.我开始研究 XML 包文档,但还没有找到简单的解决方案.我的代码:

# 加载包要求(XML")# 创建一个新序列newSeq <- "TGTCAATGGAACCTG"#读取xmlsecondPartXml <- xmlTreeParse("generateSequences_secondPart.xml")

解决方案

您可以尝试使用 replaceNodes 并创建一个可能更易于使用的新节点或替换文本.

>

# 新节点名# invisible(replaceNodes(doc[["//sequence/text()"]], newXMLNode("new", newSeq)))# 仅新文本不可见(replaceNodes(doc[[//sequence/text()"]],newXMLTextNode(newSeq)))文档<?xml version="1.0"?><sequence id="ancestralSequence"><taxon id="test"></taxon>TGTCAATGGAACCTG</sequence>

I have an xml document with a following element:

<sequence id = "ancestralSequence"> 
    <taxon id="test">
     </taxon>       
    ACAGTTGACACCCTT
</sequence>

and would like to parse a new sequence of characters inside the "taxon" tags. I started looking into XML package documentation, but cannot find a simple solution yet. My code:

# load packages
require("XML")

# create a new sequence
newSeq <- "TGTCAATGGAACCTG"

# read the xml
secondPartXml <- xmlTreeParse("generateSequences_secondPart.xml")

解决方案

You could try to use replaceNodes and either create a new node which may be easier to work with or replace the text.

# new node name
# invisible(replaceNodes(doc[["//sequence/text()"]], newXMLNode("new", newSeq)))

# new text only
invisible(replaceNodes(doc[["//sequence/text()"]], newXMLTextNode( newSeq)))
doc

<?xml version="1.0"?>
<sequence id="ancestralSequence"><taxon id="test">
     </taxon>TGTCAATGGAACCTG</sequence>

这篇关于在 R 中编辑 XML 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆