<data> - tag
<country name "Singapore"> name = attribute
</country> - element
1) root: tree의 가장 상위에 있는 노드
(root[1][2]와 같이 인덱싱으로 가능하나 좋지 않은 방법)
2) iter() : 모든 elements에서 해당 Tag를 검색
3) find() : element를 찾을 때 사용
4) find all(): 현재 요소의 직계 자식인 태그가 있는 요소만 찾는다
5) get() : attribute의 값을 찾을 때 사용
6) set(’update’,’값’): attribute 추가
7) remove(): 삭제
def get_seoul_hotspots(request):
api_key = settings.SEOUL_API_KEY
url = f'http://openapi.seoul.go.kr:8088/{api_key}/xml/citydata/1/20/강남 MICE 관광특구'
print(' get_seoul_hotspots')
response = requests.get(url)
hotspots_data = response.content
xml_dict = xmltodict.parse(hotspots_data)
json_data = json.dumps(xml_dict)
return HttpResponse(json_data, content_type = 'application/json')
json.dump(): Python 객체를 Json혁식으로 직렬화하여 파일에 쓰는 역할
json.loads(): json 형식의 문자열을 피싱하여 Python 객체로 로드하는 역할, 주로 API 등에서 받은 JSON 문자열을 파이썬에서 사용하기 위해 파싱할 때 사용됩니다.