Google Scripts: XML parsing errors - javascript

I have a google script that locates a specific .zip folder on a server, extracts the files, and takes a specific .xml file to be processed. My problem is getting this file into the proper format.
The applicable snippet of code:
var dir = UrlFetchApp.fetch(url);
var b = dir.getBlob();
var files = Utilities.unzip(b);
var vesselDataBlob;
for (var i = 0; i < files.length; i++) {
if (files[i].getName().equals("dat/vesselDataMod.xml")) { //finds file with appropriate name
vesselDataBlob = files[i];
break;
}
}
var vesselData = vesselDataBlob.getDataAsString(); // Returns FULL document as a string.
var data = XmlService.parse(vesselData); // Throws error.
vesselData is in xml format, and vesselData.getContentType() returns "text/xml".
However, I'm struggling to find a way to parse the data. XmlService.parse(vesselData) throws an error: "Content is not allowed in prolog." I tried using DOMParser, which also throws an error. Is there something wrong with how I've set up my code? Is the data not actually in xml format?
The obvious difference between what most people probably do and my situation is that I'm pulling a file from a zipped folder, instead of just straight from a website. That's not the problem, I've tried just using a xml file uploaded to Drive, and the same problem occurs.
I can set up string manipulation to get the data I need, but I'd rather not go through the effort if someone can help out. Thanks!
I've been using this snippet of xml for debugging:
<?xml version="1.0" encoding="UTF-8"?>
<vessel_data version="2.1">
<hullRace ID="0" name="TNS" keys="player">
<taunt immunity="Yadayada" text="More yadayada"/>
</hullRace>
</vessel_data>

The following function works for me with a very simple zip file. I recommend that you try getDataAsString("UTF-8") and see if that resolves the issue.
function test() {
var f = DriveApp.getFilesByName("ingest.zip").next();
var files = Utilities.unzip(f.getBlob());
for(var i=0; i<files.length; i++) {
var ff = files[i];
if (/\.xml$/.test(ff.getName())){
var s = XmlService.parse(ff.getDataAsString());
Logger.log(s);
s = XmlService.parse(ff.getDataAsString("UTF-8"));
Logger.log(s);
break;
}
}
}

I put your XML file into a gist (as XML, not zip) and it parses.
function test2() {
var f = UrlFetchApp.fetch("...gisturl.../test.xml").getBlob(
);
var s = XmlService.parse(f.getDataAsString());
Logger.log(s.getDescendants().length);
}
Unfortunately, I am now having trouble getting Utilities.unzip() to run on a zip file uploaded to Google Drive. Hopefully another user will give you a better solution.

Related

read local file in phantomjs

I'm attempting to write a script which is able to take a screenshot given an url using phantom.js. This works great when I supply the url(s). However, I would like to be able to read urls from a file in much the same way as in:
here and here. However, all methods return with
unable to open file 'filename.txt'
Am I missing something here?
Working from a win7 installation.
EDIT:
var fs = require('fs');
var file_h = fs.open('urls.txt', 'r');
var line = file_h.readLine();
while(line) {
console.log(line);
line = file_h.readLine();
}
file_h.close();
The output I get is:
unable to open file 'urls.txt'
phantomjs://platform/fs.js:79 in open

Write data to a local file by javascript/AngularJs

I'm beginner to the JS development.what I want to do is write the data to a file in local storage.
In my application i read a raw data and save it in mongoDB as key value pairs.same time i want to write those data to a file in a local storage.
this code is used to read the read the file line by line.i get structured data in "event" .what i want to do is write those data to a file in local storage.
var lines = readDetails.split('\n');
for(var line = 0; line < lines.length-1; line++){
var FileContent = "";
var linesSpace = lines[line].split(' ');
for(var y=3;y <= linesSpace.length-1;y++){
FileContent +=linesSpace[y];
FileContent += " ";
}
var event = {
dat: linesSpace[0],
tim: linesSpace[1],
details: FileContent,
};
}
if this is not that much clear .please questioned me.
Thanks.
ngStorage is the best I've found, super easy to use.

pdf.js failing on getDocument

browser: Chrome
environment: grails app localhost
I'm running a grails app on local host (which i know there's an issue with pdf.js and local file system) and instead of using a file: url which i know would fail i'm passing in a typed javascript array and it's still failing. To be correct it's not telling me anything but "Warning: Setting up fake worker." and then it does nothing.
this.base64ToBinary = function(dataURI) {
var BASE64_MARKER = ';base64,';
var base64Index = dataURI.indexOf(BASE64_MARKER) + BASE64_MARKER.length;
var base64 = dataURI.substring(base64Index);
var raw = window.atob(base64);
var rawLength = raw.length;
var array = new Uint8Array(new ArrayBuffer(rawLength));
for(i = 0; i < rawLength; i++) {
array[i] = raw.charCodeAt(i);
}
return array;
};
PDFJS.disableWorker = true; // due to CORS
// I convert some base64 data to binary data here which comes back correctly
var data = utilities.base64ToBinary(result);
PDFJS.getDocument(data).then(function (pdf) {
//nothing console logs or reaches here
console.log(pdf);
}).catch(function(error){
//no error message is logged either
console.log("Error occurred", error);
});
I'm wondering if I just don't have it set up correctly? Can I use this library purely on the client side by just including pdf.js or do I need to include viewer.js too? and also i noticed compatibility file... the set up isn't very clear and this example works FIDDLE and mine doesn't and I'm not understanding the difference. Also if I use the url supplied in that example it also says the same thing.
I get to answer my own question:
the documentation isn't clear at all. If you don't define PDFJS.workerSrc to point to the correct pdf.worker.js file than in pdf.js it tries to figure out what the correct src path is to the file and load it.
Their method however is pretty sketchy for doing this:
if (!PDFJS.workerSrc && typeof document !== 'undefined') {
// workerSrc is not set -- using last script url to define default location
PDFJS.workerSrc = (function () {
'use strict';
var scriptTagContainer = document.body ||
document.getElementsByTagName('head')[0];
var pdfjsSrc = scriptTagContainer.lastChild.src;
return pdfjsSrc && pdfjsSrc.replace(/\.js$/i, '.worker.js');
})();
}
They only grab the last script tag in the head and assume that that is the right src to load the file instead of searching all the script tags for the src that contains "pdf.js" and using that as the correct one.
Instead they should just make it clear and require that you do in fact point PDFJS.workerSrc = "(your path)/pdf.worker.js"
Here is the short answer : define PDFJS.workerSrc at the begining of your code.
PDFJS.workerSrc = "(your path)/pdf.worker.js"
see the exemple on the documentation : https://mozilla.github.io/pdf.js/examples/#interactive-examples

Displaying .dcm files with XTK's X.volume()

According to the lesson 15 I pass .dcm file to the volume. I get no errors (e.g. no parsing errors I faced before) but nothing is displayed. What could be wrong with the .dcm files?
Here is a excerpt of the code I use:
function demo(file){
var _dicom = [file];
var r = new X.renderer3D();
r.init();
var v = new X.volume();
v.file = _dicom.map(function(v) {
return file;
});
r.add(v);
r.render();
r.onShowtime = function() {
v.volumeRendering = true;
};
};
I pass the full file path here. Well, I'm not sure it's a correct wording, but I'd lile to know, what dicom settings or parameters could cause such a behaviour, no errors and no diplayed data. Thanks in advance.
Did you try to drag the .dcm file into http://slicedrop.com and see the same effect? DICOM support currently only exists for uncompressed DICOM files and we didn't get Ultrasound too work yet. So on uncompressed MR data or CT data in DICOM it should work.

JavaScript: Read files in folder

EDIT: I'm trying to read all the files in a specific folder and list the files in there, not read the content of a specific file. I just tried to simply create an FileSystemObject and it doesn't do anything either. I show an alert (which pops up) beforfe making the FileSystemObject, and one after it (which isn't shown). So the problem is in simply creating the object.
Original:
I am trying to read all the files in a folder by using JavaScript.
It is a local HTML file, and it will not be on a server, so I can't use PHP I guess.
Now I'm trying to read all the files in a specific given folder, but it doesn't do anything on the point I make a FileSystemObject
Here is the code I use, The alert shows until 2, then it stops.
alert('1');
var myObject, afolder, date;
alert('2');
myObject = new ActiveXObject("Scripting.FileSystemObject");
alert('3');
afolder = myObject.GetFolder("c:\\tmp");
alert('4');
date = afolder.DateLastAccessed;
alert("The folder"+name+" is a temporary folder.");
Am I doing this the right way?
Thanks!
The method I found with a Google search uses HTML5 so if you are using a modern browser you should be good. Also the tutorial page seems to check if the browser you are using supports the features. If so you should be good to follow the tutorial which seems pretty thorough.
http://www.html5rocks.com/en/tutorials/file/dndfiles/
This solution only works on IE11 or older since it is MS based
<script type="text/javascript">
var fso = new ActiveXObject("Scripting.FileSystemObject");
function showFolderFileList(folderspec) {
var s = "";
var f = fso.GetFolder(folderspec);
// recurse subfolders
var subfolders = new Enumerator(f.SubFolders);
for(; !subfolders.atEnd(); subfolders.moveNext()) {
s += ShowFolderFileList((subfolders.item()).path);
}
// display all file path names.
var fc = new Enumerator(f.files);
for (; !fc.atEnd(); fc.moveNext()) {
s += fc.item() + "<br>";
}
return s;
}
function listFiles() {
document.getElementById('files').innerHTML = showFolderFileList('C:');
}
</script>
<input type='button' onclick='listFiles()' value='List Files' />
<div id="files" />

Categories

Resources