可以使用 NSXMLParser 解析 HTML [英] Can NSXMLParser Be Use To Parse HTML
问题描述
可能的重复:
使用 NSXMLParser 解析 HTML
我使用 NSXMLParser
来解析 xml 文件和 RSS 提要.我感到困惑的是 NSXMLParser
是否仅用于 xml 还是我们也可以使用它来解析 html.通过在 Internet 上的一些搜索,我假设有些人使用它来解析 html.
I have used NSXMLParser
to parse xml files and RSS feeds. What i am confused about is that whether NSXMLParser
is only for xml or can we use it to parse html as well. From a little searching on the Internet, i am assuming that some people use it for parsing html.
但是在 html 中使用 NSXMLParser
有什么限制或缺点吗?
But are there any limitations or disadvantages of using NSXMLParser
with html ?
推荐答案
如果你的 HTML 文档是格式良好的 xhtml,那么它就可以工作.作为猜测,您不会使用格式良好的 xhtml,因为它在现实世界中很少见.
If you HTML document is well formed xhtml, then it will work. As a guess, you will not be working with well formed xhtml, as it's rare in the real world.
HTML(包括 HTML 4 和 5)不是格式良好的 XML,不会被 XML 解析器成功解析.
HTML (including HTML 4 and 5) is not well formed XML and will not be successfully parsed by an XML parser.
考虑以下示例:
<HTML>
<HEAD>
<META http-equiv=content-type content="text/html; charset=UTF-8">
<TITLE>Sample Document</TITLE>
</HEAD>
<BODY>
<H1>Sample Document</h1>
<P>This document will <strong><em>fail</strong></em> as XML.
</BODY>
</HTML>
在上面的文档中,content-type没有在引号中(),
和
</h1>
是不同的情况, 没有结束标记,而
strong
和 em
没有正确嵌套.这是有效的 HTML,但无效的 XML.
In the above document, content-type is not in quotes (<META http-equiv=content-type …
), <H1>
and </h1>
are different cases, <P>
does not have an end tag, and strong
and em
are not nested correctly. This is valid HTML but invalid XML.
这篇关于可以使用 NSXMLParser 解析 HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!