+ 9

read XML with JS

I need some kind way if reading.parsing XML files with JS. I need toknow how to: -select the first, second etc. child -get attributes of an element -get value of element -how many elements exist called:““ I would prefer jQuery

12th Oct 2020, 5:38 PM
Galaxy-Coding (inactive)
Galaxy-Coding (inactive) - avatar
3 Réponses
+ 6
JaScript thanks, I have still a few unanswerd things how do I get the first, second, etc. if there is more than on title, for example How do I get an elements child WITH XML syntax, for exmaple get album would result in <title>Dirrty><year><2011></year> How many elements exist as a child of an element, for example album would result in 2 Thank You!
12th Oct 2020, 10:24 PM
Galaxy-Coding (inactive)
Galaxy-Coding (inactive) - avatar
+ 4
Today will be used mostly json which is easier to parsing. If you need xml you can do that i.e. as follows: let myxml = '<?xml version="1.0" encoding="ISO-8859-1"?>' + '<artists>' + '<artist name="Christina">' + '<albums>' + '<album>' + '<title>Dirrty</title>' + '<year>2011</year>' + '</album>' + '</albums>' + '</artist>' + '</artists>'; let myParser = new DOMParser(); let xmlDOM = myParser.parseFromString(myxml, 'text/xml'); let artist = xmlDOM.querySelector('artist'); console.log(artist.getAttribute('name')); // "Christina" console.log(artist.querySelector('title').textContent); // "Dirrty" console.log(artist.querySelector('year').textContent); // 2011
12th Oct 2020, 7:57 PM
JaScript
JaScript - avatar
+ 3
Your welcome Galaxy-Coding . The selection of XML elements is similar to html DOM. The example as follows shows all needed for it. I hope, this will help you. https://code.sololearn.com/WKzTAH2PqetR/?ref=app
13th Oct 2020, 11:06 AM
JaScript
JaScript - avatar