-->

从,使用R xml文件(声明)提取数据仅(Extract data from xml file &#

2019-09-19 05:08发布

我试图从XML文件看起来像这样(见下文)的数据。 我需要提取ID内部节点节点其中type = 0。 我必须找到只有R染料溶液。 现在我可以通过xmlToList提取类型( “的test.xml”)[[3]] [[1]]和id由xmlToList( “的test.xml”)[[3]] [[4]]。 更改3〜6,9,等等 - 我可以检索所需的所有类型的和id的。 但我不知道这正确的,因为它是基于可以改变(在XML结构变化的情况下)编号。 请提出从XML提取数据的另一种更简单的方法? 或在非理想我的解决方案的任何修改? 谢谢!

<?xml version="1.0" encoding="UTF-8"?>
<image name="test1" id="367432589" width="952" height="1024" create_date="Mar 2, 2009" >
  <nodes>
    <node type="16" name="Target532" url="/cgi/im?id=5657" id="5657" x="67" y="45" width="153" height="69">
      <alt>Synthesis1</alt>
      <Appearance TextArea="Rectangle: 550"  Comlex="Boolean: true" />
    </node>
    <node type="0" name="Target1" url="/cgi/im?id=680" id="680" x="193" y="535" width="70" height="70">
      <alt>Object &lt;b&gt;Target1&lt;TestingCond32</alt>
      <Appearance TextArea="Rectangle: 210"  Comlex="Boolean: false" />
    </node>
  </nodes>
  <edges>
    <edge type="-100" id="234523">
      <alt />
      <Appearance Visualization="String: Hexa" HexagonIndex="Integer: 0" />
    </edge>
    <edge type="-100" id="23">
      <alt />
      <Appearance Visualization="String: Hexa" HexagonIndex="Integer: 0" />
    </edge>
  </edges>
</image>

我是新来的XML和有R的基本知识,谢谢!

Answer 1:

你可以尝试以下

xpathSApply(xdata,"//*/node[@type=\"0\"]/@id")

> xpathSApply(xdata,"//*/node[@type=\"0\"]/@id")
   id 
"680" 

这看起来对于一个名为“节点”与属性“类型”与值0然后返回与此节点关联标识的属性值节点



文章来源: Extract data from xml file (declaration) using R only
标签: xml r