0

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.

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;  

Here is the 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>

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

thanks

2
  • I thought XML was designed to be human-readable already. Do you want to reformat it as something else? Commented Feb 27, 2012 at 15:25
  • Hi this is using google script and the output has to be in google docs. I need to print it out without the xml tags. Can some one point me out how to go about this Commented Feb 28, 2012 at 5:11

1 Answer 1

1

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

Try like this

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;
Sign up to request clarification or add additional context in comments.

5 Comments

is my solution answered your question? then you can accept as an answered.
no arunes i am getting eror on getEleements("record") it showing TypeError: Cannot find function getElements in object XmlDocument. (line 14)
I tried this code on your xml and worked. Did you replace all my codes over yours? Check xml address? maybe somethink wrong with it.
yes it works not sure whatt i was doing wrong. I pased your code and it works now. Thanks. Can you please point out my error if possible so I can learn my mistake
You tried get root node doc.getElement(), but doc.records gets root node. And after that there is record node you skipped that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.