使用Javascript从Xml获得未定义的结果
我知道以前在不同的情况下都有人问过这个问题 …但我遵循w3schools的示例--> 我试图做的是调用我的函数并返回结果,但我一直得到未定义的结果。目标是获取此XML文档中节点的值使用Javascript从Xml获得未定义的结果,javascript,xml,xpath,undefined,Javascript,Xml,Xpath,Undefined,我知道以前在不同的情况下都有人问过这个问题 …但我遵循w3schools的示例--> 我试图做的是调用我的函数并返回结果,但我一直得到未定义的结果。目标是获取此XML文档中节点的值 <curriculum> <Course Name="Slyngel"> <StartDate>2014-09-04</StartDate> <EndDate>2014-09-10</EndDate>
<curriculum>
<Course Name="Slyngel">
<StartDate>2014-09-04</StartDate>
<EndDate>2014-09-10</EndDate>
<Heading>My heading</Heading>
<Info>This is some info about the course</Info>
<Subheading>Course Include</Subheading>
<Body>
<Item>This</Item>
<Item>And This</Item>
<Item>And This</Item>
</Body>
<Price>2000</Price>
</Course>
</curriculum>
这是我试图用这些代码行调用函数时得到的结果。
也许不能解决问题,但您确定行var InfoInfoText=InfoHeaderText=GetCourseInfo('Info')?对GetCourseInfo()有一个用于节点名称的参数。GetCourseInfo然后使用两个属性调用DisplayInfo,一个来自http参数(属性=名称),然后是节点名称。它看起来是这样的:函数GetCourseInfo(node){var CourseName=GetURLParameter('Course');DisplayInfo(CourseName,node);}thx用于您的响应;我没有质疑函数getcourseinfo(),而是质疑最后一行中变量的赋值;这就像“VarA=b=c;”而不是“VarA=c;”我认为最好将其改为“VarInfoInfoText=GetCourseInfo('Info');”,因为这种赋值会导致js错误我没有看到,这是错的。非常感谢!
function DisplayInfo(courseName, node) {
var x = loadXMLDoc("Xml/CourseInfo.xml");
var xml = x.responseXML;
path = "/curriculum/Course[@Name='" + courseName + "']/" + node;
var resultValue ="";
// code for IE
if (window.ActiveXObject || xhttp.responseType == "msxml-document") {
xml.setProperty("SelectionLanguage", "XPath");
var nodes = xml.selectNodes(path);
return nodes.childNodes[0].nodeValue;
}
// code for Chrome, Firefox, Opera, etc.
else if (document.implementation && document.implementation.createDocument) {
var nodes = xml.evaluate(path, xml, null, XPathResult.ANY_TYPE, null);
var result = nodes.iterateNext();
return result.childNodes[0].nodeValue;
}
}
var InfoHeaderText = GetCourseInfo('Heading');
InfoHeader.innerHTML = (InfoHeaderText);
var InfoInfoText = InfoHeaderText = GetCourseInfo('Info');
InfoInfo.innerHTML = (InfoInfoText);