javascript - document.documentElement function does not display proper result -
i trying parse xml file using java-script. according tutorial read found root element, have use document.documentelement
.
i use syntax when tried display returned value syntax, browser displays [object htmlhtmlelement]
.
my question is: (1) why getting [object htmlhtmlelement]
displayed in web browser. (2) according below posted xml-file, should expect output after usingrootelement = document.documentelement;
please find below code used(javascript) , xml file.
javascript
function findwriter() { var schriftstellerknoten, spracheknoten; var fuellerknoten, dichtungknoten, anzeige, rootelement; rootelement = document.documentelement; document.write(rootelement); }
xml file:
<?xml version="1.0" ?> <schriftsteller> <englischsprache> <dichtung> <fueller> <name>jane austin</name> <name>rex stout</name> <name>dashiell hammett</name> </fueller> </dichtung> </englischsprache> </schriftsteller>
document.documentelement
root html tag of page in javascript present.
to fetch , display xml content, must access xml document , use load xml file following:
function findwriter() { var schriftstellerknoten, spracheknoten; var fuellerknoten, dichtungknoten, anzeige, rootelement; if (window.xmlhttprequest){ xhttp=new xmlhttprequest(); //for non ie browsers } else { // ie 5/6 xhttp=new activexobject("microsoft.xmlhttp"); } xhttp.open("get","test.xml",false); xhttp.send(); xmldoc=xhttp.responsexml; //fetch xml file contents var nameel = xmldoc.documentelement.getelementsbytagname("name"); //get <name> tags in (i=0; i<nameel.length; i++){ document.write("name" + + ": " + nameel[i].firstchild.nodevalue + "<br/>"); //write names } }
Comments
Post a Comment