gpt4 book ai didi

Javascript XML 相同元素名称

转载 作者:行者123 更新时间:2023-11-28 09:33:05 24 4
gpt4 key购买 nike

我有一个 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 = "";
}
}

最佳答案

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

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

关于Javascript XML 相同元素名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13494532/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com