The ability to rapidly parse XML is important for interacting with quite a few APIs and online offerings because it's miles a broadly used format for storing and transmitting data.
Once we have the XML text, we will parse it into a document item model (DOM) structure using the DOMParser API.
- First, we have an HTML structure with an <h1> element displaying the headline "Books" and a blank <ul> element with the id "bookList" that will display the parsed book information.
- It stands for the XML data that has to be parsed.
- It provides details about two books, including their titles and authors.
- Next, we use the DOMParser constructor to build a clean instance of the DOMParser.
- With the resource of this item, we may additionally convert HTML or XML strings into DOM files.
- The DOMParser instance's parseFromString() technique is used to convert the XML facts right into a DOM report.
- The XML content material and the content type, which is "text/XML" in this situation, are the two arguments which might be required.
- We take records about the books from the XML record after parsing them.
- Get all of the factors with the tag name "ebook" with the aid of calling getElementsByTagName() on the xmlDoc item.
- This produces a listing of e-book components.
- Using getElementById(), we also can get the <ul> element with the identity "bookList".
- For later use, this detail is kept inside the bookList variable.
- Now, we use a for loop.
- Using getElementsByTagName() on each book element, we retrieve the name and creator of every e-book inside the loop.
- Since there may be only one identify and one creator for each ebook, we get right of entry to the primary element inside the lower back series and acquire their textual content content material the use of textContent.
- To show the e-book statistics, we create an <li> detail the use of document.CreateElement().
- Using template literals, we set the textContent of the <li> detail to a string that contains the name and writer of the ebook.
- Finally, we use appendChild() to append each <li> detail to the bookList detail.
- Each book is now added as a new <li> item in the <ul> detail.