phaser/src/dom/ParseXML.js

52 lines
1.2 KiB
JavaScript
Raw Normal View History

2018-02-12 16:01:20 +00:00
/**
* @author Richard Davey <rich@photonstorm.com>
2019-01-15 16:20:22 +00:00
* @copyright 2019 Photon Storm Ltd.
2019-05-10 15:15:04 +00:00
* @license {@link https://opensource.org/licenses/MIT|MIT License}
2018-02-12 16:01:20 +00:00
*/
2017-10-12 14:09:52 +00:00
/**
2018-01-26 03:40:49 +00:00
* Takes the given data string and parses it as XML.
* First tries to use the window.DOMParser and reverts to the Microsoft.XMLDOM if that fails.
* The parsed XML object is returned, or `null` if there was an error while parsing the data.
2017-10-12 14:09:52 +00:00
*
2018-02-13 00:40:51 +00:00
* @function Phaser.DOM.ParseXML
2017-10-12 14:09:52 +00:00
* @since 3.0.0
*
* @param {string} data - The XML source stored in a string.
*
2018-03-20 14:36:03 +00:00
* @return {?(DOMParser|ActiveXObject)} The parsed XML data, or `null` if the data could not be parsed.
2017-10-12 14:09:52 +00:00
*/
var ParseXML = function (data)
{
var xml = '';
try
{
if (window['DOMParser'])
{
var domparser = new DOMParser();
xml = domparser.parseFromString(data, 'text/xml');
}
else
{
xml = new ActiveXObject('Microsoft.XMLDOM');
xml.loadXML(data);
}
}
catch (e)
{
xml = null;
}
if (!xml || !xml.documentElement || xml.getElementsByTagName('parsererror').length)
{
return null;
}
else
{
return xml;
}
};
module.exports = ParseXML;