Cocoa: Loading a XML File (array); -
is there way can load xml file , put data array exemple:
the xml file:
<xml ...> <adressbook> <contact name="name" phone="00000000"/> <contact name="name2" phone="00000002"/> <contact name="name3" phone="00000003"/> <contact name="name4" phone="00000004"/> </adressbook>
now have to if want each attribute in 1 array lets say:
nsarray* name; nsarray* phone;
i want make each array holds (consecutively) xml attributes. in case like:
name hold:
name name2 name3 name4
and phone: 00000000 000000002 000000003 000000004
thanks in advance.
source:
nsxmldocument* doc = [[nsxmldocument alloc] initwithcontentsofurl: [nsurl fileurlwithpath:@"/folder/with/sample.xml"] options:0 error:null]; nsmutablearray* objects = [[nsmutablearray alloc] initwithcapacity:10]; nsmutablearray* descriptions = [[nsmutablearray alloc] initwithcapacity:10]; nsxmlelement* root = [doc rootelement]; nsarray* objectelements = [root nodesforxpath:@"//object" error:nil]; for(nsxmlelement* xmlelement in objectelements) [objects addobject:[xmlelement stringvalue]]; nsarray* descelements = [root nodesforxpath:@"//description" error:nil]; for(nsxmlelement* xmlelement in descelements) [descriptions addobject:[xmlelement stringvalue]]; nslog(@"%@", objects); nslog(@"%@", descriptions); [doc release]; [objects release]; [descriptions release];
sample xml-file:
<?xml version="1.0" encoding="utf-8"?> <items> <item id="0"> <object>first object</object> <description>description one</description> </item> <item id="1"> <object>second object</object> <description>description two</description> </item> </items>
Comments
Post a Comment