XML文件解析委托问题

时间:2017-11-27 07:34:18

标签: ios objective-c xml

我有xml文件,我正在通过NSXML解析器的委托方法解析。我有三个委托方法didStart,FoundCharacters和didEndElement。当我解析文件时,当它进入找到的字符方法时,它会向我显示控制台中xml文件的所有字符串数据,但我只是尝试获取给定的标记数据值。它向我展示了xml文件中的所有字符串数据。我的xml文件就是这个,

<?xml version="1.0" encoding="UTF-8"?>
<QF id="AB2001" topic="Alertness" category="C&amp;M" ni_exempt="no">
<question>
    <xref>DES s4, DES s9, HC r159-161</xref>
    <text>Before you make a U-turn in the road, you should</text>
    <graphic></graphic>
    <prompt>Mark one answer</prompt>
    <voice id="AB2001-1"/>

    <explanation>
    <text>If you want to make a U-turn, slow down and ensure that the road is clear in both directions. Make sure that the road is wide enough to carry out the manoeuvre safely.</text>
    <voice id="AB2001-2"/>
</explanation>

</question>
<answers>
    <answer correct="no">
        <text>give an arm signal as well as using your indicators</text>
        <graphic></graphic>
    </answer>
    <answer correct="no">
        <text>signal so that other drivers can slow down for you</text>
        <graphic></graphic>
    </answer>
    <answer correct="yes">
        <text>look over your shoulder for a final check</text>
        <graphic></graphic>
    </answer>
    <answer correct="no">
        <text>select a higher gear than normal</text>
        <graphic></graphic>
    </answer>
</answers>

这些方法就是我正在解析的方法,

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



 attributes: (NSDictionary *)attributeDict

{
if([elementName isEqualToString:@"QF"]) {
    NSLog(@"ID %@",[attributeDict objectForKey:@"id"]);
    NSLog(@"Topic %@",[attributeDict objectForKey:@"topic"]);

}

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

    _foundValue = [[NSMutableString alloc] init];

}

}
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
// Store the found characters if only we're interested in the current element.

if([_currentElement isEqualToString:@"xref"]){
// init the ad hoc string with the value
_currentElement = [[NSMutableString alloc] initWithString:string];
    NSLog(@"Hello");
} else {
// append value to the ad hoc string
[_currentElement stringByAppendingString:string];
NSLog(@"Hi");
}
NSLog(@"Processing value for : %@", string);
}
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{

if( [elementName isEqualToString:@"QF"])
{
    [_foundValue setString:elementName];

    NSLog(@"rr %@",_foundValue);
}

if( [elementName isEqualToString:@"xref"])
{
    [_foundValue setString:elementName];
}
_currentElement = nil;
}

在控制台中,我从答案标签中获取所有字符串数据,但我还没有给出该标签进行解析。我很困惑,为什么它没有从我给出的标签中获得价值?

0 个答案:

没有答案