开发者

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). I开发者_如何学运维t'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).


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).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜