使用Google脚本解析XML数据 [英] Parsing XML data using google script

查看:83
本文介绍了使用Google脚本解析XML数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试将xml提要转换为人类可读的形式,但没有成功.我尝试查看tutorails部分中给出的示例,但是它太复杂了.请有人帮我做什么.在解析xml之前,我可以做的很好.

I have been trying to convert a xml feed into human readable form but have not succeeded . I have tried seeing the example given in tutorails section but its too complex. Please can someone help me out with what is to be done. I can do fine till parsing of xml.

我正在使用Google脚本,输出必须在Google文档中.

I am using google script and the output has to be in google docs.

这是我到目前为止想出的内容

here is what i came up with till now

    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>info@tbc.co.ke</email>
<open_hours>8am-1pm; 2pm-5.30pm Mon-Sat.</open_hours>
</details>
</record>
</records>

我如何删除标签,然后在文档中将其打印出来.请帮助

how do i remove the tags and just print it out in docs. Please help

谢谢

推荐答案

看起来缺少根节点开始标记.是原始文件吗?还是只是粘贴错误?

Looks root node opening tag is missing. Is it original doc? or just paste error?

尝试这样

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;

这篇关于使用Google脚本解析XML数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆