I have a div
element in TouchXML, and I know for a fact that it contains an a
element which I need to access. This is the element, which is declared as CXMLElement *element
in a for-in loop
<div id="song_html" class="show3">
<div class="left">
<!-- info mp3 here -->
128 kbps<br/>2:35<br/>2.36 mb
</div>
<div id="right_song">
<div style="font-size:15px;"><b>The Doors - The Crystal Ship mp3</b></div>
<div style="clear:both;"/>
<div style="float:left;">
<div style="float:left; height:27px; font-size:13px; padding-top:2px;">
<div style="float:left;"><a href="http://member.jinbo.net/renegade/music/The Doors-03.The Crystal Ship.mp3" rel="nofollow" target="_blank" style="color:green;">Download</a></div>
<div style="margin-left:8px; float:left; width:27px; text-align:center;"><a href="javascript:void(0)" onclick="showPlayer_new(14851327, 'a89d3a80a719b6c91a4c441f879685e1c8647140', 'the+doors', 'the+crystal+ship')" rel="nofollow" id="lk14851327" class="play_now">Play</a></div>
<!-- New ad start -->
<script><![CDATA[
var id_array = ["newad14851327"]; window.onload = function(){
var i = 0;
for(i=0; i < id_array.length; i++){
var tgt = document.getElementById(id_array[i]).href; document.getElementById(id_array[i]).href = ""; document.getElementById(id_array[i]).onclick = function(){
window.location.href= tgt;
return false;
};
} }
]]>
</script><!-- New ad end -->
<div style="margin-left:8px; float:left;"><a href="http://ads.ad-center.com/offer?prod=91&ref=4989201&q=The+Doors+-+The+Crystal+Ship" rel="nofollow" target="_blank" style="color:red;" id="newad14851327">Download Album</a></div>
<!-- ToneFuse Start -->
<script><![CDATA[ (function(id) {
console.log("tonefuse id:"+id);
document.write('<div style="margin-left:8px; float:left; color:red;" id="'+id+'">]]>
</script>
</div>
');
tfp_multi_ad_slots = window.tfp_multi_ad_slots || [];
tfp_multi_ad_slots.push(id);
window.tfpWriteMultiAd && tfpWriteMultiAd();
})(("tfp_multi_"+Math.random()).replace(".",""));
<!-- ToneFuse End -->
<div style="clear:both;"/>
</div>
<div id="player14851327" style="float:left; margin-left:10px;" class="player"/>
</div>
<div style="clear:both;"/>
</div>
(Sorry about the formatting; I didn't write it)
As you can see, there is an a
element (there's actually two) in this. However, when I attempt to access these elements using the following
NSArray *linkElements = [element elementsForName:@"a"];
linkElements
always contains 0 objects. Why is TouchXML not filling linkElements
with the two a
elements?
The elementsForName:
method only looks at the immediate child nodes of the element. It does not do a recursive descent. In your posted code, element
does not have any a
elements, just more div
child elements. You need to write your own code to recursively traverse all of the child div
elements until you find all of the desired a
elements.