XMLSerializer serializeToString escape characters >< - javascript

I have a XML file which i want to convert to string in java script code. It works fine except one thing.The XML file has on line like below,
.....other lines....
< Source Data>< /Source Data>
.....other lines....
It shows other lines perfectly but for < Source Data>< /Source Data> lines it displays only < Source Data/> in console log. < Source Data> is omitted somehow. I am using below code to display ,
var xmlString = (new XMLSerializer()).serializeToString(xmlFile);
console.log(xmlString)
Please explain me why it is happening and how to resolve this issue to display properly. please note, there is no space after less then character "<". i am putting as it was not displaying here properly without that
Thanks in advance

Related

error on line 3 at column 63: Space required after the Public Identifier when parsing HTML to XML

For a chrome project I got thrown into, I have to parse HTML into XML. But I keep getting the error:
error on line 3 at column 63: Space required after the Public Identifier
The code that throws me this code:
var html1 = httpGet(messages);
var html2 = html1.toString();
parser = new DOMParser();
var test = ["<tr><td>test</td><td>test</td><td>test</td></tr>"];
xmlDoc = parser.parseFromString(html2,"text/xml");
console.log(xmlDoc);
I tried to find it in Google but I could not find anything that would make me any wiser.
I did find out however that when I use this simple array, instead of html2 in the parseFromString it works perfectly fine.
var test = ["<tr><td>test</td><td>test</td><td>test</td></tr>"];
html2 variable contains a full HTML page. I tried to convert it to a string but that didn't seem to help at all.
I only have basic knowledge about Javascript so I'm hoping you guys can make it a bit clearer for me.
Thanks in advance!
Changed:
htmlDoc = parser.parseFromString(html1,"text/xml");
to:
htmlDoc = parser.parseFromString(html1,"text/html");
Thanks to Tomalak, who told me HTML and XML are not compatible so cant be parsed.

Getting "Uncaught SyntaxError: Unexpected token ;" error while using for loop

Here I have a loop to iterator over html elements in a JSF application.
var tmp = document.getElementsByTagName("input");
var counter = 0;
var arr = new Array();
for (var i = 0; i<tmp.length; i++) {
if (tmp[i].parentNode.nodeName === "TD" && tmp[i].getAttribute("type") !== "submit") {
arr[counter] = tmp[i];
counter++;
}
}
This loop is inside this script tag:
<script type="text/javascript">
<!--
//-->
</script>
When I load the page in the web browser I get following error(shown in google chrome developer tools)
As you can see in your screenshot, your script in the HTML page has < where it should have <. This is your syntax error. Are you generating this page with some templating language that "escapes" HTML code? That would cause this problem.
One good solution is to put your JavaScript code in a separate .js file. That should avoid the escaping and leave your code unchanged.
If you do need to have inline JavaScript right there in the HTML page, most templating languages have a way to turn off escaping for a specific piece of text.
The line in picture 2 isn't the same as the one in picture 1. < was substituted for <, which is the HTML escape code for <. The error subsequently does not lie within your code itself, but somewhere in your development workflow. You either saved the file with the wrong contents (which I presume to be unlikely) or your web server or any pre processor is messing up by HTML escaping code inside script-tags.

DOM Exception 5 INVALID CHARACTER error on valid base64 image string in javascript

I'm trying to decode a base64 string for an image back into binary so it can be downloaded and displayed locally by an OS.
The string I have successfully renders when put as the src of an HTML IMG element with the data URI preface (data: img/png;base64, ) but when using the atob function or a goog closure function it fails.
However decoding succeeds when put in here: http://www.base64decode.org/
Any ideas?
EDIT:
I successfully got it to decode with another library other than the built-in JS function. But, it still won't open locally - on a Mac says it's damaged or in an unknown format and can't get opened.
The code is just something like:
imgEl.src = 'data:img/png;base64,' + contentStr; //this displays successfully
decodedStr = window.atob(contentStr); //this throws the invalid char exception but i just
//used a different script to get it decode successfully but still won't display locally
the base64 string itself is too long to display here (limit is 30,000 characters)
I was just banging my head against the wall on this one for awhile.
There are a couple of possible causes to the problem. 1) Utf-8 problems. There's a good write up + a solution for that here.
In my case, I also had to make sure all the whitespace was out of the string before passing it to atob. e.g.
function decodeFromBase64(input) {
input = input.replace(/\s/g, '');
return atob(input);
}
What was really frustrating was that the base64 parsed correctly using the base64 library in python, but not in JS.
I had to remove the data:audio/wav;base64, in front of the b64, as this was given as part of the b64.
var data = b64Data.substring(b64Data.indexOf(',')+1);
var processed = atob(data);

How to add line break character on my widget's javascript for Nokia WRT?

If anybody like me is using Nokia WRT Plug-in for Visual Studio...
I've created on Visual Studio a Rss Reader Widget.
Now I'm customizing it, trying to add line breaks in rss tag called "< description>".
I'm trying many codes like with no luck:
"Fist line\u000dSecond line"
"Fist line\u000dSecond line"
"Fist line\nSecond line"
"Fist line& #xD;Second line" --> remove space here :)
"Fist line<br>Second line"
I'm also digging more to find out what's up with html format, since CDATA is not working to present formatted content (I have to use clean text in my rss file).
thanks in advance
I came up with a clutter solution:
my rss content is generated dynamically on a aspx page (response content type is rss format).
nokia widget is able to read a url location, in this case my rss url location: http://localhost/mysite/rss.aspx
since line breaks symbols defined in my rss output are not understood by nokia's javacript function getContentHTMLForFeedItem, I changed rss content:
"First line; Second line"
Now javascript reads this rss content as valid. It's time to force a line break.
To force line break, I changed getContentHTMLForFeedItem function as follows:
// Returns the content HTML for a feed item.
function getContentHTMLForFeedItem(item) {
var buf = "";
// item description
if (item.description != null) {
var linebreaked = "" + item.description;
while (linebreaked.indexOf("; ") > 0)
linebreaked = linebreaked.replace("; ", "[br]");
buf += "[div class=\"FeedItemDescription\"]" + linebreaked + "[/div]";
}
Note: Change the brackets to less than "<" and greater than signs ">".
If anyone is having the same problem or if I'm doing something wrong, please let me know.

Javascript not-well formed error in for loop

I copied this code from an example. I've read it 100 times.
Array.prototype.map = function(fn) {
var r = [];
var l = this.length;
for(var i = 0; i < l; i++) {
r.push(fn(this[i]));
}
return r;
};
Why does Firefox say:
not well-formed
file:///some/path.html Line: 5
for(var i = 0; i < l; i++) {
-------------------^
UPDATE
The error is only shown when Firebug is turned on for the page.
You are using Javascript code in an HTML page claiming to be fully XHTML-compliant. Therefore, the < character cannot appear in the Javascript, as it would be interpreted as the beginning of an XHTML tag.
There are a number of ways to fix this.
You could change the DOCTYPE and make it not XHTML.
You could enclose the Javascript in a <![CDATA[ section.
You could move the Javascript into a separate .js file.
You could escape every occurrence of < with &lt and every & with &. I do not recommend this option; it'll make your code unreadable and will almost definitely not work in IE.
Likely your error isn't in this code, but something above it trickling errors down. So, instead of finding an error in this code, look above for malformed HTML or javascript that could be causing this error instead.
If I use the following HTML and your text as "test.js", I too get no errors in Firebug.
<html>
<head>
<script type="text/javascript" src="test.js"></script>
</head>
<body>
test
</body>
</html>
Works for me. Please post the full file, and make sure you are using script tags.
I posted a validating pastebin file (Chetan's was technically not valid), and it works fine with Firebug. So I suggest you come back with a full page of validating code that doesn't work.

Categories