NSXMLParser无法解析内部实体

时间:2014-02-14 16:12:05

标签: objective-c xml macos

我有这个XML声明内部实体n

NSString *xmlString = @"<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<!DOCTYPE JMdict [<!ENTITY n \"noun\">]>
    <JMdict>
         <entry>
             <sense>
                 <pos>&n;</pos>
                 <gloss>love</gloss>
             </sense>
         </entry>
    </JMdict>";

并使用NSXMLParser来解析它

NSXMLParser *parser = [[NSXMLParser alloc] initWithData:[xmlString dataUsingEncoding:NSUTF8StringEncoding]];
[parser setDelegate:self];
[parser parse];

我实现了委托方法

- (void)parser:(NSXMLParser *)parser foundInternalEntityDeclarationWithName:(NSString *)name value:(NSString *)value {
    NSLog(@"foundInternalEntityDeclarationWithName: %@ = %@", name, value);
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
    NSLog(@"<%@>", elementName);
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
    NSLog(@"</%@>", elementName);
}

- (void)parser:(NSXMLParser *)parser didStartMappingPrefix:(NSString *)prefix toURI:(NSString *)namespaceURI {
    NSLog(@"didStartMappingPrefix %@ %@", prefix, namespaceURI);
}

- (void)parser:(NSXMLParser *)parser didEndMappingPrefix:(NSString *)prefix {
    NSLog(@"didEndMappingPrefix %@", prefix);
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
    NSLog(@"\t%@", string);
}

- (void)parser:(NSXMLParser *)parser foundIgnorableWhitespace:(NSString *)whitespaceString {
    NSLog(@"foundIgnorableWhitespace %@", whitespaceString);
}

- (void)parser:(NSXMLParser *)parser foundProcessingInstructionWithTarget:(NSString *)target data:(NSString *)data {
    NSLog(@"foundProcessingInstructionWithTarget %@", target);
}

- (void)parser:(NSXMLParser *)parser foundComment:(NSString *)comment {
    NSLog(@"foundComment %@", comment);
}

- (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock {
    NSLog(@"foundCDATA");
}

- (NSData *)parser:(NSXMLParser *)parser resolveExternalEntityName:(NSString *)name systemID:(NSString *)systemID {
    NSLog(@"resolveExternalEntityName %@ %@", name, systemID);
    return nil;
}

- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError {
    NSLog(@"Error %@", parseError);
}

&n;未解决。只是空白。我做错了什么?

解析输出:

2014-02-15 01:09:16.579 TestNSXMLParser[76939:303] foundInternalEntityDeclarationWithName: n = noun
2014-02-15 01:09:16.580 TestNSXMLParser[76939:303] <JMdict>
2014-02-15 01:09:16.581 TestNSXMLParser[76939:303] <entry>
2014-02-15 01:09:16.581 TestNSXMLParser[76939:303] <sense>
2014-02-15 01:09:16.582 TestNSXMLParser[76939:303] <pos>
2014-02-15 01:09:16.582 TestNSXMLParser[76939:303] </pos>
2014-02-15 01:09:16.582 TestNSXMLParser[76939:303] <gloss>
2014-02-15 01:09:16.583 TestNSXMLParser[76939:303]  love
2014-02-15 01:09:16.583 TestNSXMLParser[76939:303] </gloss>
2014-02-15 01:09:16.583 TestNSXMLParser[76939:303] </sense>
2014-02-15 01:09:16.584 TestNSXMLParser[76939:303] </entry>
2014-02-15 01:09:16.584 TestNSXMLParser[76939:303] </JMdict>

1 个答案:

答案 0 :(得分:0)

你的实体定义的值(“名词”)周围有反斜杠“\”的原因吗?

<!ENTITY n \"noun\">]

内部实体声明的XML语法如下:

<!ENTITY entity-name "entity-value">

从“名词”周围删除反斜杠,然后重试。