-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProcessingXML.R
65 lines (52 loc) · 1.51 KB
/
ProcessingXML.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
##### Processing .xml file with XML package #####
##### Test #####
setwd("C:/Users/yumeng.zou/Google Drive/Freshyear/Summer/Research")
library(XML)
doc<-xmlTreeParse("test_cd.xml")
root<-xmlRoot(doc)
class(root)
names(root)
onecd<-root[[1]]
class(onecd)
onecd
xmlValue(onecd[["title"]])
prices<-xmlSApply(root, function(x)xmlValue(x[["price"]]))
## xmlValue only applies to one node
## xmlSApply applies to a loop of nodes
prices
getvar<-function(x,var)xmlValue(x[[var]])
names(root[[1]])
class(names(root[[1]]))
res = lapply(names(root[[1]]),function(var)xmlSApply(root,getvar,var))
## double loop
## var = names(root[[1]]) = "title", "artist", "country", "company", "price", "year"
cds=data.frame(res)
## if necessary, names(cds)<-names(root[[1]])
cds
##### Baise 1848 #####
setwd("C:/Users/yumeng.zou/Google Drive/Freshyear/Summer/Research")
library(xml2)
doc<-read_xml("BaiseYDHG1848stele.xml")
class(doc)
doc
xml_contents(doc) ## node set
text<-xml_contents(doc)[[3]] ## node
xml_path(text)
class(text)
xml_contents(text)[[1]]
body<-xml_contents(text)[[1]]
xml_path(body)
## namespace
xml_ns(doc)
xml_ns(text)
div<-xml_contents(body)
list<-xml_contents(div[[3]])
xml_path(list)
item<-xml_find_all(doc,"/*/*[3]/*/*[3]/*/*",xml_ns(doc))
## absolute location path
measure<-xml_find_all(item,"./*[2]/*",xml_ns(item))
## relative location path
quantity<-xml_find_all(measure,"./@quantity",xml_ns(measure))
## return a node set
xml_text(item)
xml_integer(quantity)