1

可能重复:
使用 NSXMLParser 解析 HTML

我正在尝试解析以下 XML 数据,但结构混乱并且没有结束标记。这不是我制作的 XML 文件,而是我试图从网络服务器解析的文件。

    <FORM ACTION="/prod/bwckgens.p_proc_term_date" METHOD="POST" onSubmit="return checkSubmit()">
<INPUT TYPE="hidden" NAME="p_calling_proc" VALUE="bwckschd.p_disp_dyn_sched">
<TABLE  CLASS="dataentrytable" summary="This layout table is used for term selection."width="100%"><CAPTION class="captiontext">Search by Term: </CAPTION>
<TR>
<TD CLASS="dedefault"><LABEL for=term_input_id><SPAN class="fieldlabeltextinvisible">Term</SPAN></LABEL>
<SELECT NAME="p_term" SIZE="1"  ID="term_input_id">
<OPTION VALUE="">None
<OPTION VALUE="201320">Spring 2013
<OPTION VALUE="201315">STAR/BGR: New Admits Fall 2012 (View only)
<OPTION VALUE="201310">Fall 2012 (View only)
<OPTION VALUE="201230">Summer 2012 (View only)
<OPTION VALUE="201220">Spring 2012 (View only)
<OPTION VALUE="201210">Fall 2011 (View only)
<OPTION VALUE="201130">Summer 2011 (View only)
<OPTION VALUE="201120">Spring 2011 (View only)
<OPTION VALUE="201110">Fall 2010 (View only)
<OPTION VALUE="201030">Summer 2010 (View only)
<OPTION VALUE="201020">Spring 2010 (View only)
<OPTION VALUE="201010">Fall 2009 (View only)
<OPTION VALUE="200930">Summer 2009 (View only)
<OPTION VALUE="200920">Spring 2009 (View only)
<OPTION VALUE="200910">Fall 2008 (View only)
<OPTION VALUE="200830">Summer 2008 (View only)
<OPTION VALUE="200820">Spring 2008 (View only)
</SELECT>
</TD>
</TR>
</TABLE>
<BR>
<BR>
<INPUT TYPE="submit" VALUE="Submit">
<INPUT TYPE="reset" VALUE="Reset">
</FORM>

HTML 文件还有很多内容,但我只包括相关的内容。我想得到OPTION VALUE="these numbers"括号后面的所有数字和术语。例如 2013 年春季。

NSXMLParser由于没有结束标签,我如何使用来获取这些值。我尝试打印出解析器遇到的所有元素

NSLog(@"Current start element: %@\n", elementName);
NSLog(@"Current attr:%@\n", attributeDict.description);

但我没有看到OPTIONVALUE任何地方。这是NSLog语句的结果:

2012-10-28 13:58:47.638 Purdue Course Finder[32890:c07] Current start element: HTML
2012-10-28 13:58:47.638 Purdue Course Finder[32890:c07] Current attr:{
    lang = en;
}
2012-10-28 13:58:47.639 Purdue Course Finder[32890:c07] Current start element: HEAD
2012-10-28 13:58:47.639 Purdue Course Finder[32890:c07] Current attr:{
}
2012-10-28 13:58:47.639 Purdue Course Finder[32890:c07] Current start element: META
2012-10-28 13:58:47.640 Purdue Course Finder[32890:c07] Current attr:{
    content = "text/html; charset=UTF-8";
    "http-equiv" = "Content-Type";
}
2012-10-28 13:58:47.640 Purdue Course Finder[32890:c07] Current start element: META
2012-10-28 13:58:47.640 Purdue Course Finder[32890:c07] Current attr:{
    CONTENT = "no-cache";
    "HTTP-EQUIV" = Pragma;
    NAME = "Cache-Control";
}
2012-10-28 13:58:47.641 Purdue Course Finder[32890:c07] Current start element: META
2012-10-28 13:58:47.641 Purdue Course Finder[32890:c07] Current attr:{
    CONTENT = "no-cache";
    "HTTP-EQUIV" = "Cache-Control";
    NAME = "Cache-Control";
}
2012-10-28 13:58:47.641 Purdue Course Finder[32890:c07] Current start element: LINK
2012-10-28 13:58:47.642 Purdue Course Finder[32890:c07] Current attr:{
    HREF = "/css/web_defaultapp.css";
    REL = stylesheet;
    TYPE = "text/css";
}
2012-10-28 13:58:47.642 Purdue Course Finder[32890:c07] Current start element: LINK
2012-10-28 13:58:47.642 Purdue Course Finder[32890:c07] Current attr:{
    HREF = "/css/web_defaultprint.css";
    REL = stylesheet;
    TYPE = "text/css";
    media = print;
}
2012-10-28 13:58:47.643 Purdue Course Finder[32890:c07] Current start element: TITLE
2012-10-28 13:58:47.643 Purdue Course Finder[32890:c07] Current attr:{
}
2012-10-28 13:58:47.643 Purdue Course Finder[32890:c07] Current end element: TITLE
2012-10-28 13:58:47.644 Purdue Course Finder[32890:c07] Current start element: META
2012-10-28 13:58:47.644 Purdue Course Finder[32890:c07] Current attr:{
    CONTENT = "text/javascript";
    "HTTP-EQUIV" = "Content-Script-Type";
    NAME = "Default_Script_Language";
}
2012-10-28 13:58:47.644 Purdue Course Finder[32890:c07] Current start element: SCRIPT
2012-10-28 13:58:47.645 Purdue Course Finder[32890:c07] Current attr:{
    LANGUAGE = JavaScript;
    TYPE = "text/javascript";
}
2012-10-28 13:58:47.645 Purdue Course Finder[32890:c07] Current end element: SCRIPT
2012-10-28 13:58:47.645 Purdue Course Finder[32890:c07] Current start element: SCRIPT
2012-10-28 13:58:47.646 Purdue Course Finder[32890:c07] Current attr:{
    LANGUAGE = JavaScript;
    TYPE = "text/javascript";
}
2012-10-28 13:58:47.646 Purdue Course Finder[32890:c07] Current end element: SCRIPT

我什至尝试在该- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string方法中到处打印。它在任何地方都找不到这些标签。我想知道是否有人可以帮助我解析这个构造不佳的 XML 文件。谢谢!

4

2 回答 2

2

对于特定的混乱文件,我个人使用“手动”解析,使用子字符串、索引和正则表达式。

您还可以尝试在解析文件之前“修复”文件,例如</option>在每个<option>.

于 2012-10-28T18:03:35.330 回答
1

在我看来,您甚至没有获得您感兴趣的主要数据块,可能是因为 HEAD 格式也错误(尽管我不确定,因为文档的那部分不包括在内在问题中)。

我建议在收到文档后对文档进行细微的调整,直到正确处理为止。您不必修复所有错误,只需修复那些阻止您获取选项数据的错误。一旦您知道所需的修复程序,请在收到文件并正常处理后进行一些自动替换/正则表达式。

于 2012-10-28T18:22:14.503 回答