Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serializing XML from Browser DOM doesn't create an XML declaration

Okay, so we have a webapp that's communicating with a web-service via XML.

As of now we're assembling those XMLs by just hacking Strings together (and sending that via XMLHttpRequest/POST). I'm of a mind to re-factor that into using the facilities for building and serialising an XML DOM tree that are built into the browser, i.e. document.implementation & XMLSerializer().serializeToString(doc) (see Mozilla docs here and here). It's been working fine so far, except the resulting string doesn't contain an XML declaration.

So, how's that supposed to work? Any good advice and/or reading?

Oh, yeah, I've found this mozilla bug describing the problem and a hacky workaround (the linked thread is available via archive.org).

like image 512
Ben Schäffner Avatar asked Nov 14 '22 16:11

Ben Schäffner


1 Answers

Different browsers have different behavior around outputting the xml declarations.

Here's been my experience:

  • Opera 12.15 on Mac -- XML declaration
  • Safari 6.0 on Mac -- No declaration
  • Chrome 26.0 on Mac -- No declaration
  • Firefox 21.0 on Mac -- No declaration

Not sure what IE does. Another noteworthy observation is that there is seemingly no API (that i have found) to turn off the declaration in Opera. As a result, I'd find a better 'hack' than the one that you point to that is aware of if the XML declaration is already there or not. Perhaps a quick string based check if the serialized form contains the declaration would be adequate (e.g if (serialized.slice(0, 21) == "<?xml version=\"1.0\"?>") ... though this isn't a great way to check/ I'd consider alternatives).

like image 123
Michael Wasser Avatar answered Nov 17 '22 00:11

Michael Wasser