Closed Bug 298447 Opened 20 years ago Closed 18 years ago

Add document.loadXML method

Categories

(Core :: DOM: Core & HTML, enhancement)

enhancement
Not set
normal

Tracking

()

RESOLVED WONTFIX

People

(Reporter: log, Unassigned)

Details

Attachments

(1 file, 1 obsolete file)

User-Agent:       Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7.8) Gecko/20050511 Firefox/1.0.4
Build Identifier: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7.8) Gecko/20050511 Firefox/1.0.4

Creating a document object from an XML string is a little more than trivial. 
Moz/Fx and MSIE  both have a load() method that will load an XML file and
generate a document object, but the latter also supports a loadXML() method that
takes an XML string:
http://msdn.microsoft.com/library/en-us/xmlsdk/html/19957329-69cd-456a-876f-7d2b692b0afc.asp

It'd be nice to have this method.  Here's a piece of code I came across that
will emulate this functionality:

  // Loads an XML document from a text string
  Document.prototype.loadXML = function(s) {
    var doc2 = (new DOMParser()).parseFromString(s,"text/xml");
    while (this.hasChildNodes()) {
      this.removeChild(this.lastChild);
    }
    for (var i = 0; i != doc2.childNodes.length; i++) {
      this.appendChild(this.importNode(doc2.childNodes[i],true));
    }
  }



Reproducible: Always

Steps to Reproduce:
1. xmlDoc = document.implementation.createDocument("", "", null);
2. xmlDoc.loadXML(longXMLString);

Actual Results:  
There is no loadXML method

Expected Results:  
Successfully import the data, allowing you to call
xmlDoc.getElementsByTagName(), for instance.
Do data: URIs help at all here?
Attached file testcase using data: uri (obsolete) —
Ian, that doesn't seem to work. Or I may be using data: wrong - I've never used
it before. Trying to use a data: uri with XMLDocument.load throws the following
error:

Permission denied to call method XMLDocument.load
Forgot to add the ";base64". Testcase still doesn't work.
Attachment #199291 - Attachment is obsolete: true
You'd just set document.location to the data: URI.
I discovered today that DOMParser basically does what I wanted.  This RFE could be closed WONTFIX, or the function could be replaced with something like this:

var xmlDoc;
if (typeof(DOMParser) != "undefined") {
    var parser = new DOMParser();
    xmlDoc = parser.parseFromString(x, "application/xhtml+xml");
}
(In reply to comment #4)
> You'd just set document.location to the data: URI.
> 

That doesn't seem to work with an XML document created by createDocument().
Ah, yes.  Documents created via createDocument are static -- no scripts run, no loads happen, etc.
In HTML5 as proposed today, "document" objects (even in XML) have an "innerHTML" attribute that could be used to handle this:

   xmlDoc = document.implementation.createDocument("", "", null);
   xmlDoc.innerHTML = longXMLString;

This is still being discussed (in particular bz has raised some objections) but something to this effect will be available.

Anyway, closing for now, as discussed above. (comment 5)
Status: NEW → RESOLVED
Closed: 18 years ago
Resolution: --- → WONTFIX
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Creator:
Created:
Updated:
Size: