i trying display nodes present in xml file. require is, child nodes displayed when parent node clicked. tried using onclick there error (i not sure if onclick can used in case)
my xml looks (it cannot modified)
<types xmlns:xsi="http://www.w3.org.."> <entities1> <abc gender="male" ></abc> <xyz sub="" norm=""></xyz> </entities1> <entities2> <phrase>some phrase</phrase> <sc/> </entities2> </types> code tried var nodes = xmldoc.documentelement.childnodes; (i = 0; < nodes.length; i++) { var node = nodes[i]; if (node.nodename === '#text') { document.write(""); } else { document.write("<br><br>" + "<b>" + "<ul>" + node.nodename + " </ul>" + "</b>" + "<br>"); document.getelementsbyclassname("node").onclick= function () { if (node.haschildnodes()) { var children = node.childnodes; (var j = 0; j < children.length; j++) { var child = children[j]; if (child.nodename === '#text' || child.nodename === '#comment') { document.write(""); } else { document.write("<li>" + child.nodename + " " + child.textcontent + "</li>" + " <br>"); (var k = 0; k < child.attributes.length; k++) { var attrib = child.attributes[k]; if (attrib.specified === true) { document.write("<li>" + attrib.name + " = " + attrib.value + "</li>" + " <br>"); } document.write("<br>"); } } } } }; } }
<!doctype html> <html> <head> <title> </title> <script type="text/javascript"> function loadxmldoc(dname) { if (window.xmlhttprequest) { xhttp=new xmlhttprequest(); } else { xhttp=new activexobject("microsoft.xmldom"); } xhttp.open("get",dname,false); xhttp.send(); return xhttp.responsexml; } var myxml = loadxmldoc("test.xml"); //document.write(myxml.getelementsbytagname("title")[0].childnodes[0].nodevalue + "<br>"); var mybtn = document.getelementbyid('expand_parent'); function myfun1(){ var divarea = document.getelementbyid('myanchor'); var msg = myxml.getelementsbytagname("bookstore")[0].childnodes[0].nodevalue; divarea.text = msg; } function myfun2(){ var divarea = document.getelementbyid('mydiv'); var output = ""; var x = myxml.getelementsbytagname("title"); (i=0;i<x.length;i++) { output += x[i].childnodes[0].nodevalue; //output+=": "; //output+=x[i].childnodes[0].nodevalue; output+="<br>"; } divarea.innerhtml = output; } </script> </head> <body onload="myfun1()"> <a href="#" id="myanchor" onclick="myfun2();return false;"> </a> <div id="mydiv"></div> </body> <html> , xml follows : <?xml version="1.0" encoding="utf-8"?> <bookstore>mybookstore <book category="cooking"> <title lang="en">everyday italian</title> <author>giada de laurentiis</author> <year>2005</year> <price>30.00</price> </book> <book category="children"> <title lang="en">harry potter</title> <author>j k. rowling</author> <year>2005</year> <price>29.99</price> </book> <book category="web"> <title lang="en">xquery kick start</title> <author>james mcgovern</author> <author>per bothner</author> <author>kurt cagle</author> <author>james linn</author> <author>vaidyanathan nagarajan</author> <year>2003</year> <price>49.99</price> </book> <book category="web" cover="paperback"> <title lang="en">learning xml</title> <author>erik t. ray</author> <year>2003</year> <price>39.95</price> </book> </bookstore>
Comments
Post a Comment