User Tools

Site Tools


read_data_between_xml_tags

read data between XML tags

  • file: Node auslesen_BspTabelle.r
  • document page: 19


code:

#----open XML package-------------------------------------------------
library(XML)

#----read data between an XML tag-------------------------------------
filename = "D:/Heidi/Master/R_Daten/XML/Beispiel.xml"
tag = "//Fst"
doc = xmlTreeParse(filename, useInternal = TRUE)
ch = getNodeSet(doc, tag)

subDoc = xmlDoc(ch[[1]])
tagData <- xpathApply(subDoc, tag, xmlValue)
free(subDoc)
#print(tagData, indent=FALSE)

#----convert string data to a numeric matrix---------------------------
#----split string----
tagData2 <- as.character(tagData)

tagData3 <- strsplit(tagData2, "\n")  
tagMatrix <- as.matrix(as.data.frame(tagData3))
tagMatrix <- tagMatrix[2:nrow(tagMatrix)]

Data <- strsplit(tagMatrix, " ")
Matrix <- as.matrix(as.data.frame(Data))

#----to numeric matrix----
numericList <- as.numeric(Matrix[,1])
numericMatrix <- t(as.matrix(numericList))

for(n in 2:nrow(Matrix)){
  numericList <- as.numeric(Matrix[,n])
  numericMatrix <- rbind(numericMatrix, t(as.matrix(numericList)))
}
numericMatrix

# numericTable <- as.table(numericMatrix)     
# numericTable


XML file (Beispiel.xml):

<?xml version="1.0" encoding="iso-8859-1"?>
<uebung>
  <beispiel_1>
    <titel>pairwise_Fst</titel>
    <Fst>
1 2 3 4
1 2 3 4
5 6 7 8
5 6 7 8
    </Fst>
  </beispiel_1>
  <beispiel_2>
    <titel>mismatch</titel>
    <mismatch>
9 8 7 6
5 4 3 2
    </mismatch>
  </beispiel_2>
</uebung>


extracted data (with R):

> numericMatrix
     [,1] [,2] [,3] [,4]
[1,]    1    2    3    4
[2,]    1    2    3    4
[3,]    5    6    7    8
[4,]    5    6    7    8
read_data_between_xml_tags.txt · Last modified: 2008/07/22 13:31 by 127.0.0.1