Xpath和HTML Cleaner问题,没有返回数据

时间:2010-08-15 15:39:04

标签: android xml url xpath html-parsing

社区新手。整夜都试图充实我的应用程序功能核心的底层html阅读系统。我真的可以用一双新眼睛。

问题:在尝试返回要在我的应用程序的主页活动中显示的字符串时,我遇到了一个问题,我几乎可以肯定数据是正确的,通过“Html Cleaner”清理成XML (http://htmlcleaner.sourceforge.net/),并通过Jaxen(opensource Xpath)结果应显示一些文本。当然,问题是我的努力,我还没弄清楚为什么它不会。我的代码如下。

作为测试,我试图从http://www.google.com主页中提取“地图”一词,该主页位于带有超链接“http://maps.google.com/maps?hl=en&tab=wl”的标签内(我用它来唯一地识别标签):

public class home extends Activity {

  TextView text1;


  //** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState)
  {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.main);

   text1 = (TextView)findViewById(R.id.text1);
   text1.setText(LoadHTMLFromURL("http://www.google.com"));
  }



  private String LoadHTMLFromURL(String url)
  {
   try
   {
    // Load data from URL     
     InputStream is = (InputStream) new URL(url).getContent(); //generate
     BufferedReader reader = new BufferedReader(new InputStreamReader(is));
     StringBuilder stringBuilder = new StringBuilder();
     String line = null;

     while ((line = reader.readLine()) != null) 
     {
      stringBuilder.append(line + "");
     }
     is.close();

     String HTMLout = stringBuilder.toString();

     // Clean up HTML input.
     //Initialize HTML Cleaner.
     HtmlCleaner cleaner = new HtmlCleaner();

     // This next line Cleans the html and exports it to a Tagnode named "node"
     TagNode node = cleaner.clean(HTMLout);

     // This is the xpath parsing info
     String SearchTerm = "//a[@href='http://maps.google.com/maps?hl=en&tab=wl']";


     Object[] info_nodes = node.evaluateXPath(SearchTerm);

     TagNode info_node = (TagNode) info_nodes[0];
              String info = info_node.getChildren().iterator().next().toString().trim();

              return info;
   }

   catch (Exception e) 
   {
    System.out.println( "Inside: home.LoadHTMLFromURL()" + "Exc="+e);
    return null;
   }

  }
 }

我为这个杂乱的代码道歉,并且代码缺乏整洁,仍然是一个中低档程序员,在我的能力的“随时随地学习”阶段。任何建议都表示赞赏。

旁注:我运行了一个字符串,其中包含一些手工制作的简单XML,以测试它是否会读取信息,并且它可以完美地工作,但不能用于从html网页生成的xml。

1 个答案:

答案 0 :(得分:0)

好的,我相信这个问题是我的搜索词。我的xpath术语键入错误。