1

これがデータ ストリームです (これが正しく表示されていることを願っています。理解するのに永遠にかかりました)。タイトルを抽出して、機能させることができるかどうかを確認しようとしていました。私は明らかに道に迷っており、なぜエラーが発生し続けるのか理解できません。

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
    <items total="475974">
        <link href="https://www.sciencebase.gov/catalog/items?
            max=1&amp;s=Search&amp;q=water&amp;format=xml" 
            rel="self" />
        <link href="https://www.sciencebase.gov/catalog/items?
            max=1&amp;s=Search&amp;q=water&amp;format=xml&amp;offset=1"
            rel="next" />
        <item id="4f4e4a62e4b07f02db6369dc">
        <link href="https://www.sciencebase.gov/catalog/
                item/4f4e4a62e4b07f02db6369dc" rel="self" />
        <oldId>1800335</oldId>
        <title>Reconnaissance of the water resources of Beaver County,
            Oklahoma</title>
        <summary>Ground water is the major source of water supply in Beaver
            County. Because of the rapidly increasing demand for the limited
            supply of water for irrigation, additional geologic and hydrologic
            data are needed for management of ground-water resources...</summary>
        </item>
    </items>

そして、これは私のコードです:

function myFunction() {
    var response = UrlFetchApp.fetch("http://www.sciencebase.gov/catalog        
        /items?s=Search&q=water&format=xml&max=1").getContentText();
    var parsedResponse = Xml.parse(response, true);
    //I tried this but it didn't work
    var title = parsedResponse.html.head.getElements("title");
    Browser.msgBox(title);
}
4

1 に答える 1

2

私は常にツールを使用して XML を調べます。必要な場合は、Chrome でXML ツリー拡張機能を使用します。それをインストールし、テスト URL にアクセスすると、(IE のように) 展開および折りたたみ可能なフォーマットされた XML が表示され、構造を簡単に確認できます。

いくつかの改造と軽いナレーションを含むコードは次のとおりです。

function myFunction() {
  //note I've set max=10 here
  var url="http://www.sciencebase.gov/catalog/items?s=Search&q=water&format=xml&max=10";
  var response = UrlFetchApp.fetch(url).getContentText();

  var parsedResponse = Xml.parse(response, true);

  debugger; //You will see in the debugger that your 10 results are each XmlElements in the array parsedResponse.items.item[]

  //iterate through the results
  for (var i = 0; i < parsedResponse.items.item.length; i++) {
    var result = parsedResponse.items.item[i];
    //View > Logs... to see the title of each result
    Logger.log(result.getElement("title").getText());
  }
}

次の 10 タイトルを記録します。

Reconnaissance of the water resources of Beaver County, Oklahoma
..
Water resources data, Oklahoma, water year 2004; Volume 2. Red River basin

これがお役に立てば幸いです。さらに明確にする必要がある場合はお知らせください。

于 2012-06-15T00:53:05.643 に答える