在R中访问嵌入了多个层的XML数据

时间:2017-07-12 19:20:57

标签: r xml

我正在使用结构良好的XML文件。到目前为止,我已成功访问此数据集的元素,这些元素只是一个层/子域深。但是,现在我需要访问一种更深入嵌入此数据结构的数据类型,并且预期的方法不起作用...

摘自XML数据;这是"目标"我需要访问的字段,其中每个节点(即药物)可以具有0到N个目标(我现在任意将N设置为20,因为我不确定这个值对于整个数据集是什么):

<targets> --> 51st field in each node
    <target> --> there are a variable number of targets per drug
      <id>BE0000048</id> --> this is the value I want for each Target
      <name>Prothrombin</name>
      <organism>Human</organism>
      <actions>
        <action>inhibitor</action>
      </actions>
      <references>
        <articles>
          <article>
            <pubmed-id>10505536</pubmed-id>
            <citation>Turpie AG: Anticoagulants in acute coronary syndromes. 
    ...

我已经确定我需要的主要Target字段是每个节点结构中的字段51,因此下面是硬编码值。我认为在节点的目标字段中访问第j个目标中的第i个节点的id值应该具有[[i]] [[51]]的索引[[j]] [[1]]或[[i]] [[51]] [[j]] [[&#39; id&#39;]]:

这是我的代码没有按预期工作:

Target <- array(1:NumNodes, dim=c(1,NumNodes,MaxTargets))

for (i in 1:NumNodes){
   for (j in 1:MaxTargets){
      Target[i][j] <- Data[[i]][[51]][[j]][[1]]
   }
}

我看到的行为是,我可以在命令行上将下标扩展到多个级别,并且不会将结果缩小到以下范围:

> Data[[1]][[51]][[1]][[1]][[1]][[1]][[1]][[1]][[1]][[1]]
[1] "BE0000048ProthrombinHumaninhibitor10505536Turpie AG: Anticoagulants...

我添加的下标数量似乎并不重要; Target子字段中的所有字段总是连在一起,似乎不能分开......

令人困惑的是,当我运行我的代码时,收到以下错误消息:

Error in Data[[i]][[51]][[1]] : subscript out of bounds

...这似乎没有意义,因为我限制了我的节点数量,并且即使是上面显示的可疑的长列表,也没有抛出错误,当我查询命令行上的那个短语...

提前感谢您提供的任何见解。

1 个答案:

答案 0 :(得分:0)

感谢你的建议,cderv;我打算检查一下xml2包和XPATH。我非常感谢你愿意提供一个例子。

我正在粘贴应该是我的XML文件的功能子集;然而,现在代替“目标”领域是第51个领域,它是第六个。同样,它是目标 - &gt;目标 - &gt;我想为每个目标报告的id值,每个节点具有可变数量的目标值。我的代码遵循XML内容。

<?xml version="1.0" encoding="UTF-8"?>
<drugbank xmlns="http://www.drugbank.ca" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.drugbank.ca http://www.drugbank.ca/docs/drugbank.xsd" version="5.0" exported-on="2017-07-06">
<drug type="biotech" created="2005-06-13" updated="2016-08-17">
  <drugbank-id primary="true">DB00001</drugbank-id>
  <drugbank-id>BTD00024</drugbank-id>
  <drugbank-id>BIOD00024</drugbank-id>
  <name>Lepirudin</name>
  <description>Lepirudin is identical to natural hirudin except for substitution of leucine for isoleucine at the N-terminal end of the molecule and the absence of a sulfate group on the tyrosine at position 63. It is produced via yeast cells. Bayer ceased the production of lepirudin (Refludan) effective May 31, 2012.</description>
  <targets>
    <target>
      <id>BE0000048</id>
      <name>Prothrombin</name>
      <organism>Human</organism>
      <actions>
        <action>inhibitor</action>
      </actions>
      <references>
        <articles>
          <article>
            <pubmed-id>10505536</pubmed-id>
            <citation>Turpie AG: Anticoagulants in acute coronary syndromes. Am J Cardiol. 1999 Sep 2;84(5A):2M-6M.</citation>
          </article>
          <article>
            <pubmed-id>10912644</pubmed-id>
            <citation>Warkentin TE: Venous thromboembolism in heparin-induced thrombocytopenia. Curr Opin Pulm Med. 2000 Jul;6(4):343-51.</citation>
          </article>
        </articles>  
      </references>
      <known-action>yes</known-action>
    </target>
   </targets>   
</drug>
</drugbank>

现在我已经大大截断了上面的文件,我的代码现在给出一条错误消息,表明Data [[1]] [[1]]之上的任何下标都超出了界限,但希望这段代码可以让你了解我打算做什么...

library(XML)

# Save the database file as a tree structure
xmldata = xmlRoot(xmlTreeParse("DrugBank_TruncatedDatabase_v4_Tiny.xml"))

# Number of nodes in the entire database file
NumNodes <- xmlSize(xmldata)

MaxTargets <- 20

Data <- xmlSApply(xmldata, function(x) xmlSApply(x, xmlValue))

Target <- array(1:NumNodes, dim=c(1,NumNodes,MaxTargets))

for (i in 1:NumNodes){
   for (j in 1:MaxTargets){
      Target[i][j] <- Data[[i]][[5]][[j]][[1]]
   }
}

感谢您的投入!