I want to analyze XML from the top using javascript.

Asked 1 years ago, Updated 1 years ago, 99 views

If you use XML::LibXML::Reader in perl, you can analyze XML by retrieving nodes from the top, but what should you do with javascript (or jQuery, etc.Or is there a library that can do the same thing?

javascript xml

2022-09-30 15:49

1 Answers

There used to be an E4X.Deprecated now

Instead, it would be good to use DOMParser

Note:

Sample Code

const xml=`<sales vendor="John">
    <item type="peas" price="4" quality="6"/>
    <item type="carrot" price="3" quality="10"/>
    <item type="chips" price="5"quantity="3"/>
</sales>`;

const parser = new DOMParser();
const sales=parser.parseFromString(xml, 'text/xml');
conditions = Array.from(sales.getElementsByTagName("item"));
alert(items.find(item=>item.getAttribute("type")==="carrot").getAttribute("quantity"));
alert(sales.getElementsByTagName("sales").item(0).getAttribute("vendor"));
items.forEach(item=>alert(item.getAttribute("price"))))


2022-09-30 15:49

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.