如何使用 NSXMLParser 获取 DTD 的公共 ID 和系统 ID

发布于 2024-09-14 05:49:31 字数 1061 浏览 2 评论 0原文

我正在尝试通过 NSXMLParser 检索 XML 文档中 DTD 的公共 ID 和系统 ID。虽然 NSXMLParser 主要提供 publicIDsystemID 选择器,但它们似乎不适合我。 doctype 标记如下所示:

<!DOCTYPE Article PUBLIC "-//SoftQuad Software//DTD Journalist v2.0 20000501//EN" "file:///C:/Program%20Files/Corel/XMetaL%204/Author/Rules/journalist.dtd">

这是我的代码(该文件是通过 NSFileHandlereadDataToEndOfFile 打开的:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

灯下孤影 2024-09-21 05:49:31

您可以尝试另一个解析器。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文