Click here to Skip to main content
15,943,477 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
The following XML document is stored in an object called "x":

XML
<?xml version = "1.0"?>
<note>
 <to>Tove</to>
 <from>Jani</from>
 <heading>Reminder</heading>
 <body>Don't forget me this weekend</body>
</note>


What will each of the following code(s) display when used?

1.)
document.write(x.documentElement.childNodes.item(0).nodeName)

2.)
document.write(x.documentElement.childNodes.length)

3.)
document.write(x.documentElement.childNodes[1].childNodes[0].nodeValue)

4.)
document.write(x.documentElement.childNodes[0].nextSibling.nodeName)

5.)
document.write(x.documentElement.nodeName)

6.)
document.write(x.documentElementchildNode[1].nodeName)


What I have tried:

-Looking up the information on my own
-Trying recreate the XML document and trying a multitude of things
-Going through old notes
-Searching the internet for a more clear-cut answer
-Trying anything else
Posted
Updated 26-Jul-23 3:23am

While we are more than willing to help those that are stuck, that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.

Just posting your assignment and saying "I googled for the answers" isn't going to get you anywhere.
Read your course materials, read the documentation (Document: documentElement property - Web APIs | MDN[^] is a good place to start), and try executing the code against the XML file.
 
Share this answer
 
Comments
Larry Chapman II 26-Jul-23 7:19am    
Thank you for your words and encouragement. This is in efforts to earn an XML certification—which I've failed 3 times already—for something at work. When I look back at the course material on w3schools, I can't find it anywhere except in the exam itself. I know at most what the code I posted is to do, but I can't seem to figure it out because the "document.write(x.documentElement...)" confuses me a little. Thank you for your answer nonetheless.
The document.write statement is part of the HTML DOM, and nothing to do with XML. XML is merely a useful way of formatting data for transmission between computing systems or applications. So it seems that you are mixing two different systems without fully understanding either. You can learn both at W3Schools Online Web Tutorials[^].
 
Share this answer
 
Okay so, as far as I've come along this is essentially what I'm trying to do here:

<!DOCTYPE html>
<html>
<body>

<p id="demo">something else should be showing</p>

<script>
var parser, xmlDoc;
var text = "<note><to>Tove</to>"+"<from>Jani</from>"+"<heading>Reminder</heading>"+"<body>Don't forget me this weekend</body></note>";

parser = new DOMParser();
xmlDoc = parser.parseFromString(text,"text/xml");

document.getElementById("demo").innerHTML =
xmlDoc.getElementsByTagName("note")[0].childNodes.length; //Output: 4

/*xmlDoc.getElementsByTagName("note")[0].childNodes.item(0).nodeName;*/ //Output: "to"

/*xmlDoc.getElementsByTagName("note")[0].nodeName;*/ //Output: 

/*xmlDoc.getElementsByTagName("note")[0].childNodes[1].nodeName;*/ //Output: "from"

/*xmlDoc.getElementsByTagName("note")[0].childNodes[1].childNodes[0].nodeValue;*/ //Output: "Jani"

/*xmlDoc.getElementsByTagName("note")[0].childNodes[0].nextSibling.nodeName;*/ //Output: "from"
</script>

</body>
</html>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900