7
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

fetchした結果をXMLパースする

7
Last updated at Posted at 2017-12-17

MPDを利用するのにXMLパースしたかった
XMLHttpRequestでの仕方はよく記事があったがfetchだと見当たらなかったのでメモ程度ですが共有

fetchのpolyfillを参考にのparseJSONの部分をparseXMLにした例文を用意してみた

function checkStatus(res) {
  if (res.status >= 200 && res.status < 300) {
    return res
  } else {
    const error = new Error(res.statusText);
    error.response = res;
    throw error
  }
}

function parseXML(response) {
  return response.text().then((stringContainingXMLSource) => {
    const parser = new DOMParser();
    return parser.parseFromString(stringContainingXMLSource, "text/xml");
  });

fetch(url)
  .then(checkStatus)
  .then(parseXML)
  .catch((error) => {
    console.log('request failed', error)
  });
7
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?