Last active
June 4, 2020 05:22
-
-
Save benvium/7398ef1bab7a5b448608 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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* | |
* 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
Congratulations, benvium, for your solution.
I already a lot of search one clean solution and one effective code, however your code was the only one that run with sucess (internet and others foruns). I get up this alert to all the programers that how me, spent a long time searching a real solution that works.
Attention all the programers, i am using NodeJS 10.13 and the WebService was development using .NET 4.5.1 C # WCF ASMX. Below there are one slice from my code to help all the programers, that passed many time searching one effective solution:
This is inside the file React Native App.js, lets go:
PS: The xmlBase need completed with your result request webservice
The result was the XML child value: Code Blog
Have a nice day and merry christmas.
Willy Thorpe