John Amraph
John Amraph

Reputation: 461

Extract data from xml file (declaration) using R only

I am trying to extract data from xml file that looks like this (see below). I need to extract id inside nodes for node where type=0. I have to find solution for R only. For now I can extract type by xmlToList("test.xml")[[3]][[1]] and id by xmlToList("test.xml")[[3]][[4]]. Changing 3 to 6,9,etc - I can retrieve all of needed type's and id's. But I am not sure that this correct because it is based on numbering that can be changed (in case of xml structure change). Can you please propose another more simple way of extracting data from xml ? or any modifications on my nonideal solution? Thank's!

<?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>

I am new to xml and have basic knowledge of R. Thank you!

Upvotes: 3

Views: 2154

Answers (1)

shhhhimhuntingrabbits
shhhhimhuntingrabbits

Reputation: 7475

You can try the following

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

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

This looks for a node that is named "node" with attribute "type" with a value of 0. It then returns the attribute value of id associated with this node

Upvotes: 2

Related Questions