如何使用 NSXMLParser 获取 DTD 的公共 ID 和系统 ID
我正在尝试通过 NSXMLParser 检索 XML 文档中 DTD 的公共 ID 和系统 ID。虽然 NSXMLParser
主要提供 publicID
和 systemID
选择器,但它们似乎不适合我。 doctype 标记如下所示:
<!DOCTYPE Article PUBLIC "-//SoftQuad Software//DTD Journalist v2.0 20000501//EN" "file:///C:/Program%20Files/Corel/XMetaL%204/Author/Rules/journalist.dtd">
这是我的代码(该文件是通过 NSFileHandle
的 readDataToEndOfFile
打开的:
NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data];
[parser setDelegate:self];
BOOL parseSuccessful = [parser parse];
在委托的 parserDidStartDocument:
中,我尝试访问ID:
NSLog(@"%@ : %@", [parser publicID], [parser systemID]);
看到
(null) : (null)
但我只从文档中
:一旦解析操作开始或发生错误后,您可以调用此方法。
所以我认为这应该已经在 parserDidStartDocument:
中工作,但我尝试在不同的委托方法中调用这些选择器(例如 parser:didStartElement:namespaceURI:qualifiedName:attributes:
但是没有成功。
有什么想法我做错了吗?
I'm trying to retrieve the public and system IDs for a DTD in an XML document via NSXMLParser
. While NSXMLParser
in principal offers publicID
and systemID
selectors they don't seem to work for me. The doctype tag looks like this:
<!DOCTYPE Article PUBLIC "-//SoftQuad Software//DTD Journalist v2.0 20000501//EN" "file:///C:/Program%20Files/Corel/XMetaL%204/Author/Rules/journalist.dtd">
Here's my code (the file was opened via NSFileHandle
's readDataToEndOfFile
:
NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data];
[parser setDelegate:self];
BOOL parseSuccessful = [parser parse];
In the delegate's parserDidStartDocument:
I try to access the IDs:
NSLog(@"%@ : %@", [parser publicID], [parser systemID]);
But I only see
(null) : (null)
From the documentation:
You may invoke this method once a parsing operation has begun or after an error occurs.
So I'd think this should work already in parserDidStartDocument:
but I tried to call these selectors in different delegate methods (like parser:didStartElement:namespaceURI:qualifiedName:attributes:
but without success.
Any ideas what I'm doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以尝试另一个解析器。
http://www.robbiehanson.com/expat.html 提供了一个直接替代基于 expat 的 NSXMLParser。
您可能还会发现这篇文章很有趣,它比较了 iPhone 上各种 XML 解析器的性能。
http:// www.raywenderlich.com/553/how-to-chose-the-best-xml-parser-for-your-iphone-project
You might try out another parser.
http://www.robbiehanson.com/expat.html provides a drop-in replacement for NSXMLParser based on expat.
You might also find this article interesting, comparing the performance of various XML parsers on the iPhone.
http://www.raywenderlich.com/553/how-to-chose-the-best-xml-parser-for-your-iphone-project