开发者

Getting XML child elements -- include tag names

开发者 https://www.devze.com 2023-02-22 19:22 出处:网络
I am trying to parse some XML using NSXMLParser on the iPhone. When I detect the element I want, I would like to have everything asis between the start element and end element.

I am trying to parse some XML using NSXMLParser on the iPhone.

When I detect the element I want, I would like to have everything as is between the start element and end element.

For example, given:

<A>
    <K>
        <C>
            <D>dddd</D>
        </C>
        <E> eeee </E>
    </K>
    <F> ffff </f>
</A>

I would like to specify K

And receive:

    <C> 
        <D> dddd </D>
    </C>
    <E> eeee </E>

However, currently NSXMLParser ignores all the <, >, and attributes. What do I put in the delegate methods to make sure I get everything as is in between the tags I specify?

Here is what I have right now:

//---when the start of an element is found---
-(void) parser:(NSXMLParser *) parser didStart开发者_如何学编程Element:(NSString *) elementName namespaceURI:(NSString *) namespaceURI qualifiedName:(NSString *) qName attributes:(NSDictionary *) attributeDict {

    if( [elementName isEqualToString:@"ns11:createNewPrincipalResponse"])
    {
        elementFound = YES;
        NSLog(@"PrincResp FOUND");
    }
}


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


//---when the end of element is found---
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName

    if ([elementName isEqualToString:@"ns11:createNewPrincipalResponse"])
    {
        //---displays the country---
        NSLog(@"\n******************\nParse Results...\n*************\n\n%@",parseResults);
        [parseResults setString:@""];
        token = [NSString stringWithString:parseResults];
        elementFound = FALSE; 
    }
}

Thanks in advance!


You can probably use columnNumber, lineNumber parser's methods to get the location where your element starts (i.e. store them as column1, line1 in didStartElement for the element you look for), then when that element ends, in didEndElement, get the column2 and line2 again and then just cut the text from the original XML from (line1, column1) to (line2, column2).

Quite hacky but it'll probably work.

Edit: to extract text you could do something along these lines:

__block int lineNumber = 0;
__block NSMutableString* result = [NSMutableString stringWithCapacity:100];

[str enumerateLinesUsingBlock:^(NSString* line, BOOL* stop) {

if(lineNumber == line1) {
    [result appendString:[line substringFromIndex:column1]];
}
if(lineNumber > line1 && lineNumber < line2) [result appendString:line];

if(lineNumber == line2) {
    [result appendString:[line substringToIndex:column2]];
}
lineNumber++;
if(lineNumber > line2) *stop = YES;

}];

I don't know if the parser's position when the element starts/ends is at the beginning or at the end of that element - you may need to either skip it yourself or, if the position is at the end of the element, check if column1/column2 are not outside the line (in which case just don't add anything). Also, if you need to preserve newline characters, you'll need to add them yourself (instead of appendString:str use appendFormat:"%@\n", str)


NSXmlParser is fairly horrible to use. Have you tried using LibXML for parsing instead? Then you can use XPath to query the XML, which is much simpler and a much more standard way of parsing XML documents.

0

精彩评论

暂无评论...
验证码 换一张
取 消