0

我在 HTML 文件的脚本标记中嵌入了一些(实际上是很多)XML。我抓住这些数据来处理

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

以下是数据示例:

<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>

当我使用时,$.parseXML(xmlString)我在 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>...

奇怪的是,它在 Internet Explorer 11 中运行良好。我曾经$.isXMLDoc(xmlString)测试过它是否已经是 XML Doc,但它返回 False。那么这在 Chrome 中不起作用是什么?

4

1 回答 1

0

XML 规范说 像这样的XML 声明<?xml version="1.0"?>只能出现在文件的开头,因此 Chrome 只是拒绝无效的 XML。Internet Explorer 比 Chrome 稍微宽松一点,并且接受这个无效的文档。

解决您的问题的一种可能方法是从 HTML 文档中删除此 XML 声明。

于 2014-04-04T22:28:33.283 回答