2012-02-27 72 views
0

我一直在嘗試將XML feed轉換爲人類可讀形式,但沒有成功。我曾嘗試看到在tutorails部分給出的例子,但它太複雜。請有人可以幫我解決問題。我可以做得很好,直到解析XML。使用谷歌腳本解析XML數據

我使用谷歌腳本,輸出必須在谷歌文檔。

這裏是我想出了到現在爲止

var response = UrlFetchApp.fetch("http://getRecords.php?oauth_token=3e73c7&lat="+lat+"&lon="+lon+"&searchFor="+text+"&miles=100&response_type=xml"); 

    var doc = Xml.parse(response.getContentText(), true) 
var root = doc.getElement(); 
var entryList = "<ul>\n"; 

var entries = root.getElements("details"); 

    for (var i=0; i<entries.length; i++) { 
    var e = entries[i]; 
    var name = e.getElement("name").getText(); 

    } 



    entryList += "<li>name </li>" + name; 


    entryList += "</ul>\n"; 
    return entryList; 

這裏是XML

<record> 
<details> 
<id>212929</id> 
<distance>0</distance> 
<category cat="8" sub="202" id="1201">General</category> 
<name>Text Book Center</name> 
<short_desc>One of Kenya's finest</short_desc> 
<long_desc> 
One of Kenya's leading bookshops, the Text Book Center offers a wide selection of titles. There is everything here from textbooks to fiction to the latest Information Technology titles. The range of maps is especially impressive. The shop is spacious and cool, giving shoppers plenty of room to browse the shelves upon shelves of books. Look out for the regular special offers. 
</long_desc> 
<address> 
<address1>Kijabe Street</address1> 
<city>Nairobi</city> 
<country>Kenya</country> 
<latitude>0</latitude> 
<longitude>0</longitude> 
</address> 
<neighborhood>Downtown</neighborhood> 
<phone>+254 2 330 340</phone> 
<email>[email protected]</email> 
<open_hours>8am-1pm; 2pm-5.30pm Mon-Sat.</open_hours> 
</details> 
</record> 
</records> 

我如何刪除標籤和剛打印出來的文檔。請幫助

謝謝

+0

我認爲XML被設計爲人類可讀了。你想重新格式化它嗎? – 2012-02-27 15:25:08

+0

嗨,這是使用谷歌腳本和輸出必須在谷歌文檔。我需要打印出來沒有XML標籤。有人能指出我如何去感謝這個 – 2012-02-28 05:11:01

回答

1

看起來根節點開放標記丟失。它是原始文檔嗎?或者只是粘貼錯誤?

嘗試這樣

var response = UrlFetchApp.fetch("http://getRecords.php? oauth_token=3e73c7&lat="+lat+"&lon="+lon+"&searchFor="+text+"&miles=100&response_type=xml"); 
var doc = Xml.parse(response.getContentText(), true); 
var records = doc.records.getElements("record"); 
var entryList = "<ul>\n"; 

for (var i=0; i < records.length; i++) { 
    var details = records[i].details; 
    var name = details.name.getText(); 
    entryList += "<li>" + name + "</li>\n"; 
} 

entryList += "</ul>\n"; 
return entryList; 
+0

謝謝。它的一個粘貼錯誤 – 2012-03-01 04:55:39

+0

是我的解決方案回答你的問題?那麼你可以接受爲答案。 – arunes 2012-03-01 06:03:04

+0

no arunes我getErrors(「記錄」)它顯示TypeError:無法找到對象XmlDocument函數getElements。 (第14行) – 2012-03-01 09:37:13