madLokesh
madLokesh

Reputation: 1878

Unable to Parse following XML?

I have continuous tags of img id="#", where # varies from 1 to 9.

The description of tags consists of floating values.

When I use the standard NSXML Parser, i not getting all the values.

My XML for reference:

<img id="1">-0.0111328,-0.0635608,0.152549,0.11211,-0.0250431,

            -0.0370875,0.0862391,0.0970791,-0.0195908,

            -0.00892297,0.0791795,0.0554013,0.00362028,0.0138572,0.0432729,

             0.0253036,-0.0770325,0.14065,0.118424,0.1787,

             0.0734354,0.160883,0.101831,0.237038,0.0681151,0.178331,

             0.106532,0.224731,0.133766,0.222096,0.165214,0.240752,

             -0.0280366,0.106239,0.052094,0.110642,   

</img>

How would I parse the above XML?

Kindly, help me out.

Thanx

Upvotes: 2

Views: 153

Answers (2)

madLokesh
madLokesh

Reputation: 1878

Finally got it... i placed start and end tags for img id = '#'. My structure now looks like this:

<images>

    <img id = '1'> -0.0111328,-0.0635608,0.152549,0.11211,-0.0250431,

        -0.0370875,0.0862391,0.0970791,-0.0195908,

        -0.00892297,0.0791795,0.0554013,0.00362028,0.0138572,0.0432729,

         0.0253036,-0.0770325,0.14065,0.118424,0.1787,

         0.0734354,0.160883,0.101831,0.237038,0.0681151,0.178331,

         0.106532,0.224731,0.133766,0.222096,0.165214,0.240752,

         -0.0280366,0.106239,0.052094,0.110642, ....  

     </img>

     <img id = '2'> ... 

     </img>

   ....

   ....

</images>

<mapping>

  <map>
    <imgid> 1 </imgid>
    <keyword> heavy </keyword>

  </map>

  <map>
    <imgid> 2 </imgid>
    <keyword> metal </keyword>

  </map>

  ...

  ...

</mapping>

Placing start and end tags allowed me to parse the whole xml.

Earlier, the start and end tags were for individual images which only resulted in parsing of one img.

This made me add another key point while parsing XML.

Hope this helps others as well.

Upvotes: 0

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 727027

This is because parser:foundCharacters: does not deliver all characters at once. You need to concatenate all strings that you get between the callbacks of the parser:didStartElement:namespaceURI:qualifiedName:attributes: and parser:didEndElement:namespaceURI:qualifiedName: that you get for the <img> tag.

In the code below, buf is an NSMutableString ivar of your parser delegate.

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict {
    if ([qualifiedName isEqualToString:@"img"]) {
        buf = [NSMutableString string];
    }
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
    if ([qualifiedName isEqualToString:@"img"]) {
        buf = [buf stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
        NSLog(@"Got %@", buf);
    }
}

- (void) parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    [buf appendString:string];
}

Upvotes: 2

Related Questions