javascript - get child and subchild attribute value using ajax xpath and store in to variable -
xml :-
<childrens> <child_1 entity_id="1" value="root catalog" parent_id="0"> <child_2 entity_id="2" value="navigate" parent_id="1"> <child_4 entity_id="4" value="activities" parent_id="2"> <child_10066 entity_id="10066" value="physical1" parent_id="4"> <child_10067 entity_id="10067" value="cricket" parent_id="10066"> <child_10068 entity_id="10068" value="one day" parent_id="10067"/> </child_10067> </child_10066> <child_10069 entity_id="10069" value="test2" parent_id="4"/> <child_10070 entity_id="10070" value="test3" parent_id="4"/> <child_10071 entity_id="10071" value="test4" parent_id="4"/> <child_10072 entity_id="10072" value="test5" parent_id="4"/> <child_5 entity_id="5" value="physical" parent_id="4"/> </child_4> <child_4331 entity_id="4331" value="region" parent_id="2"> <child_4332 entity_id="4332" value="asia" parent_id="4331"> <child_4333 entity_id="4333" value="afghanistan" parent_id="4332"> <child_4334 entity_id="4334" value="balkh" parent_id="4333"> <child_4335 entity_id="4335" value="mazar-e-sharif" parent_id="4334"/> </child_4334> <child_4336 entity_id="4336" value="herat" parent_id="4333"> <child_4337 entity_id="4337" value="herat" parent_id="4336"/> </child_4336> </child_4333> </child_4332> </child_4331> </child_2> </child_1> </childrens> in above xml file try activities , region both in different variable.
tried.
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script> <script> var result_region=''; var node_region; var comma_region=''; var activity; var result_activity=''; var node_activity; var comma_activity=''; var region; $(document).ready(function(){ $.ajax({ url: 'final.xml', type:"get", async: false, success: function(xml) { var xpath_region = '//child_4/*[@value]' + '/@value'; var xpath_activity = '//child_4331/*[@value]' + '/@value'; var iterator = xml.evaluate(xpath_region, xml.documentelement, null, xpathresult.unordered_node_iterator_type, null); var thisnode = iterator.iteratenext(); while (thisnode) { node_region = thisnode.textcontent; thisnode = iterator.iteratenext(); if(result_region.indexof(node_region) == -1){ result_region += comma_region+node_region; comma_region=','; } } var iterator = xml.evaluate(xpath_activity, xml.documentelement, null, xpathresult.unordered_node_iterator_type, null); var thisnode = iterator.iteratenext(); while (thisnode) { node_activity = thisnode.textcontent; thisnode = iterator.iteratenext(); if(result_activity.indexof(node_activity) == -1){ result_activity += comma_activity+node_activity; comma_activity=','; } } activity = result_region; region = result_activity; alert(activity); alert(region); }, }); }); </script> above code returned:-
var activity = "physical1,test2,test3,test4,test5"; var region = "asia"; its return first level node b'coz try on xpath find child_4 want child , there child node.
like:-
var activity = "physical1,cricket,one day,test2,test3,test4,test5"; var region = "asia,afghanistan,balkh,mazar-e-sharif,herat,herat"; how can child , there child sub child value.
thanks.
your quite close: have change both xpaht expressions from:
var xpath_region = '//child_4/*[@value]' + '/@value'; var xpath_activity = '//child_4331/*[@value]' + '/@value'; to:
var xpath_region = '//child_4//*[@value]' + '/@value'; var xpath_activity = '//child_4331//*[@value]' + '/@value'; you have selected direct child of child_4 wiht value attribute select descendant of child_4 (//child_4/descendant::*) or short (//child_4//*) output following regions:
"asia,afghanistan,balkh,mazar-e-sharif,herat" and activities:
"physical1,cricket,one day,test2,test3,test4,test5"
Comments
Post a Comment