1

The web application that I am working with was build about 10 years ago. It works just in IE. I want it to work also in Crome.

I encountered in next problem: how can I extend "text" property of IXMLDOMElement to use "text" property(when working with IE) or "textContent" property(when working with Crome). Below there is a code exaple.

Crome has a problem with next row:

var resultContent = result_xml.selectSingleNode("WSResult/Result").text;  (*)

instead of "text" property, there is "textContent" property. I am searching how can I extend the "text" property that it will work in Crome also without changing (*) line.

I found the next code, but it does not work for me

Element.prototype.text = function () {
return (this.textContent === undefined ? this.text : this.textContent);
} 

code example:

function XMLDocWorkspace
{
    this.LoadXML = function(xmlStr) {
        if (xmlStr == null || $.trim(xmlStr) == "") return null;       
        if (window.DOMParser) {
            parser = new DOMParser();
            _xmlDoc = parser.parseFromString(xmlStr, "text/xml");
        }
        else // Internet Explorer
        {
            _xmlDoc = new ActiveXObject(Msxml2.DOMDocument.6.0);
            _xmlDoc.async = false;
            _xmlDoc.loadXML(xmlStr);
        }
        var errorMsg = null;
        if (_xmlDoc.parseError && _xmlDoc.parseError.errorCode != 0) {
            errorMsg = "XML Parsing Error: " + _xmlDoc.parseError.reason
                          + " at line " + _xmlDoc.parseError.line
                          + " at position " + _xmlDoc.parseError.linepos;
        }
        else {
            if (_xmlDoc.documentElement) {
                if (_xmlDoc.documentElement.nodeName == "parsererror") {
                    errorMsg = _xmlDoc.documentElement.childNodes[0].nodeValue;
                }
            }
            else {
                errorMsg = "XML Parsing Error!";
            }
        }

        if (errorMsg) {
            return false;
        }        
    }

    this.SelectSingleNode = function(xPath) {
        if (_xmlDoc == null) return null;
        var nodes = this.selectNodes(xPath);
        if (nodes.length > 0) {
            return nodes[0];
        }
        return null;
    }


    this.SelectNodes = function (xPath)
    {
        if (_xmlDoc == null) return null;
        var condition = xPath;
        condition = condition.substring(0, 1) == "/" ? condition.substring(1, condition.length) : condition;
        condition = condition.replace(new RegExp("/", "g"), ">").replace(new RegExp("@", "g"), "");
        return $(_xmlDoc).find(condition);
    }
}


//*************************************************
 var result_xml = new XMLDocWorkspace()
 result_xml.loadXML(xmlString);
var resultContent = result_xml.selectSingleNode("WSResult/Result").text;
//*************
4

1 に答える 1

0

ここを見てください。

ゲッターを定義する必要があります。

要素.プロトタイプ。defineGetter ('text', function () { return this.textContent; });

;)

于 2013-02-13T15:45:00.413 に答える