简体   繁体   中英

javascript XML ChildNodes

Hi I am trying to access a node's elements with childNodes. Here is a sample XML

<ROOT xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<node>
 <nodeid>28</nodeid> 
 <account_no xsi:nil="true" /> 
 <address1>15 CANCUN CT</address1> 
 <serial_no>112199543</serial_no> 
 <x_lat>25.95513358000</x_lat> 
 <y_lon>-97.49027147000</y_lon> 
 <alarm>
  <alarmid>Outage</alarmid> 
  <alarmtime>2012-07-30T14:46:29</alarmtime> 
 </alarm>
 <alarm>
  <alarmid>Restore</alarmid> 
  <alarmtime>2012-07-30T14:48:37</alarmtime> 
 </alarm>
 </node>
</ROOT>

I'm trying to get the second childNodes but can't using javascript. I can however get the nodevalues from the first by this js code.

var alarmId = xmlDocOut.getElementsByTagName('alarmid')[i].childNodes[0].nodeValue;
var alarmTime = xmlDocOut.getElementsByTagName('alarmtime')[i].childNodes[0].nodeValue;

If i try to use ...[i].childNodes[1].nodeValue the js will throw an error saying 'Object Required' on that line.
I've tried

...[i].childNodes[1...4].nodeValue 

and

...[i].childNodes[0].childNodes[0].nodeValue.  

And still nothing!

xmlDocOut.getElementsByTagName('alarmid')[i].childNodes[1] doesn't exist; every alarmId node only has 1 child node; a text node.

for example:

<alarmid>Outage</alarmid>

has 1 childNode; a textNode with text == "Outage".

我知道了,我只需要在下标上添加... [i + 1] .childNodes [0] .nodeValue即可获取第二个警报元素。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM