如何解析XML数据而不关闭标签Objective-C

时间:2012-10-28 18:01:25

标签: objective-c ios xml cocoa-touch nsxmlparser

  

可能重复:
  Using an NSXMLParser to parse 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文件。谢谢!

2 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分:1)

它看起来像你甚至没有找到你感兴趣的主要数据块,可能是因为HEAD也是畸形的(虽然我不确定,因为该部分文件不是不包括在问题中。)

我建议在收到文件之后对其进行细微的调整,直到它正确处理为止。您不必修复每个错误,只是那些阻止您获取选项数据的错误。一旦知道了所需的修复,在收到文件并正常处理后,请进行一些自动替换/重新编译。