Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
613 views
in Technique[技术] by (71.8m points)

html - How can I iterate through an Elements child nodes using Javascript?

I'm trying to iterate through a DOM elements children and display their attributes in my code:

document.write("<table border='1'>");
var testcaseiterator = xmlDoc.getElementsByTagName("TestCase")[1].childNodes;

for(i=0; i < testcaseiterator.length; i++){

    document.write("<tr><td>");
    document.write(testcaseiterator[i].getAttribute('name'));
    document.write("</td></tr>");
}
document.write("</table>");

My XML:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="XSLTOutput2.xsl"?>
<TestSuite>
  <ActiveStep case="6" step="14" />
  <TestCase name="Visit all buttons [in region Button: Navi 0]">
    <Verification type="screen" id="1e542f75-1ba2-482d-8edd-39f70620d6dd" status="">
      <screen widgetid="audio_radio_fm">
        <menu entryStrategy="FIRST" looped="false" orientation="HORIZONTAL" selectable="true" widgetid="status">
          <entry selectable="true" widgetid="status" widgettype="entry">
            <label widgetid="status_label">status</label>
          </entry>
        </menu>
        <menu entryStrategy="SECOND" looped="false" orientation="HORIZONTAL" selectable="true" widgetid="apl">
          <button selectable="true" widgetid="apl_navi" widgettype="button">
            <label widgetid="apl_navi_label">Navi</label>
          </button>
          <button selectable="true" widgetid="apl_audio" widgettype="button">
            <label widgetid="apl_audio_label">Audio</label>
          </button>
          <button selectable="true" widgetid="apl_tel" widgettype="button">
            <label widgetid="apl_tel_label">Tel</label>
          </button>
          <button selectable="true" widgetid="apl_video" widgettype="button">
            <label widgetid="apl_video_label">Video</label>
          </button>
          <button selectable="true" widgetid="apl_system" widgettype="button">
            <label widgetid="apl_system_label">System</label>
          </button>
          <button selectable="true" widgetid="apl_net" widgettype="button">
            <img src="img/apl_net.png" widgetid="apl_net_image" />
          </button>
        </menu>
        <menu entryStrategy="FIRST" looped="false" orientation="HORIZONTAL" selectable="true" widgetid="plf">
          <entry selectable="true" widgetid="playfield" widgettype="entry">
            <label widgetid="playfield_label">playfield</label>
          </entry>
        </menu>
        <menu entryStrategy="MIDDLE" looped="false" orientation="HORIZONTAL" selectable="true" widgetid="audio_radio_sfl">
          <button selectable="true" widgetid="audio_radio_sfl_radio" widgettype="button">
            <label widgetid="audio_radio_sfl_radio_label">Radio</label>
          </button>
          <button selectable="true" widgetid="audio_radio_sfl_presets" widgettype="button">
            <label widgetid="audio_radio_sfl_presets_label">Presets</label>
          </button>
          <button selectable="true" widgetid="audio_radio_sfl_info" widgettype="button">
            <label widgetid="audio_radio_sfl_info_label">Info</label>
          </button>
          <button selectable="true" widgetid="audio_radio_sfl_fm" widgettype="button">
            <label widgetid="audio_radio_sfl_fm_label">FM</label>
          </button>
          <button selectable="true" widgetid="audio_radio_sfl_sound" widgettype="button">
            <label widgetid="audio_radio_sfl_sound_label">Sound</label>
          </button>
        </menu>
      </screen>
    </Verification>
    <Verification optionID="Audio" type="focus" id="d713b58b-06b1-4274-9a79-fe90a071383c" status="" />
    <Command type="cce:left" id="3a591a3c-9b9f-41a7-9b50-0892197128ec" status="" />
    <Verification optionID="Navi" type="focus" id="6037419a-25ae-4a7f-b842-0e88df9dfe4c" status="" />
  </TestCase>

This is not working: Any ideas?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
var testcaseiterator = xmlDoc.getElementsById("tc").childNodes;

The rest of the code looks okay.

for(i=0; i < testcaseiterator.length; i++)
 {
     document.write("<tr><td>");
     document.write(testcaseiterator[i].getAttribute('name'));
     document.write("</td></tr>");
 }
 document.write("</table>")

You will also have to edit the 5th line of the XML,

 <TestCase id ="tc"  name="Visit all buttons [in region Button: Navi 0]">

This will only work if you XML in your DOM structure.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...