R언어

XML 데이터 R으로 받아오기

carrot62 2021. 1. 11. 16:04

출처: gomguard.tistory.com/259

 

XML 구조 및 정의, parsing in r , xml to dataframe

XML 정의 XML 은 소프트웨어나 하드웨어에 관계없이 데이터를 저장하거나 전송하기위한 도구, 마크업 언어 입니다. 일반적으로 데이터를 구조화해서 다른 프로그래밍 언어간 전송할 때 자주 사용

gomguard.tistory.com

xml 파일 예시 (출처 w3chools XML Example Version 2)

www.w3schools.com/xml/

 

XML Tutorial

XML Tutorial XML stands for eXtensible Markup Language. XML was designed to store and transport data. XML was designed to be both human- and machine-readable. XML Example 2     Belgian

www.w3schools.com

 

<?xml version="1.0" encoding="UTF-8"?>
<breakfast_menu>
<food>
    <name>Belgian Waffles</name>
    <price>$5.95</price>
    <description>
   Two of our famous Belgian Waffles with plenty of real maple syrup
   </description>
    <calories>650</calories>
</food>
<food>
    <name>Strawberry Belgian Waffles</name>
    <price>$7.95</price>
    <description>
    Light Belgian waffles covered with strawberries and whipped cream
    </description>
    <calories>900</calories>
</food>
<food>
    <name>Berry-Berry Belgian Waffles</name>
    <price>$8.95</price>
    <description>
    Belgian waffles covered with assorted fresh berries and whipped cream
    </description>
    <calories>900</calories>
</food>
<food>
    <name>French Toast</name>
    <price>$4.50</price>
    <description>
    Thick slices made from our homemade sourdough bread
    </description>
    <calories>600</calories>
</food>
<food>
    <name>Homestyle Breakfast</name>
    <price>$6.95</price>
    <description>
    Two eggs, bacon or sausage, toast, and our ever-popular hash browns
    </description>
    <calories>950</calories>
</food>
</breakfast_menu>
file_1 <- DocFromXML<-xmlTreeParse("example1.xml",useInternal=T)
file_2<-xmlRoot(file_1)
file_2
file_3 <- xpathSApply(file_2,"//name",xmlValue)
file_3
file_4 <- xpathSApply(file_2,"//price",xmlValue)

file_5 <- data.frame(NAME=file_3, PRICE=file_4)

View(file_5)

만약에 되지 않는다면 다음 글을 참고해보자:

다른 출처: lunalog.tistory.com/41

 

R에서 파일 읽기

실행한 RData가 있다면 같은 디렉토리에 파일이 있어야 하고, R studio 를 사용한다면 파일이 기본디렉토리에 있어야 한다 1. csv파일 > 변수명 default : 파일의 첫번째 행을 header로 인식 변수행의 이름

lunalog.tistory.com