How to Read a Tif.aux.xml File Into R

R - XML Files


XML is a file format which shares both the file format and the data on the World wide web, intranets, and elsewhere using standard ASCII text. Information technology stands for Extensible Markup Language (XML). Similar to HTML it contains markup tags. But unlike HTML where the markup tag describes structure of the page, in xml the markup tags describe the meaning of the data contained into he file.

You can read a xml file in R using the "XML" package. This bundle can be installed using following command.

install.packages("XML")        

Input Data

Create a XMl file by copying the below data into a text editor like notepad. Relieve the file with a .xml extension and choosing the file type every bit all files(*.*).

<RECORDS>    <EMPLOYEE>       <ID>i</ID>       <Name>Rick</Name>       <Salary>623.iii</SALARY>       <STARTDATE>one/1/2012</STARTDATE>       <DEPT>IT</DEPT>    </EMPLOYEE> 	    <EMPLOYEE>       <ID>2</ID>       <Proper noun>Dan</Proper noun>       <SALARY>515.2</SALARY>       <STARTDATE>9/23/2013</STARTDATE>       <DEPT>Operations</DEPT>    </EMPLOYEE>        <EMPLOYEE>       <ID>iii</ID>       <Name>Michelle</NAME>       <Salary>611</SALARY>       <STARTDATE>eleven/15/2014</STARTDATE>       <DEPT>Information technology</DEPT>    </EMPLOYEE>        <EMPLOYEE>       <ID>iv</ID>       <Name>Ryan</NAME>       <SALARY>729</Bacon>       <STARTDATE>5/11/2014</STARTDATE>       <DEPT>HR</DEPT>    </EMPLOYEE>        <EMPLOYEE>       <ID>5</ID>       <Proper name>Gary</Name>       <SALARY>843.25</Salary>       <STARTDATE>three/27/2015</STARTDATE>       <DEPT>Finance</DEPT>    </EMPLOYEE>        <EMPLOYEE>       <ID>half dozen</ID>       <NAME>Nina</NAME>       <SALARY>578</Bacon>       <STARTDATE>v/21/2013</STARTDATE>       <DEPT>Information technology</DEPT>    </EMPLOYEE>        <EMPLOYEE>       <ID>vii</ID>       <NAME>Simon</NAME>       <Salary>632.8</Bacon>       <STARTDATE>7/30/2013</STARTDATE>       <DEPT>Operations</DEPT>    </EMPLOYEE>        <EMPLOYEE>       <ID>8</ID>       <Proper name>Guru</NAME>       <Bacon>722.v</Bacon>       <STARTDATE>6/17/2014</STARTDATE>       <DEPT>Finance</DEPT>    </EMPLOYEE> 	 </RECORDS>        

Reading XML File

The xml file is read by R using the part xmlParse(). It is stored equally a listing in R.

# Load the parcel required to read XML files. library("XML")  # Also load the other required package. library("methods")  # Give the input file proper noun to the role. upshot <- xmlParse(file = "input.xml")  # Print the result. print(result)        

When we execute the above code, it produces the following effect −

one Rick 623.3 1/1/2012 It  2 Dan 515.two 9/23/2013 Operations  3 Michelle 611 11/fifteen/2014 IT  iv Ryan 729 5/11/2014 60 minutes  5 Gary 843.25 three/27/2015 Finance  6 Nina 578 v/21/2013 It  7 Simon 632.8 vii/thirty/2013 Operations  eight Guru 722.5 6/17/2014 Finance        

Go Number of Nodes Present in XML File

# Load the packages required to read XML files. library("XML") library("methods")  # Give the input file proper name to the part. event <- xmlParse(file = "input.xml")  # Exract the root node form the xml file. rootnode <- xmlRoot(result)  # Notice number of nodes in the root. rootsize <- xmlSize(rootnode)  # Print the effect. print(rootsize)        

When we execute the above lawmaking, it produces the following event −

output [one] 8        

Details of the First Node

Permit's look at the get-go record of the parsed file. It will give us an idea of the diverse elements present in the top level node.

# Load the packages required to read XML files. library("XML") library("methods")  # Give the input file name to the function. result <- xmlParse(file = "input.xml")  # Exract the root node form the xml file. rootnode <- xmlRoot(result)  # Print the event. print(rootnode[1])        

When nosotros execute the above lawmaking, information technology produces the post-obit result −

$EMPLOYEE    1    Rick    623.3    one/1/2012    Information technology    attr(,"class") [one] "XMLInternalNodeList" "XMLNodeList"        

Get Different Elements of a Node

# Load the packages required to read XML files. library("XML") library("methods")  # Give the input file name to the function. issue <- xmlParse(file = "input.xml")  # Exract the root node course the xml file. rootnode <- xmlRoot(issue)  # Get the start element of the starting time node. impress(rootnode[[1]][[1]])  # Get the fifth element of the first node. print(rootnode[[1]][[v]])  # Get the 2d element of the tertiary node. print(rootnode[[3]][[ii]])        

When we execute the above code, it produces the post-obit result −

1  It  Michelle        

XML to Data Frame

To handle the data effectively in large files we read the data in the xml file equally a data frame. Then process the data frame for data analysis.

# Load the packages required to read XML files. library("XML") library("methods")  # Catechumen the input xml file to a information frame. xmldataframe <- xmlToDataFrame("input.xml") impress(xmldataframe)        

When we execute the above code, it produces the post-obit consequence −

          ID    Name     SALARY    STARTDATE       DEPT  one      1    Rick     623.30    2012-01-01      It ii      ii    Dan      515.xx    2013-09-23      Operations 3      3    Michelle 611.00    2014-11-15      IT 4      four    Ryan     729.00    2014-05-11      60 minutes 5     NA    Gary     843.25    2015-03-27      Finance half dozen      6    Nina     578.00    2013-05-21      IT vii      7    Simon    632.80    2013-07-xxx      Operations 8      8    Guru     722.50    2014-06-17      Finance        

As the information is now available equally a dataframe nosotros can utilize data frame related function to read and manipulate the file.

Useful Video Courses


JCL Online Training

Video

DB2 Online Training

Video

COBOL Online Training

Video

Email Marketing Online Training

Video

Mainframe Online Training

Video

CRO Online Training

Video

looneyimen1962.blogspot.com

Source: https://www.tutorialspoint.com/r/r_xml_files.htm

0 Response to "How to Read a Tif.aux.xml File Into R"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel