日期:2014-05-16  浏览次数:20555 次

js如何解析xml字符串?
如下xml字符串:
--------------------------------------
<markers>
<marker carId="1" carName="SHANGHAI" gpsTime="2011-08-26 21:21:11.0" lat="31.230393" lng="121.473704" direction="222" speed="22" />
<marker carId="7" carName="XIAMEN" gpsTime="2011-08-26 21:21:11.0" lat="24.479836" lng="118.089421" direction="222" speed="22" />
</markers>
---------------------------------------
如何解析?
----------------------------------------
提示:我在网上看到的js解析xml字符串方法,xml字符串都是如下格式,
上面的格式怎么解析?
<NewInfo>
<News>
  <Title>今天要下雨</Title>
  <Content>今天确实下雨了</Content>
<Date>2009-02-04</Date>
</News>
<News>
<Title>今天出太阳</Title>
<Content>今天确实出太阳了</Content>
  <Date>2009-02-03</Date>
</News>
</NewInfo>
参考帖子
http://blog.csdn.net/snowfox3761/article/details/4281775
http://zhidao.baidu.com/question/397490376.html
http://blog.163.com/hubincqdj@yeah/blog/static/122148106200972914454372/
-----------------------------------------
xml js ajax

------解决方案--------------------
都有例子你还不会啊;
 var items = xmlDoc.getElementsByTagName("marker"); 
   alert(items.length);
   for(var i=0;i<items.length;i++){ 
   var x = items[i];
   alert(x.getAttribute("carId")); 
   alert(x.getAttribute("carName")); 
  }

------解决方案--------------------
<html>
<head>
<script type="text/javascript">
function parseXML()
{
text="<note>";
text=text+"<to>George</to>";
text=text+"<from>John</from>";
text=text+"<heading>Reminder</heading>";
text=text+"<body>Don't forget the meeting!</body>";
text=text+"</note>";
try //Internet Explorer
  {
  xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
  xmlDoc.async="false";
  xmlDoc.loadXML(text);
  }
catch(e)
  {
  try //Firefox, Mozilla, Opera, etc.
  {
  parser=new DOMParser();
  xmlDoc=parser.parseFromString(text,"text/xml");
  }
  catch(e)
  {
  alert(e.message);
  return;
  }
}
document.getElementById("to").innerHTML=xmlDoc.getElementsByTagName("to")[0].childNodes[0].nodeValue;
document.getElementById("from").innerHTML=xmlDoc.getElementsByTagName("from")[0].childNodes[0].nodeValue;
document.getElementById("message").innerHTML=xmlDoc.getElementsByTagName("body")[0].childNodes[0].nodeValue;
}
</script>
</head>

<body onload="parseXML()">
<h1>W3Schools Internal Note</h1>
<p><b>To:</b> <span id="to"></span><br />
<b>From:</b> <span id="from"></span><br />
<b>Message:</b> <span id="message"></span>
</p>
</body>
</html>

------解决方案--------------------
<html>