Element Tree 를 이용한다. python3.5 기준으로 내장되어있다.
1. Xml이 문자열일 때
다음과 같은 Xml string이 있다.
1 2 3 4 5 6 7 8 9 10 | <? xml version = "1.0" encoding = "utf-8" ?> < GXG_RES type = "BillingLog" > < result > < status >9</ status > < detail >9100</ detail > < message >요청하신 조건과 일치하는 정보가 없습니다.</ message > < appid >OA00701871</ appid > < count >0</ count > </ result > </ GXG_RES > |
1 2 3 4 5 6 | import xml.etree.ElementTree as ET tree = ET.ElementTree(ET.fromstring(r.text)) note = tree.getroot() res = note.find( 'result' ) res.find( 'status' ).text |
getroot 함수는 root 태그인 GXG_RES 라는 태그를 받아온다.
그리고 그 하위 태그는 바로 result이기 때문에 result를 find하고,
다시 또 거기서 하위를 찾는 등으로 찾을 수 있다.
findall을 이용해서 모든 태그를 다 찾을 수도 있다.
2.xml이 파일일 때
1 2 3 | from xml.etree.ElementTree import parse tree = parse( "note.xml" ) note = tree.getroot() |
나머지는 위와 동일하게 할 수 있다.
'Developer > 모아두기' 카테고리의 다른 글
[Linux] Ubuntu net.core.somaxconn 값 변경 (1) | 2016.06.22 |
---|---|
[uwsgi + nginx] uwsgi unix socket 문제 (1) | 2016.06.22 |
[Nginx] nginx error – 413 Request Entity Too Large (0) | 2016.06.22 |
[uwsgi,psycopg2] uwsgi multi-worker 에러 (1) | 2016.04.25 |
[python] psycopg2 설치 안될 때. (0) | 2016.04.08 |