使用 jQuery 解析 XML 搜索结果页

Parse XML search results page with jQuery

本文关键字:搜索结果 XML 解析 jQuery 使用      更新时间:2023-09-26

>Background:

我正在

尝试在我正在创建的页面中包含 Google 搜索结果。这些搜索结果的格式为 XML。

目前,我正在像这样导入XML:

if (window.XMLHttpRequest) {
    // Code for Internet Explorer7+, Firefox, Chrome, Opera, and Safari
    xmlhttp = new XMLHttpRequest();
}
else {
    // Code for Internet Explorer 6 and Internet Explorer 5
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET", "foo", false);
xmlhttp.send();
xmlDoc = xmlhttp.responseXML;

其中foo是保存搜索结果的 XML 文件的 URL。

问题:

这非常有效,但前提是foo以适当的扩展名结尾,例如 .xml .这会带来一个问题,因为包含搜索结果(http://search.domain.com/search?q=queryString&output=xml(的页面没有扩展名。

当我尝试使用搜索结果的 URL 调用xmlhttp.open()时,xmlhttp.send()失败,并且不会执行其后的任何函数或命令。

我已经尝试了几种不同的方法来使用 jQuery 和纯 JavaScript 导入/解析文件,但似乎都不起作用。

问题:

有没有办法只导入无扩展名文件的文本?然后,我将能够使用 parseFromString 解析文本。这将允许我获得我需要的数据,但只能通过将搜索结果中的所有文本(无论数量(复制到我的页面中。

我希望有一种方法可以允许我将无扩展名页面作为 XML 文件打开和解析。

如果您需要更多信息,请告诉我。

<小时 />

这是应该返回的代码:

<?xml version="1.0" encoding="ISO-8859-1" standalone="no"?>
<!DOCTYPE GSP SYSTEM "google.dtd">
<GSP VER="3.2">
  <TM>TimeTaken(in seconds)</TM>
  <Q>queryString</Q>
  <PARAM name="q" value="queryString" original_value="queryString"/>
  <PARAM name="output" value="xml" original_value="xml"/>
  <PARAM name="ie" value="UTF-8" original_value="UTF-8"/>
  <PARAM name="ulang" value="en" original_value="en"/>
  <PARAM name="ip" value="ipAddress" original_value="ipAddress"/>
  <PARAM name="access" value="p" original_value="p"/>
  <PARAM name="sort" value="date:D:L:d1" original_value="date:D:L:d1"/>
  <RES SN="1" EN="10">
    <M><!-- The estimated total number of results for the search -->3560</M>
    <FI/>
    <NB>
      <NU>/search?q=queryString&amp;lr=&amp;ie=UTF-8&amp;output=xml&amp;access=p&amp;sort=date:D:L:d1&amp;start=10&amp;sa=N</NU>
    </NB>
    <!-- First result -->
    <R N="1"> <U><!--URL of result-->http://www.google.com?option=42</U>
      <UE><!--URL of result with special characters changed to html
              equivalent-->http://www.google.com%3Foption%3D</UE>
      <T><!--Title of result -->Google </T>
      <RK><!--Query Ranking
              10(highest relevance)-1(lowest relevance)-->10</RK>
      <ENT_SOURCE> <!--Identifies the application ID (serial number) of the
                      search appliance that contributes to a result.-->
        S5-KUB000F0ADETLA </ENT_SOURCE>
      <FS NAME="date" VALUE=""/>
      <S><!-- Snippet for the search result --> Search the world's information,
      including webpages, images, videos and more. <em>Google</em> has many
      special features to help you find exactly what you're looking&nbsp;
      <b>...</b></S><LANG>en</LANG>
      <HAS><!--special features that are included for this search result-->
        <L/>
        <C SZ="30k" CID="TiXnj_p8qlgJ" ENC="ISO-8859-1"/>
      </HAS>
    </R>
  </RES>
</GSP>

现在我实际上仔细看了一下,谷歌在使用&output=xml时向我承诺的XML代码看起来并不那么"xml-ey"。有没有办法解决这个问题,或者我将不得不放弃我的项目?

我能够让同样的"XML"与 php 解析器很好地工作,但我被要求将所有内容更改为 JavaScript 而不是 PHP。

由于您已经在使用 jQuery,只需使用其 .ajax 方法,该方法可以在 dataType 选项设置为"xml"时自动解析 XML。

jQuery.ajax({
   url:"http://example.com/someurl",
   dataType:"xml",
   success:function(xml) {
      //xml will be an object which you can use to access the elements.
   }
});

如果正在发送正在检索的文件的内容类型,例如text/html,您可以将dataType更改为'text xml',以便jQuery知道以XML形式查看文本。

但是,如果你想继续使用vanilla JavaScript来执行Ajax请求,则必须获取.responseText而不是.responseXML,然后解析

var xmlDoc = new DOMParser().parseFromString(xmlhttp.responseText,'text/xml');

请注意,DOMParser并不完全受支持,例如低于9的Internet Explorer不支持它。