0

当我尝试使用 NSXMLParser 解析 XML 文件时,我无法解析所有元素.. XML 响应如下所示:

<AList>
   <Details>
          <AId>1</AId>
          <Date>10/31/2012</Date>
          <Time>11:00 AM</Time>
       <User>
          <Employee>
              <Name>Rosy</Name>
              </Employee>
              <Company>
                  <Customer>
                     <CustName>Williams</CustName>
                     <CustPhone>1232322</CustPhone>
                 </Customer>
             </Company>
       </User>
  </Details>
<Details>
          <AId>2</AId>
          <Date>10/31/2012</Date>
          <Time>11:30 AM</Time>
       <User>
          <Employee>
              <Name>Mary</Name>
              </Employee>
              <Company>
                  <Customer>
                     <CustName>Christopher</CustName>
                     <CustPhone>123233323</CustPhone>
                 </Customer>
             </Company>
       </User>
  </Details>
</AList>

如何获取所有 XML 标记中的所有元素?

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {


if([elementName isEqualToString:@"AList"])
{
    arr=[[NSMutableArray alloc] init];



}
    if([elementName isEqualToString:@"Details"]||[elementName isEqualToString:@"User"])
    {
        drr=[[NSMutableDictionary alloc] init];
    }

srr=[[NSMutableString alloc] init];

} 


-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{


    [srr appendString:string];
}


- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {

    if([elementName isEqualToString:@"AId"])
    {

        [drr setObject:srr forKey:@"AId"];
        [srr release],srr=nil;
        return;
    }  

    if([elementName isEqualToString:@"Date"])
    {

        [drr setObject:srr forKey:@"Date"];
        [srr release],srr=nil;
       return;    
    }
    if([elementName isEqualToString:@"Time"])
    {

        [drr setObject:srr forKey:@"Time"];
        [srr release],srr=nil;
     return;      
    }
    if([elementName isEqualToString:@"Name"])
    {

        [drr setObject:srr forKey:@"Name"];
        [srr release],srr=nil;
        return;
    }
        if([elementName isEqualToString:@"CustName"])
    {
        [drr setObject:srr forKey:@"CustName"];
        [srr release],srr=nil;


       return;
    }

    if([elementName isEqualToString:@"CustPhone"])
    { 
        [arr addObject:drr]; 

    }    



    if([elementName isEqualToString:@"AList"])
    {

        [drr release];


    }

    [srr release],srr=nil;

}

我不想要 CustPhone...我只能从我编写的代码中获取 CustName。我哪里出错了?

4

3 回答 3

0

请更改此

    if([elementName isEqualToString:@"Details"]||[elementName isEqualToString:@"User"])
    {
        drr=[[NSMutableDictionary alloc] init];
    }

   if([elementName isEqualToString:@"Details"])
    {
        drr=[[NSMutableDictionary alloc] init];
    }

当找到客户电话元素时,您正在将字典添加到数组中。您正在分配字典两次。当找到“详细信息”和“用户”标签时。

于 2012-10-31T11:26:49.580 回答
0

您可以参考链接http://wiki.cs.unh.edu/wiki/index.php/Parsing_XML_data_with_NSXMLParser。希望它会帮助你。

于 2012-10-31T09:56:41.307 回答
0

您的 < /Alist> TAG 与 <AList> - 'L'不同。否则 NSXMLParser 可以轻松地在didFoundCharacters委托中为您正确解析所有其他标签数据。

这是您在字符串比较中的问题

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {

if([elementName isEqualToString:@"AList"]) // in ur xml it is Alist
{

    [drr release];

}

于 2012-10-31T10:02:14.080 回答