Chapter 11 Lesson 6 PDF
Chapter 11 Lesson 6 PDF
Chapter 11
Lesson 6
Figure 11-22
Parsing XML Content
• Syntax for parser object used to convert XML text stored
within the body of a server response into an XML DOM
object:
let parser = new DOMParser();
• Syntax to create the XML DOM:
parser.parseFromString(text, mimeType)
– text is the text string to be parsed
– mimeType is the type of structured data, such as text/html,
text/xml, or image/svg+xml
• Sample expression that creates and uses a parser object:
new
DOMParser().parseFromString("headlines.xml",
"text/xml")
Working with an XML Node Tree
• XML node trees are DOM objects and thus accessible to
the same JavaScript methods used with an HTML DOM,
such as querySelectorAll() and appendChild()
– For the sports blog project, you can use these methods to convert
the XML content from the newsfeed file into an HTML article
• You can use the JSON object to write HTML code for a suggestion
box, where json.matches[0], json.matches[1], etc. are the
suggested keywords from the matches array:
<div id="suggestBox">
<div class="suggestion">json.matches[0]</div>
<div class="suggestion">json.matches[1]</div>
…
</div>