TBXML和子元素

时间:2014-04-19 03:43:49

标签: ios tbxml

我知道有很多关于TBXML的问题,我尝试了很少的解决方案,但没有什么对我有用。我是这个人的新手。

<?xml version="1.0" encoding="utf-8"?>
  <ArrayOfNewsFeedService xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/">
    <NewsFeedService>
      <LngNewsItemID>1</LngNewsItemID>
        <Contents>test</Contents>
        <LngUserID>1</LngUserID>
        <DteCreated>1/04/2014 12:00:00 a.m.</DteCreated>
        <NewsReply>
           <NewsFeedReplyService>
              <lngNewsItemReplyID />
              <LngNewsItemID>1</LngNewsItemID>
              <ReplyContent>reply</ReplyContent>
              <LngUserID>1</LngUserID>
              <DteCreated>1/04/2014 12:00:00 a.m.</DteCreated>
           </NewsFeedReplyService>
          <NewsFeedReplyService>
              <lngNewsItemReplyID />
              <LngNewsItemID>1</LngNewsItemID>
              <ReplyContent>reply2</ReplyContent>
              <LngUserID>1</LngUserID>
              <DteCreated>1/04/2014 12:00:00 a.m.</DteCreated>
          </NewsFeedReplyService>
        </NewsReply>
     </NewsFeedService>
   </ArrayOfNewsFeedService>

我可以阅读&#39;内容&#39;来自xml的价值很好,但是当我尝试阅读&#39; NewsReply&#39;时,我遇到了问题。和所有内在的xml。不确定如何解决它。任何帮助将不胜感激。我一直试图解决这个问题超过2周。

提前致谢

编辑 - 这里的道歉是我所尝试的代码。

NSString *theXML = [[NSString alloc] initWithBytes: [webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding];



NSLog(@"%@", theXML);

TBXML * tbxml = [TBXML newTBXMLWithXMLString:theXML];



NSLog(@"%@", [TBXML elementName:tbxml.rootXMLElement]);





if (tbxml.rootXMLElement)

{



    NSString *content = [self traverseElement:tbxml.rootXMLElement: @"Contents"];



    UITextViewPost.text = content;



    NSString *test = [self traverseElement:tbxml.rootXMLElement: @"NewsReply"];

    NSLog(@"ReplyContent - %@", test);



    TBXMLElement * elem_items = [TBXML childElementNamed:@"NewsFeedReplyService" parentElement:tbxml.rootXMLElement];



    NSLog(@"elem_items - %@", elem_items);



    NSMutableArray * array=[[NSMutableArray alloc]init];



    while (elem_items !=nil)

    {

        NSMutableDictionary * dictionary=[[NSMutableDictionary alloc]init];

        NSString * str_ParseData=[[NSString alloc]init];



        TBXMLElement * elem_item = [TBXML childElementNamed:@"NewsFeedReplyService" parentElement:elem_items];



        TBXMLElement * elem_itemid = [TBXML childElementNamed:@"LngUserID" parentElement:elem_item];

        str_ParseData = [TBXML textForElement:elem_itemid];

        [dictionary setObject:str_ParseData forKey:@"LngUserID"];



        TBXMLElement * elem_itemname = [TBXML childElementNamed:@"ReplyContent" parentElement:elem_item];

        str_ParseData = [TBXML textForElement:elem_itemname];

        [dictionary setObject:str_ParseData forKey:@"ReplyContent"];



        [array addObject:dictionary];

        elem_items = [TBXML nextSiblingNamed:@"NewsFeedReplyService" searchFromElement:elem_items];  /// end node



        NSLog(@"printing arrays - %@", str_ParseData);

    }

}

1 个答案:

答案 0 :(得分:0)

我修好了。如果有人想解决他们的问题,我使用了以下代码。

- (void) traverseElement:(TBXMLElement *)element {
do {
    if (element->firstChild) 
        [self traverseElement:element->firstChild];

    if ([[TBXML elementName:element] isEqualToString:@"Record"]) {
        TBXMLElement *destination = [TBXML childElementNamed:@"Destination" parentElement:element];
        TBXMLElement *status = [TBXML childElementNamed:@"Status" parentElement:element];
        TBXMLElement *guid = [TBXML childElementNamed:@"GUID" parentElement:element];
        TBXMLElement *dateSub = [TBXML childElementNamed:@"DateSubmitted" parentElement:element];
        TBXMLElement *dateToSend = [TBXML childElementNamed:@"DateToSend" parentElement:element];
        TBXMLElement *dateSent = [TBXML childElementNamed:@"DateSent" parentElement:element];
        TBXMLElement *dateReceived = [TBXML childElementNamed:@"DateReceived" parentElement:element];
        TBXMLElement *message = [TBXML childElementNamed:@"Message" parentElement:element];
        TBXMLElement *id = [TBXML childElementNamed:@"ID" parentElement:element];

        [records addObject:[NSArray arrayWithObjects:
                              [TBXML textForElement:destination],
                              [TBXML textForElement:status],
                              [TBXML textForElement:guid],
                              [TBXML textForElement:dateSub],
                              [TBXML textForElement:dateToSend],
                              [TBXML textForElement:dateSent],
                              [TBXML textForElement:dateReceived],
                              [TBXML textForElement:message],
                              [TBXML textForElement:id],nil]];  
    }
} while ((element = element->nextSibling));}

之后,只需在代码中调用函数来填充数组。

另外,请确保更改我上面发布的字段名称。

由于