xml2json.js error: Syntax error - javascript

I want to convert an XML file, after many trials with my AngularJS application, I've simply uploaded on the server the samples.html that I've found at https://github.com/abdmob/x2js .
The result is always the same: on the console I receive a "SCRIPTxxxx: Syntax error xml2json.min.js" and "SCRIPTxxxx: 'X2JS' is undefined". The script is linked normally, like:<script type="text/javascript" src="/vendor-scripts/xml2json.min.js"></script> (same issue with xml2json.js) and also this simple code gives the same error: var x2js = new X2JS();
var xmlText = "<MyRoot><test>Success</test><test2><item>val1</item><item>val2</item></test2></MyRoot>";
var jsonObj = x2js.xml_str2json( xmlText );. Does anyone know if this is a known issue (I didn't find anything on the net) or if there are other libraries to do this job?

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

how to get xml namespace in javascript

my xml is
<message to="to_test" from="from_test">
<test xmlns="google:mobile:data">
{"message":"test_message"}
</test>
</message>
i get the value {"message":"test_message"} by using the function getChildText("pcm"). But i tried to retrieve the namespace xmlns value.
I tried the following How to get XML namespace? , How to get specific XML namespace in XQuery in SQL Server with no luck, it shows me is not function error what i'm doing wrong?
I forgot to mention, I'm currently staring work with node.js.
update
the above xml output is xmpp stanza.
Here i'm getting the attrs using the following.
stanza.attrs.to gives me to_test.
stanza.attrs.from gives me from_test.
stanza.getChildText("test") gives me {"message":"test_message"}
I tried to get the xmlns using
var parser = new DOMParser();
var documents = parser.parseFromString(stanza, 'text/xml');
var response = documents.responseXML.getElementsByTagName("test");
var sunrise = response[0].getElementsByTagNameNS("[Namespace URI]", "test")[0].getAttribute("xmlns");
console.log(sunrise);
here i got
[xmldom error] element parse error: TypeError: source.indexOf is not a function ##[line:0,col:undefined]
Using the standard browser's DOM parser, you can do the following:
var txt = "<message><test xmlns=\"google:mobile:data\"> {\"message\":\"test_message\"}</test></message>";
parser = new DOMParser();
xmlDoc = parser.parseFromString(txt,"text/xml");
ns = xmlDoc.getElementsByTagName("test")[0].namespaceURI
I have tested with Chrome and IE and it works.
I hit a similar error -- in my case it was because I had passed something besides a string to "parseFromString". Could this be the problem? It looks like "stanza" is not a string.

External JavaScript file is not defined

For a web project, I've included a JavaScript file as a script src, as shown here.
<script src="xml2json.js"> //same directory as the web project
Next, I tried to invoke a method within xml2json, called xml_str2json.
downloadUrl("ship_track_ajax.php", function(data) {
var xml_string = data.responseText; //an XML string
//A parser to transform XML string into a JSON object is required.
//Use convert XML to JSON with xml2json.js
var markers = xml2json.xml_str2json(xml_string);
}
However, console log indicates "Uncaught ReferenceError: xml2json is not defined", even though xml2json is included as a script src. Can anyone tell me as to what is wrong?
You have to call the function directly in javascript without reffering the filename as like
xml_str2json(xml_string);
If the function is defined in any of the included file it will be invoked.
I hope this will solve your problem
Maybe you should try this:
var json = xml2json(parseXml(xml), " ");
See Demo from https://github.com/henrikingo/xml2json

Parse xml/HTML string with Parse.com

I need to parse an XML/HTML file within the cloud-code of parse.com.
I've got a string which contains the source-code of the html file.
I've already tried multiple frameworks like jsdom etc, but nothing seems to work in the Parse.com-cloudcode environment.
This code for example results in an error in the jsdom-file. But I've got no idea what the real problem is. Because the "<" ">" tags are set correctly in the jsdom.js-file.
var jsdom = require("cloud/jsdom.js");
var window = jsdom.jsdom().createWindow();
var jquery = require("cloud/jquery-1.11.2.min.js")(window);
var dataHtml = httpResponse.text;
response.success(jquery.$(dataHtml).find("body").text());
Error:
{"code":141,"error":"Error: Uncaught SyntaxError: Unexpected token \u003c in jsdom.js:5\n at Object.Parse.Cloud.httpRequest.success (main.js:9:21)
Is there another possibility to parse a string with XPath or dom in the parse.com-cloudcode?
This answer can be too late, but I just realized a bundle of cheerio module that works with Parse.
Example:
var cheerio = require('cloud/cheerio.bundle.js'),
$ = cheerio.load('<h2 class="title">Hello world</h2>');
$('h2.title').text('Hello there!');
$('h2').addClass('welcome');
$.html();
//=> <h2 class="title welcome">Hello there!</h2>
You can get it here.

Loading local XML in IE

Im trying to read a local xml file without using active-x in IE based on code from this site
var xml = document.createElement("xml");
xml.src = 'file.xml';
document.body.appendChild(xml);
var xmlDocument = xml.XMLDocument;
document.body.removeChild(xml);
console.log(xml.XMLDocument); //undefined
console.log(xml); //<xml XMLDocument="[object]">
What I need here is the [object] that contains my XML file. But I can't manage to fetch it (see above). Did I miss something obvious?
To anyone who runs in to this problem: The XMLDocument property doesn't exist in IE9, therefore causing the issue.

Categories

Resources