Pergunta

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?

Foi útil?

Solução

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.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top