R language XML format data import and processing

Source: Internet
Author: User

Data parsing

XML is an Extensible markup language designed to transmit and store data. XML is the most common tool for data transfer between various applications. Unlike databases such as access,oracle and SQL Server, the database provides more powerful data storage and analysis capabilities, such as data indexing, sorting, lookups, correlation consistency, and so on, which simply stores data . In fact it's the biggest difference from other data representations: it's extremely simple, it's a seemingly trivial advantage, but it makes it different.

For XML format data, the R language XML package can be used for data import and processing, see the following case description.

Case 1

Enter a markup language text directly, using XML packet parsing.

library (XML) TT = ' <x><a>text< /a><b foo=  "1"/><c  Bar= "Me" ><d>a Phrase</d></c>< Span class= "Hljs-tag" ></x> ' doc = Xmlparse (TT) xmltolist (DOC) # Use an R-level node Representationdoc = Xmltreeparse (TT) xmltolist (DOC)          
Case 2

Import the existing XML format data and processing, this case uses the phone address Book XML data, as follows:

#读取xml格式数据并解析xmlfile =xmlparse (File.choose (), encoding= "UTF-8") Class (XMLFile)#形成根目录列表数据xmltop = XmlRoot (XMLFile) class (Xmltop) #查看类xmlName (xmltop) #查看根目录名xmlSize (xmltop) #查看根目录总数xmlName (xmltop[ [1]]) #查看子目录名# view First subdirectory xmltop[[1]]# See Second subdirectory xmltop[[2]]#子目录节点xmlSize (Xmltop[[1]]) #子目录节点数xmlSApply (xmltop[[1]], XMLName) #子目录节点名xmlSApply (Xmltop[[1]], xmlattrs) # Subdirectory Node Properties xmlsapply (Xmltop[[1]], xmlsize) #子目录节点大小#查看第一个子目录的第一个节点xmltop [[1]] [[1]] #查看第一个子目录的第二个节点xmltop [[1]][[2]]  #第二个子目录xmltop [[2]][[1]]xmltop[[ 2]][[2]]xmltop[[1]][[3]][[1]][[1 ]] #查看联系人电话xmltop [[' contact ']][[' phonelist ']][[1]][[1]" #第二种方式getNodeSet (xmltop, "//contact/phonelist") [[1]][[1]][[1]] #第三种方式xmltop [[1]][[3]][[1]][[1]] = 13717232323 #更改联系人电话xmltop [[1]][[1] ][[1]]= "Zhangsan" #更改联系人姓名  #保存saveXML (Xmltop, file= "Out.xml", encoding= "UTF-8")   
XML Format goto Dataframe

To do this, follow these steps:

#第一种方式,直接用xmlToDataFrame()函数library("plyr") #第二种方式,数据格式处理专用包plyrMyContact=ldply(xmlToList(file.choose()), data.frame) #先转成list,再转dataframeView(MyContact)# 查看联系方式MyContact[,c("Name" ,"PhoneList.Phone.text")]# 联系方式保存write.csv(MyContact, "MyContact.csv", row.names=FALSE)

R language XML format data import and processing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.