Re: text to XML object (E4X)

Martin Honnen <[email protected]>
Newsgroups gmane.comp.mozilla.devel.xml
Organization Liberty Development
Message-ID <[email protected]>

hluz wrote:


> I'm having a problem with creating a XML object from a document received 
> via XMLHTTPRequest.
> The following code:
> 
> var x = new XML(responseText)
> 
> gives javascript error: xml is a reserved identifier.
> If I remove the <?xml version="1.0"?> line from responseText before 
> using it, it works fine.

It is by design as the authors of the E4X specification wanted it, the 
specification for
   new XML(string)
says to do the following (13.4.2):
   Let x = ToXML(string)
and ToXML(string) is specified as follows (10.3.1):
   Parse the concatenation of
   <parent xmlns=", defaultNamespace, ">", string, "</parent>"
So that way a conforming implementation has to parse e.g.
   <parent xmlns=""><?xml version="1.0"?><root /></parent>
and that is not well-formed and gives a parse error.
Thus if you want to parse a string with XML markup make sure you throw 
the XML declaration away before passing the string to new XML().

A regular expression to match the XML declaration is e.g.
   var xmlDeclaration = /^<\?xml version[^>]+?>/;
so you could try removing it e.g.
   new XML(responseText.replace(xmlDeclaration, ''))

-- 

	Martin Honnen
	http://JavaScript.FAQTs.com/
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.