可能重复:
使用 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);
但我没有看到OPTION
或VALUE
任何地方。这是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 文件。谢谢!