1

我正在尝试解析 XML,但 XML 代码中的引号引起了问题并且无法运行。有没有办法调用这个xml <table border="0" cellpadding="0" cellspacing="0" width="392">,如代码所示?

预先感谢。

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

    if ( [elementName isEqualToString:@" table border="0" cellpadding="0" cellspacing="0" width="392" "] ) {

        [[self titleArray] addObject:[self currentStringValueTitle]];


    }


    [self setCurrentStringValueTitle:nil]; 

我想阅读这个源页面

<body>
<table border="0" cellpadding="0" cellspacing="0" width="392">
    <tr>
        <td width="392" class="hdlnType2">
        <div
            style="FONT-WEIGHT: bold; FONT-SIZE: 1.12em; FLOAT: left; ">
            <!-- enable chat --> 


        I wanna print this text....

        </td>
    </tr>
    <tr>
        <td></td>
    </tr>
</table>
</body>
4

1 回答 1

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

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

         NSString *border=[attributeDict valueForKey:@"border"];
         NSString *cellpadding=[attributeDict valueForKey:@"cellpadding"];
         NSString *cellspacing=[attributeDict valueForKey:@"cellspacing"];
         NSString *width=[attributeDict valueForKey:@"width"];

     }


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

         //fetch values here like did for table tag from attributedDict

     }
} 


- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string { 
if(!currentElementValue) 
    currentElementValue = [[NSMutableString alloc] initWithString:string];
else
    [currentElementValue appendString:string];
//NSLog(@"Processing Value: %@", currentElementValue);
}

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

        if([elementName isEqualToString:@"div"]) {
            NSLog(currentElementValue,nil);
        }
        [currentElementValue release];
        currentElementValue = nil;
 }

任何标签的参数都会被解析并保存到 didStartElement 方法的属性Dict 参数中

于 2012-04-30T10:16:36.177 回答