memostack
Published 2020. 7. 3. 21:07
React Native XML 파싱 Mobile/React Native
블로그를 이전하였습니다. 2023년 11월부터 https://bluemiv.github.io/에서 블로그를 운영하려고 합니다. 앞으로 해당 블로그의 댓글은 읽지 못할 수 도 있으니 양해바랍니다.
반응형

1. xml2js 추가

xml 파싱을 위하여 xml2js 라이브러리를 추가한다.

<python />
$ yarn add xml2js

2. 사용

<python />
import {parseString} from 'xml2js';
  • parseString를 사용하기 위해 import를 한다.

 

<javascript />
const url = "<xml API URL>"; fetch(url) .then((response) => response.text()) .then((responseText) => { parseString(responseText, (err, result) => { if (err !== null) { console.log('Fail get data.'); } else { console.log(result); } }); }) .catch((error) => { console.log('Error fetching the feed: ', error); });
  • fetch를 이용하여 API 호출.
  • API로 부터 받은 XML 데이터를 parseString을 이용하여 파싱.

 

3. Reference

https://yarnpkg.com/package/xml2js

 

https://yarnpkg.com/package/xml2js

Fast, reliable, and secure dependency management.

yarnpkg.com

 

반응형
블로그를 이전하였습니다. 2023년 11월부터 https://bluemiv.github.io/에서 블로그를 운영하려고 합니다. 앞으로 해당 블로그의 댓글은 읽지 못할 수 도 있으니 양해바랍니다.
profile

memostack

@bluemiv_mm

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!