Javascript XML相同的元素名称

时间:2012-11-21 13:38:50

标签: javascript xml ajax dom

我有一个多次包含相同元素名称的XML文件,如下所示。我只能检索第一个。想知道如何使用JavaScript检索所有4个“评论”。谢谢!

<FRIENDSSTATUS>
  <STATUSFRIENDS id="2">
    <STATUS>Life begins at the end of your comfort zone</STATUS>
    <DATETIME>2012-10-30 10:32:28</DATETIME>
    <FIRSTNAME>Malcolm</FIRSTNAME>
    <LASTNAME>Landgraab</LASTNAME>
    <COMMENTS datetime="2012-11-18 22:13:28" firstname="Ian" lastname="Tellerman">rr</COMMENTS>
    <COMMENTS datetime="2012-11-18 22:13:39" firstname="Ian" lastname="Tellerman">hello</COMMENTS>
    <COMMENTS datetime="2012-11-19 14:36:24" firstname="Ian" lastname="Tellerman">test</COMMENTS>
    <COMMENTS datetime="2012-11-19 14:45:52" firstname="Ian" lastname="Tellerman">test4</COMMENTS>
   </STATUSFRIENDS>
  <STATUSFRIENDS id="3">
    <STATUS>Prometheus. Fantastic film!!!!</STATUS>
    <DATETIME>2012-10-30 10:32:28</DATETIME>
    <FIRSTNAME>Mansa</FIRSTNAME>
    <LASTNAME>Bendett</LASTNAME>
    <COMMENTS datetime="2012-11-18 22:14:46" firstname="Ian" lastname="Tellerman">wrong</COMMENTS>
  </STATUSFRIENDS>
</FRIENDSSTATUS>

javascript代码:

    xmlDoc = xmlhttp.responseXML;
$table = "<table border='1'>";

var element = xmlDoc.getElementsByTagName("FRIENDSSTATUS");
var elements = xmlDoc.getElementsByTagName("STATUSFRIENDS");
var comments = xmlDoc.getElementsByTagName("COMMENTS");

    if(xmlDoc.documentElement !== null)
    {

    var root;
    if (navigator.userAgent.indexOf("Firefox") > 0 )
    var root = element[0].childElementCount;

    if (navigator.userAgent.indexOf("MSIE") > 0 )
    var root = xmlDoc.documentElement.childNodes.length;

    for(i=0; i<root; i++){
$strStats =  "";
$strComments = "";
$strDateTime = "";
$strFirstname = "";
$strLastname = "";

$strCommentDateTime =  "";
$strCommentFirstName = "";
$strCommentLastName = "";
$strStatusID = "";

if(navigator.userAgent.indexOf("Firefox") > 0 ){
for(y=0;y < elements[i].childElementCount;y++)
{
 $strStatusID = elements[i].getAttribute("id");
  if(strStatus = elements[i].getElementsByTagName("STATUS"))
    {
        $strStats = strStatus[0].firstChild.nodeValue; 

    }

  if(strStatus = elements[i].getElementsByTagName("DATETIME"))
    {
        $strDateTime = strStatus[0].firstChild.nodeValue; 

    }

  if(strStatus = elements[i].getElementsByTagName("FIRSTNAME"))
    {
        $strFirstname = strStatus[0].firstChild.nodeValue; 

    }

  if(strStatus = elements[i].getElementsByTagName("LASTNAME"))
    {
        $strLastname = strStatus[0].firstChild.nodeValue; 

    }
  if(strComm = elements[i].getElementsByTagName("COMMENTS"))
    { 
        $strComments = strComm[0].firstChild.nodeValue;
        $strCommentDateTime = comments[i].getAttribute("datetime");
        $strCommentFirstName = comments[i].getAttribute("firstname");
        $strCommentLastName = comments[i].getAttribute("lastname");

        $strComments += "<br>" + $strCommentFirstName + " " + $strCommentLastName + " " + $strCommentDateTime + "</br></br>";
   }
  $strStats = $strStats + " " + $strLastname + " " + $strFirstname + " " + $strDateTime;
  $strFullName = $strFirstname + " " + $strLastname;
  $strDateTime = "";
  $strFirstname = "";
  $strLastname = "";
}
}

4 个答案:

答案 0 :(得分:0)

将XML分配给变量(本例中为myXmlString),并按如下方式读取:

var parser=new DOMParser();
doc=parser.parseFromString(myXmlString,'text/xml');
var comments = doc.getElementsByTagName("COMMENTS") // Returns a array of those "comments".

答案 1 :(得分:0)

如何访问第一个STATUSFRIENDS的评论的示例。 jsFiddle demo here

var parseXml;

if (typeof window.DOMParser != "undefined") {
    parseXml = function(xmlStr) {
        return ( new window.DOMParser() ).parseFromString(xmlStr, "text/xml");
    };
} else if (typeof window.ActiveXObject != "undefined" &&
       new window.ActiveXObject("Microsoft.XMLDOM")) {
    parseXml = function(xmlStr) {
        var xmlDoc = new window.ActiveXObject("Microsoft.XMLDOM");
        xmlDoc.async = "false";
        xmlDoc.loadXML(xmlStr);
        return xmlDoc;
    };
} else {
    throw new Error("No XML parser found");
}

var xml = parseXml(xmlStr); // assuming xmlStr contains your XML
var commentsList = xml.documentElement.childNodes[0].getElementsByTagName("COMMENTS");
var output = "";

for(var i=0; i<commentsList.length; i++)
{
   output += commentsList[i].firstChild.nodeValue + "<br />";   
}

document.getElementById("comments").innerHTML = output;

答案 2 :(得分:0)

这样的事情:

var str = 'your XML as a string',
    parser, xml;

if (window.DOMParser) {
    parser = new DOMParser();
    xml = parser.parseFromString(str, "text/xml");
}
else { // IE
    xml = new ActiveXObject("Microsoft.XMLDOM");
    xml.async = "false";
    xml.loadXML(str);
}

var nodes = xml.childNodes[0].getElementsByTagName('COMMENTS');

var i, l = nodes.length, comments = [];
for (i = 0; i < l; i++) {
    comments.push({
        firstname: nodes[i].getAttribute('firstname'),
        lastname: nodes[i].getAttribute('lastname'),
        datetime: nodes[i].getAttribute('datetime'),
        content: nodes[i].childNodes[0].nodeValue
    });
}

console.log(comments);

答案 3 :(得分:0)

我没有时间发布完整的代码示例,但JS支持XPath,您可以使用此Qpath查询获取注释:

/ FRIENDSSTATUS / STATUSFRIENDS [@ ID = “3”] /评论

以下是我测试Xpath语法的地方:http://chris.photobooks.com/xml/default.htm