-
Data Engineering - XMLData Engineering 2020. 9. 28. 19:16
XML
- XML (eXtensible Markup Languagee) 은 1996년 W3C가 제안한 웹 문서 표준 형식으로 SGML의 하위셋
- HTML과 달리 웹 상에서 구조화된 문서를 전송 가능하도록 설계됨
- 확장자 : .xml
- 데이터에 의미를 부여하는 메타데이터를 기술할 수 있음
[예] “CPU 2.83GHz”:
- HTML: 데이터 명과 실제 데이터 구분 표시가 불가능
- XML: CPU과 2.83로 구분 가능
<?xml version="1.0"?> <data> <country name="Liechtenstein"> <rank>1</rank> <year>2008</year> <gdppc>141100</gdppc> <neighbor name="Austria" direction="E"/> <neighbor name="Switzerland" direction="W"/> </country> <country name="Singapore"> <rank>4</rank> <year>2011</year> <gdppc>59900</gdppc> <neighbor name="Malaysia" direction="N"/> </country> <country name="Panama"> <rank>68</rank> <year>2011</year> <gdppc>13600</gdppc> <neighbor name="Costa Rica" direction="W"/> <neighbor name="Colombia" direction="E"/> </country> </data>
- 첫 번째 코드는 XML 코드로 이루어진 문서라는 것을 명시
- <data> 태그가 루트
- 그 아래 자식 태그들
XML Parser 선택문제
XML 파싱 기법
- XML 파싱 기법 선택 -> 구현 용이, 성능
- DOM, SAX, ElementTree 등
-> ElmentTree 방식 사용이 파이썬에서 좋음
표준 및 구현
- standard : DOM의 경우 W3C
- Implementation - > Library 선택 (Paper, Essay등 연구결과)
XML Parsing Library
- XML parsing 라이브러리는 아주 많음
- xml.etree.ElementTree module, lxml 등
XML 파싱 기법
DOM 방식
- reads the XML into memory and converts it to objects
- 메모리 사용량이 크지만, 속도는 빠른 편
- 자바스크립트 등 웹에서 주로 사용
SAX 방식
- 자료를 하나 하나 읽어가며 파싱하여 메모리 사용량 적지만, 속도는 느린 편
- 주로 스마트폰 등의 응용프로그램에서 사용
ElementTree 방식
- a simple-to-use and very fast XML tree library
- Elementtree가 DOM이나 SAX보다 좋은 이유
-> Essay: https://eli.thegreenplace.net/2012/03/15/processing-xml-in-python-with-elementtree/
Processing XML in Python with ElementTree - Eli Bendersky's website
March 15, 2012 at 06:14 Tags Articles , Python When it comes to parsing and manipulating XML, Python lives true to its "batteries included" motto. Looking at the sheer amount of modules and tools it makes available out of the box in the standard library ca
eli.thegreenplace.net
XML 표준 및 구현
Standard
- Dom 방식을 선택한 경우, W3C 표준에 따라 개발
- ElementTree API 에 따라 개발 http://effbot.org/zone/element-index.htm
Ex) find(path) : Finds the first matching subelement, by tag name or path
구현 선택
- XML parsing 라이브러리
- v2.5이후 파이썬 표준라이브러리에 구현된 xml.etree.ElementTree 선택
XML Parsing Library
lxml
- lxml은 XML을 빠르고 유연하게 처리하는 외부 라이브러리
- lxml project site : https://lxml.de/
- Xpath (XML Path Language)와 XSLT (Extensible Stylesheet Language Transformation) 를 지원하며, 많이 쓰 이는 ElementTree API도 구현
Python Standard Library
- xml.etree.ElementTree 모듈
- xml.dom 모듈 (Document Object Model)
- xml.sax 모듈 (Simple API for XML)
XML Tree
국가별 기대수명 데이터 XML Parsing
유용한 도구
- dir( ) : 해당 데이터 유형의 메소드와 속성 확인
- list( ) : 자식 태그 목록 확인
import xml.etree.ElementTree as ET # parse xml file tree = ET.parse('data-text.xml')
Tag 속성
ElementTree의 XML Tag의 속성
- .tag : 해당 태그의 이름
- root의 tag는 “GHO“ root.tag #tag속성
# get root node root = tree.getroot() # ex1. dir() : 데이터 타입의 method, attribute list root.tag # dir(root) # list(root)
- .text : 해당 태그의 내용
- Disclaimer/Display태그의 text는 'The information in this database is … to this section.‘
- .attrib : 해당 노드의 attribute 맵 (key, value)
- 'Data/Observation/Dim' 태그는 다음의 attrib 맵을 가짐 {'Category': 'PUBLISHSTATE', 'Code': 'PUBLISHED'}
Tag find()메소드
# ex5. findall() list_dim = root.findall('Data/Observation/Dim') print(list_dim[8].attrib)
findall()은 리스트를 반환
'Data Engineering' 카테고리의 다른 글
Data Engineering - DataFrame(pandas) 6-2 (0) 2020.10.12 Data Engineering - csv, json 읽기week4-1(3-2) (0) 2020.09.21 Data Engineering - 데이터 랭글링 전과정 이해(week3-1) (0) 2020.09.14 Anaconda 가상환경 설정하기 (0) 2020.09.09 데이터 엔지니어링이란? 2020-2 / 2주차-1 (0) 2020.09.07