Как получить унаследованный XML на iOS? - PullRequest
0 голосов
/ 25 августа 2011

Я пытаюсь извлечь XML через этот код.

NSArray *contacts= [[[[xmlDoc rootElement] elementsForName:@"contacts"] objectAtIndex:0] elementsForName:@"contacts"];

for (int i = 0; i < [contacts count]; ++i)
            {
contact = [NSMutableDictionary dictionary];
[contact setValue:[[[[contacts objectAtIndex:i] elementsForName:@"contact_name"] objectAtIndex:0] stringValue] forKey:@"name"];

Простой XML извлекается легко, но сложно, как я показал ниже, из-за проблем.

Пожалуйста, помогите мне.

<contacts>
        <contact>
          <contact_id>0</contact_id>
        </contact>
      </contacts>

Ответы [ 2 ]

2 голосов
/ 07 сентября 2011

Это может помочь тебе, Фараз

TBXMLElement *invoiceTag = [TBXML childElementNamed:@"invoice" parentElement:root];
while(invoiceTag!=NULL){
    TBXMLElement *invoice_idTag = [TBXML childElementNamed:@"invoice_id" parentElement:invoiceTag];
    TBXMLElement *numberTag = [TBXML childElementNamed:@"number" parentElement:invoiceTag];
    TBXMLElement *contactsTag = [TBXML childElementNamed:@"contacts" parentElement:invoiceTag];
    TBXMLElement *contactTag = [TBXML childElementNamed:@"contact" parentElement:contactsTag];
    while (contactTag!=NULL) {
        TBXMLElement *contact_idTag = [TBXML childElementNamed:@"contact_id" parentElement:contactTag];
        contactTag=contactTag->nextSibling;
    }
}
0 голосов
/ 08 сентября 2011

Наконец-то я это сделал.Для тех из вас, у кого такая же проблема, проверьте этот код.

NSArray *foundRoots = [[xmlDoc rootElement] elementsForName:@"invoice"] ;
    NSLog(@"aaaaaaaaa %@",foundRoots);
    CXMLElement *Root = [foundRoots objectAtIndex:0];
    NSLog(@"root ......... %@",Root);
    NSArray *children = [Root children];
    NSLog(@"childdtee ------- %@",children);

    NSArray *tag1 = [[[Root elementsForName:@"contacts"]objectAtIndex:0]elementsForName:@"contact"];
    NSLog(@"contacts........%@",tag1);

    for (int i = 0; i < [tag1 count]; ++i)
    {
        getDetailInvoice = [NSMutableDictionary dictionary];
        [getDetailInvoice setValue:[[[[tag1 objectAtIndex:i] elementsForName:@"contact_id"] objectAtIndex:0] stringValue] forKey:@"1"];
        [invoiceDetailData addObject:getDetailInvoice];

        NSLog(@"%@",invoiceDetailData);
    }
...