2011-11-02 21 views
0

我知道我的xml看起來很奇怪,但沒有做任何事情。現在我的問題是如何解析這個xml屬性和它的值使用TBXML?請有人幫助我。感謝提前。我該如何解析這個xml在目標c中使用TBXML?

−<order> 
<id>100</id> 
    <order_date>October 13th, 2011 at 2:43 PM</order_date> 
−<customer> 
    <id>45</id> 
    <name>John Kramer</name> 
    −<address> 
    −<billing>167 Yarra Street, South Yarra, Melbourne, Australia</billing> 
    −<shipping>35 Victoria, Street, North Yarra, Melbourne, Australia</shipping> 
     <email> [email protected]</email> 
     <phone> +6546325478 </phone> 
    </address> 
    </customer> 
    −<products> 
    −<product> 
    <id>12</id> 
    <name>Asus F5RL</name> 
    −<description>Color: Blue. Size: Square.</description> 
    <qty>2</qty> 
    <unit_price>50 AUD</unit_price> 
    <total_price>100 AUD</total_price> 
    </product> 
    −<product> 
    <id>12</id> 
    <name>Acer F4</name> 
    <description>Color: Red</description> 
    <qty>3</qty> 
    <unit_price>40 AUD</unit_price> 
    <total_price>120 AUD</total_price> 
    </product> 
</products> 

-<price_details> 
    <subtotal>220 AUD</subtotal> 
    <discount>20 AUD</discount> 
    <tax> 10 AUD </tax> 
    <shipment> 5 AUD </shipment> 
    <grand_total> 235 AUD </grand_total> 
    </price_details> 
<order_status>Pending </order_status> 
</order> 

現在我仍然這樣做,但它是崩潰。

- (void)traverseElement:(TBXMLElement *)element { 
do { 
    NSLog(@"%@",[TBXML elementName:element]); 
    if (element->firstChild) 
     [self traverseElement:element->firstChild]; 


    TBXMLAttribute * attribute = element->firstAttribute; 

    // if attribute is valid 
    while (attribute) { 
     // Display name and value of attribute to the log window 
     NSLog(@"%@->%@ = %@", 
       [TBXML elementName:element], 
       [TBXML attributeName:attribute], 
       [TBXML attributeValue:attribute]); 

     // Obtain the next attribute 
     attribute = attribute->next; 
    } 

    if ([[TBXML elementName:element] isEqualToString:@"order"]) { 
     NSLog(@"xml element checking"); 
     TBXMLElement *order_id = [TBXML childElementNamed:@"id" parentElement:element]; 
     TBXMLElement *order_date = [TBXML childElementNamed:@"order_date" parentElement:element]; 
     TBXMLElement *customer = [TBXML childElementNamed:@"customer" parentElement:element]; 

     TBXMLElement *customer_id = [TBXML childElementNamed:@"id" parentElement:customer]; 
     TBXMLElement *customer_name = [TBXML childElementNamed:@"name" parentElement:customer]; 
     [records addObject:[NSArray arrayWithObjects: 
          [TBXML textForElement:customer_id], 
          [TBXML textForElement:customer_name], 
          [TBXML textForElement:order_id], 
          [TBXML textForElement:order_date],nil ] ]; 
     NSLog(@"customer id: %@",customer_id); 
     NSLog(@"customer name: %@",customer_name); 

    } 
+0

@ anand:你能幫我嗎? – Emon

+0

@每個人:請幫助我... – Emon

回答

2

我必須解決這個問題,可能是它會有人樂於助人......

- (void)traverseElement:(TBXMLElement *)element { 
do { 
    NSLog(@"%@",[TBXML elementName:element]); 
    if (element->firstChild) 
     [self traverseElement:element->firstChild]; 


    TBXMLAttribute * attribute = element->firstAttribute; 

    // if attribute is valid 
    while (attribute) { 
     // Display name and value of attribute to the log window 
     NSLog(@"%@->%@ = %@", 
       [TBXML elementName:element], 
       [TBXML attributeName:attribute], 
       [TBXML attributeValue:attribute]); 

     // Obtain the next attribute 
     attribute = attribute->next; 
    } 

    if ([[TBXML elementName:element] isEqualToString:@"order"]) { 
     NSLog(@"xml element checking"); 
     TBXMLElement *order_id = [TBXML childElementNamed:@"id" parentElement:element]; 
     TBXMLElement *order_date = [TBXML childElementNamed:@"order_date" parentElement:element]; 
     TBXMLElement *order_customer = [TBXML childElementNamed:@"customer" parentElement:element]; 
     NSLog(@"order id: %@ %@ %@",[TBXML textForElement:order_id],[TBXML textForElement:order_date],[TBXML textForElement:order_customer]); 
     if([[TBXML elementName:order_customer] isEqualToString:@"customer"]){ 
      TBXMLElement *customer_id = [TBXML childElementNamed:@"id" parentElement:order_customer]; 
      TBXMLElement *customer_name = [TBXML childElementNamed:@"name" parentElement:order_customer]; 
      TBXMLElement *customer_address = [TBXML childElementNamed:@"address" parentElement:order_customer]; 
      NSLog(@"order id: %@ %@ %@",[TBXML textForElement:customer_id],[TBXML textForElement:customer_name],[TBXML textForElement:customer_address]); 
      if([[TBXML elementName:customer_address] isEqualToString:@"address"]){ 
       TBXMLElement *address_billing = [TBXML childElementNamed:@"billing" parentElement:customer_address]; 
       [records addObject:[NSArray arrayWithObjects: 
            [TBXML textForElement:order_id], 
            [TBXML textForElement:order_date], 
            [TBXML textForElement:customer_id], 
            [TBXML textForElement:customer_name], 
            [TBXML textForElement:address_billing],nil]]; 
      } 
     } 

     // TBXMLElement *order_status = [TBXML childElementNamed:@"status" parentElement:element]; 

     //NSLog(@"%@",[TBXML textForElement:id]); 
     // NSArray *newArray = [[NSArray alloc] init]; 

    } 
    [myTable reloadData]; 
} while ((element = element->nextSibling)); 
} 
-2

嘗試使用JSon解析器。爲了您更好地理解看到這link.this link。下載JSON解析器從這個link.

檢查與此thread.

+0

其實我不想使用JSON parser.so你能告訴我如何解析這個xml這個TBXML? – Emon

+0

放置錯誤代碼。所以我可以理解你哪裏錯了。 – Anand

+0

它沒有在錯誤控制檯中顯示任何錯誤,這是主要問題。 – Emon