Question

I have embedded some (actually a whole lot) of XML in a script tag of an HTML file. I grab this data to process by

var xmlString = document.getElementById('xmldata').innerHTML;

Here is a sample of the data:

<script id="xmldata" type="text/xml">
<?xml version="1.0"?>
<?mso-application progid="Excel.Sheet"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
 xmlns:o="urn:schemas-microsoft-com:office:office"
 xmlns:x="urn:schemas-microsoft-com:office:excel"
 xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
 xmlns:html="http://www.w3.org/TR/REC-html40">
 <DocumentProperties xmlns="urn:schemas-microsoft-com:office:office">
  <Author>Nick</Author>

When I use $.parseXML(xmlString) I get the following in Chrome:

Uncaught Error: Invalid XML: 
<?xml version="1.0"?>
<?mso-application progid="Excel.Sheet"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
 xmlns:o="urn:schemas-microsoft-com:office:office"
 xmlns:x="urn:schemas-microsoft-com:offic...<omitted>...

Oddly, it works fine in Internet Explorer 11. I've used $.isXMLDoc(xmlString) to test if it's already an XML Doc, but it returns False. So what isn't this working in Chrome?

Was it helpful?

Solution

The XML specification says that an XML declaration like <?xml version="1.0"?> may appear only at the beginning of a file, so Chrome is simply rejecting invalid XML. Internet Explorer is a bit laxer than Chrome, and accepts this invalid document.

One possible solution to your problem is to remove this XML declaration from the HTML document.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top