Showing posts with label xmlTreeParse. Show all posts
Showing posts with label xmlTreeParse. Show all posts

Monday, July 4, 2016

How can I parse an XML file in one of my folders in R programming?

In the previous post, the file was placed on the local internet server and a URL reference was given. It does not have to be that way. The file can be in the folders/files and you just need to change the code a little bit as shown. I am using the same file I used in the previous post.

These are the three lines needed to parse the XML file in the following directory:
------------------
> library(XML)
> fileName="C:\\Users\\Jayaram\\Desktop\\SQLServer2016D\\R Server\\Mystudents.xml"
> xmlFile <- filename="" font="" readlines="" xmltreeparse="">
-------------

And this produces the same result as in the previous post.

---------
$doc
$file
[1] ""

$version
[1] "1.0"

$children
$children$root

 
  Linda Jones
  Access, VB5.0
 

 
  Adam Davidson
  Cobol, Mainframe
 

 
  Charles Boyer
  HTML, Photoshop
 

 
  Charles Amos
  Cobol, Mainframe
 

-------------------
Follow the warnings in the previous post. Also make sure that the file name is typed in as shown in the post (note the double slashes).

Friday, July 1, 2016

How to parse a XML document in R?

This post shows how to parse a XML document in R. I often use the following simple xml file for my posts and examples. Please find some precautions at the end of this post.


MyStudentsXMLDoc

If your xml doccument is not on the local server, you can easily place it on the server by copying and pasting the xml document(file) to the local server root (inetpub/wwwroot).

The steps to parse use the xml function xmlTreeParse as shown by the following:
The program uses the readLines() function to read the document as shown and you code these after launching R Studio or R Gui.


MyStudnetsXMLDoc_2

You immediately get the following response as shown here:

$doc
$file
[1] ""

$version
[1

$children
$children$wclass

 

  Linda Jones
  Access, VB5.0
 

 
  Adam Davidson
  Cobol, Mainframe
 

 
  Charles Boyer
  HTML, Photoshop
 

 
  Charles Amos
  Cobol, Mainframe
 


attr(,"class")
[1] "XMLDocumentContent"

$dtd
$external
NULL

$internal
NULL

attr(,"class")
[1] "DTDList"


1. R programs are case sensitive and pay attention to how they are typed-in
2. The Mystudents.xml file should have a final carriage return. If not you will end up with an error:
 incomplete final line found on 'http://localhost/Mystudents.xml'