12660

Javascript DOMParser.parseFromString giving parse error

Question:

When doing this in console, why do I get a parse error?

TR = '<TR id=line1 class="myClass"><INPUT id=input1 type=hidden> <INPUT id=input2> <TD style="PADDING-LEFT: 20px" align=left> <IMG class=im border=0 src="images/img.gif"> Hello </TD><!-- comment --> <TD id=cell1 align=right></TD> <TD id=cell2 align=right></TD> <TD align=middle>&nbsp; </TD> <TD align=middle></TD></TR>'; parser = new DOMParser() xmlDocument = _parser.parseFromString(TR, "text/xml");

Answer1:

First problem (I assume it's not the one you're experiencing, rather that this code is erroneously copy-pasted): Your variable name is parser, not _parser.

Your main problem is that you're trying to parse HTML as XML, which will work <strong><em>IF</em></strong> your HTML is also valid XML. But yours isn't. Quote your attributes for a start. That's what

error on line 1 at column 8: AttValue: " or ' expected

means.

After you do that, close your void elements. It's OK to leave off the trailing slash in HTML5, but not within the stricter rules of XML.

Answer2:

You create a new DOMParser() and assign it to a variable named parser:

parser = new DOMParser()

But then you reference an undeclared variable _parser on the next line:

xmlDocument = _parser.parseFromString(TR, "text/xml");

If you replace _parser with parser, the console error should go away.

Recommend

  • How do one only submit click event (div), when using jquery live function?
  • TCPDF's getNumLines() is sometimes wrong
  • https in htaccess and order of rules (using Expression Engine)
  • Play Framework nested form errors missing
  • How to get a table cell value using jQuery
  • window.onbeforeunload in javascript
  • PostgreSQL 9.x - pg_read_binary_file & inserting files into bytea
  • Javascript (MVC) load image (byte array) from database
  • IOS > Open URL in Safari with POST
  • Setting src to Base64-encoded image with Javascript is failing
  • How to stamp out template in self contained custom elements with vanilla js?
  • Is it better to use the “hidden” CSS attribute or fetch each set of new images?
  • Python PIL to extract number from image
  • Outputting SharePoint Hyperlink Column as URL
  • Python ImageIO Gif Set Delay Between Frames
  • Disable Kendo Autocomplete
  • converting text file into xml using php?
  • How to view images from protected folder with php?
  • how to display data from 1st point on words on y axis for line chart in d3.js
  • How to run “Deployd” on port 80 instead of port 5000 in webserver.
  • Display images in Django
  • HTTP/2 streams vs HTTP/1.1 connections
  • Custom Tabgroup Appcelerator
  • Listbox within Listbox and scrolling trouble in Windows Phone 7 Silverlight
  • Android full screen on only one activity?
  • $wpdb not working in file of WordPress plugin
  • Disable Enter in editText android
  • Syntax for setting draggablecursor property in google maps api
  • Repeat a vertical line on every page in Report Builder / SSRS
  • Change JButton Shape while respecting Look And Feel
  • Resize panoramic image to fixed size
  • How to convert from System.Drawing.Color to Excel.ColorFormat in C#? Change comment color
  • How to check if every primary key value is being referenced as foreign key in another table
  • Importing jscolor library in angular 2
  • Weird JavaScript statement, what does it mean?
  • jquery mobile loadPage not working
  • GridView Sorting works once only
  • Arrays break string types in Julia
  • How to get icons for entities from eclipse?
  • python regex in pyparsing