3

i have an xml file which contains country and codes. i have put the xml in Demo.xml .My problem is that only first dictionary has been saved. or anyone can tell me other way of parsing it like using nsxml or gdata

NSString *filePath=[[NSBundle mainBundle] pathForResource:@"Demo" ofType:@"xml"];
NSData *data=[NSData dataWithContentsOfFile:filePath];
addArray=[[NSMutableArray alloc]init];
dictData=[[NSMutableDictionary alloc]init];


TBXML *tbxml = [TBXML tbxmlWithXMLData: data];
TBXMLElement *rootXMLElement=tbxml.rootXMLElement;

TBXMLElement *rootElemnt=[TBXML childElementNamed:@"root" parentElement:rootXMLElement];
while (rootElemnt!=nil) {

    TBXMLElement *item = [TBXML childElementNamed:@"item" parentElement:rootElemnt];

    if (item!=nil) {
        TBXMLElement *country = [TBXML childElementNamed:@"country" parentElement:item];
        TBXMLElement *code = [TBXML childElementNamed:@"code" parentElement:item];

        dictData=[NSMutableDictionary dictionaryWithObjectsAndKeys:[TBXML textForElement:country],@"country",[TBXML textForElement:code],@"code",nil];
        rootElemnt = [TBXML nextSiblingNamed:@"item" searchFromElement:rootElemnt];

        [addArray addObject:dictData];
    }
}
NSLog(@"====%@",addArray);

And the outPut is:

====(
        {
            code = 355;
            country = Albania;
        }
    )

Here is the link of xml https://www.dropbox.com/s/x4zpxgi42h0rllz/Demo.xml

  • Why don't you make a plist instead of xml. There are convince methods to convert into dictionary or array. – Inder Kumar Rathore Aug 29 '13 at 14:05
  • @InderKumarRathore- yes bt it will be a long task to make a plist with country codes – Sakshi Singh Aug 29 '13 at 14:06
  • Sakshi it just took 3 min to change it to plist by just using find and replace command. You know plist is just an xaml if you use data file as plist instead of xml you will see how easy thing becomes. BTW you can download your [plist from here](http://www.filesend.net/download.php?f=3c2337c5b8324552b767540a48354659) – Inder Kumar Rathore Aug 30 '13 at 04:48
  • @InderKumarRathore-thnaks this is new thing which i learned to convert a xml file to .plist – Sakshi Singh Aug 30 '13 at 11:26

1 Answers1

2

Not tested but

NSString *filePath=[[NSBundle mainBundle] pathForResource:@"Demo" ofType:@"xml"];
NSData *data=[NSData dataWithContentsOfFile:filePath];
addArray=[[NSMutableArray alloc]init];
dictData=[[NSMutableDictionary alloc]init];


TBXML *tbxml = [TBXML tbxmlWithXMLData: data];
TBXMLElement *rootXMLElement=tbxml.rootXMLElement;

TBXMLElement *rootElemnt=[TBXML childElementNamed:@"root" parentElement:rootXMLElement];
if (rootElemnt) {
    TBXMLElement *item = [TBXML childElementNamed:@"item" parentElement:rootElemnt];

    while (item) {

        TBXMLElement *country = [TBXML childElementNamed:@"country" parentElement:item];
        TBXMLElement *code = [TBXML childElementNamed:@"code" parentElement:item];

        dictData=[NSMutableDictionary dictionaryWithObjectsAndKeys:[TBXML textForElement:country],@"country",[TBXML textForElement:code],@"code",nil];

        [addArray addObject:dictData];
        item = [TBXML nextSiblingNamed:@"item" searchFromElement:item];
    }
}
NSLog(@"====%@",addArray);

should work

Jerome Diaz
  • 1,746
  • 8
  • 15
  • rootElemnt = [TBXML nextSiblingNamed:@"item" searchFromElement:rootElemnt]; will always return nil, item isn't a sibling of your initial value of rootElemnt – Jerome Diaz Aug 29 '13 at 14:26