Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kevinold/1d16631a4603d639638f45c85245877d to your computer and use it in GitHub Desktop.
Save kevinold/1d16631a4603d639638f45c85245877d to your computer and use it in GitHub Desktop.
Parse XML Example using React Native.xmldom is a pure JavaScript implementation of an XML Parser. I've added it to window so that browser modules that require it will work. Tested on iOS and Android.
/**
*
* Before use, type:
* ```
* npm install xmldom --save
* ```
*/
window.DOMParser = require('xmldom').DOMParser;
const exampleParse = ()=> {
const text = `<bookstore>
<book category="cooking">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price> </book>
<book category="children">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price> </book>
<book category="web">
<title lang="en">XQuery Kick Start</title>
<author>James McGovern</author>
<author>Per Bothner</author>
<author>Kurt Cagle</author>
<author>James Linn</author>
<author>Vaidyanathan Nagarajan</author>
<year>2003</year>
<price>49.99</price> </book>
<book category="web" cover="paperback">
<title lang="en">Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price> </book>
</bookstore>`;
const parser = new DOMParser();
const xmlDoc = parser.parseFromString(text);
console.log("XmlTest.exampleParse " + xmlDoc.getElementsByTagName("title")[0].childNodes[0].nodeValue);
};
module.exports = exampleParse
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment