本文介绍了当我使用函数parser.parsefromstring(xml.responsetext,“text / XML”)时出了什么问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我尝试读取xml文件,但我遇到了问题......我的代码是: < script> function myFunction(xml){ var i; var 解析器,xmlDoc; parser = new DOMParser(); xmlDoc = parser.parseFromString(xml.responseText, text / xml); alert(xmlDoc); var x = xmlDoc.getElementsByTagName( 书); alert(x.length); alert(x.parentNode.nodeName); } function loadDoc(){ var xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function (){ myFunction( this ); }; xhttp.open( GET, books.xml, true ); xmlHttp.onreadystatechange = handleServerResponse; xhttp.send(); } < / script> 我的问题是当我跑的时候 alert(xmlDoc); // print [object xmldoucument] alert(x.length); //打印0 为什么警报(x.length);打印零?怎么了?我想获得书籍的名称......但是x.length是零...我的xml文件: < 书店 > < book category = 烹饪 > < title lang = en > 日常意大利语< / title > < 作者 > Giada De Laurentiis < / author > < 年 > 2005 < / year > < 价格 > 30.00 < / price > < / book > < book category = children > < title lang = en > 哈利波特< / title > < 作者 > J K. Rowling < / author > < 年 > 2005 < / year > < 价格 > 29.99 < / price > < / book > < book 类别 = web > < title lang = en > XQuery Kick Start < / title > < 作者 > James McGovern < / author > < 作者 > Per Bothner < / author > < 作者 > Kurt Cagle < / author > < 作者 > James Linn < / author > < 作家 > Vaidyanathan Nagarajan < / author > < 年 > ; 2003 < / year > < 价格 > ; 49.99 < / price > < / book > < book category = web 封面 = 平装书 > < title lang = zh > 学习XML < / title > < 作者 > Erik T. Ray < / author > < 年 > 2003 < / year > < 价格 > 39.95 < / price > < / book > < / bookstore > 我把我的xml文件放在我的项目这里我把文件放在 WEB_INF 文件夹下 所以任何帮助PLZ? 我的尝试: 我的代码是以上任何帮助plz?解决方案 如果你想查看有多少书,你可以做以下事情 - var bookXML = new XML(xmlDoc) var books = bookXML [' book']; var length = books.length(); 注意:我没有太多关于XML的功能,所以可能会有一些语法错误。 请让我知道,它不起作用。 :) 我解决了问题...我使用xmlhttp而不是xhttp,它可以工作 I try to read xml files but I have a problem ... my code is :<script > function myFunction(xml) { var i; var parser, xmlDoc; parser = new DOMParser(); xmlDoc = parser.parseFromString(xml.responseText,"text/xml"); alert(xmlDoc); var x = xmlDoc.getElementsByTagName("book"); alert(x.length); alert(x.parentNode.nodeName); } function loadDoc() { var xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function() { myFunction(this); }; xhttp.open("GET", "books.xml", true); xmlHttp.onreadystatechange = handleServerResponse; xhttp.send(); }</script>my problem is when I run thenalert(xmlDoc); // print [object xmldoucument]alert(x.length); // print 0 why alert(x.length); print zero ? what wrong ? I want to obtain the name of books ... but x.length is zero ... my xml file :<bookstore> <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>and I put my xml file in my project here here i put the file under WEB_INF folderso any help plz ?What I have tried:my code is above so any help plz ? 解决方案 If you want to check how many books are there, you can do something like following-var bookXML = new XML(xmlDoc)var books = bookXML['book'];var length = books.length();Note: I haven't worked much on XML suffs so there can be some syntax errors. Please let me know, it it doesn't work.:)I solved the problem ... I use xmlhttp instead of xhttp and it work 这篇关于当我使用函数parser.parsefromstring(xml.responsetext,“text / XML”)时出了什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-30 01:42