XML&符号错误我有&在我的xml文件中

时间:2014-05-20 13:53:05

标签: javascript xml

我有一个XML文件,当我在XML&或者一些对XML不合法的花药字符我有问题,如何在加载之前在javascript中更改XML

XML:

 <deal>
      <title> ran & ban  </title>

</deal>

我知道我需要把它改成像&amp ;; 如何在我获得白色之前更改XML的ROW数据

  function loadXMLDoc(filename){
      if (window.XMLHttpRequest)
{
    xhttp=new XMLHttpRequest();

}
else // code for IE5 and IE6
{
    xhttp=new ActiveXObject("Microsoft.XMLHTTP");
}

xhttp.open("GET",filename,true);
xhttp.send();

var xml= xhttp.responseXML;
 // xml==NULL 
}

2 个答案:

答案 0 :(得分:3)

那&#39; XML&#39;片段您已发布不是 XML,您必须事先将其作为文本文件处理,因为任何兼容的XML解析器都无法解析此文件。请注意,这本身可能容易出错(请注意字符编码等问题),理想情况下,您需要将传入的XML更正为well-formed,即

 <deal>
      <title> ran &amp; ban  </title>

</deal>

有关详细信息,请参阅this article

我肯定会推动您的来源得到纠正。如果你需要编码来纠正这个,那么很可能

  1. 它会其他错误(实体编码或其他方式)
  2. 你的纠正&#39;当你的源最终发布有效的XML时,代码可能会搞乱。

答案 1 :(得分:0)

编辑这应该:

var xmlescape = (function() {


  var setup = {
    full  : 0, // encode ascii as well, 0|1
    names : 0, // use named char. entities, 0|1
  };

  var rfulltag =

      // match opening tag, capture tagname and full tag
      '(<([a-z][a-z0-9]*)[^>]*>)' + 

     // capture everything not followed by a tag
     // (anything between matching pair of tags)
     '(?!\\s*<[a-z][a-z0-9]*[^>]*>)([\\s\\S]+?)' + 

    // capture closing tag coresponding to matched open tag
    '(<\\/\\2\\s*>)';

  rfulltag = new RegExp(rfulltag, 'gi'); // (g)lobal, case(i)nsensitive


  var runsafe = /["&\/<>']/g;   // reserved xml characters
  var rascii  = /[\x00-\xFF]/g; // ascii char.


  // hex character entities
  var unsafemap = {
    '"' : '&#x22;',
    '&' : '&#x26;',
    '/' : '&#x2F;',
    '<' : '&#x3C;',
    '>' : '&#x3E;',
    '\'': '&#x27;',
  };


  // named character entities
  var namedunsafemap = {
    '"' : '&quot;',
    '&' : '&amp;',
    '/' : '&sol;',
    '<' : '&lt;',
    '>' : '&gt;',
    '\'': '&apos;',
  };

  // use cache lookups to speed things a bit
  var cache = {};

  var pad02 = function(chr) {
    return (Array(3).slice(chr.length).join('0') + chr).toUpperCase();
  };

  var hexesc = function(chr) {
    return '&#x' + pad02(chr.charCodeAt(0).toString(16)) + ';';
  };

  // encode and cache
  var _encodeunsafe = function(chr) {
    return cache.hasOwnProperty(chr) ? cache[chr] : 
      (cache[chr] = (setup.full ? hexesc(chr) : (setup.names ? namedunsafemap[chr] : unsafemap[chr])));
  };

  var _encoder = function(all, _1, _2, _3, _4) {
    return _1 + _3.replace(setup.full ? rascii : runsafe, _encodeunsafe) + _4;
  };

  return function(sxml) {
    return ('' + sxml).replace(rfulltag, _encoder);
  };

})();

// xmlescape("<deal>  <title>  >>> \"<&> invalid b&d </&>' <<<  </title> <title>  ran &  ban </title>   </deal>");
// "<deal>  <title>  &#x3E;&#x3E;&#x3E; &#x22;&#x3C;&#x26;&#x3E; invalid b&#x26;d &#x3C;&#x2F;&#x26;&#x3E;&#x27; &#x3C;&#x3C;&#x3C;  </title> <title>  ran &#x26;  ban </title>   </deal>"


// from goog. closure
var parsexml = (function(g) {

  return ('function' == typeof g.DOMParser) ?
  // ffox, chrome, etc.
  function(xml) {
    return new g.DOMParser().parseFromString(xml, 'application/xml');
  } :
    (
    ('function' == typeof g.ActiveXObject) ? // ie

    (function(msaxo) {

      function _msdoc() {

        var doc = new msaxo('MSXML2.DOMDocument');

        if (doc) {

          // Prevent potential vulnerabilities exposed by MSXML2, see
          // http://b/1707300 and http://wiki/Main/ISETeamXMLAttacks for details.
          doc.resolveExternals = false;
          doc.validateOnParse  = false;

          // Add a try catch block because accessing these properties will throw an
          // error on unsupported MSXML versions. This affects Windows machines
          // running IE6 or IE7 that are on XP SP2 or earlier without MSXML updates.
          // See http://msdn.microsoft.com/en-us/library/ms766391(VS.85).aspx for
          // specific details on which MSXML versions support these properties.
          try {

            doc.setProperty('ProhibitDTD', true);
            doc.setProperty('MaxXMLSize', 2 * 1024); // goog.dom.xml.MAX_XML_SIZE_KB
            doc.setProperty('MaxElementDepth', 256); // goog.dom.xml.MAX_ELEMENT_DEPTH

          } catch (e) {}
        }

        return doc;
      }

      return function(xml) {
        var doc;
        return doc = _msdoc(), doc.loadXML(xml), doc;
      };

    })(g.ActiveXObject) :

    // other
    function(xml) {
      throw Error(': poor xml support; upgrade');
    }
  );
})(window);


// use:

var xhttp = new XMLHttpRequest();
// send a request..

var xml = xhttp.responseXML || xhttp.responseText;

if ('string' == typeof xml)
  xml = parsexml(xmlescape(xml));


// eof