XML parsing on different browsers

瀏覽器解析 XML 的不同行為

一個 XML 文件長這個樣子:一個文件類型宣告 (<?xml ?>)、一個根目錄 element 包含其他的 element。所以當 XMLHttpRequest 物件抓遠端的 XML 回來解析的時候,obj.responseXML 這個 XMLDocument Object 的 .childNodes.length 應該是 1 —— .firstChild 就是 root element 。

問題來啦:以上看似合理的 XML parsing 只發生在 Mozilla 上面。在 IE 裡面 childNodes.length 是 2,firstChild.nodeName 會傳回 … ?xml !這是怎樣?

那改用 .lastChild 去拿 root element 好了,這總不會出錯了吧。嘿嘿,不行 —— Operaobj.responseXML 更有趣:childNodes.length = 2 ,第一個 childNode 是 ?xml、第二個是 root、第三個是 … #text,nodeValue是長度 0 空字串。我檢查過,這絕對不是 Server 端的問題,因為那邊是用 PHP DOM 在生 well-formed XML 的。

最後我的解法還是用 obj.responseXML.lastChild 拿 root,不過在之前先經過一個判斷式:如果 .lastChild.nodeName == ‘#text’ 就做 removeChild 的動作。

這個解析 XML 的差異是因為各家瀏覽器 XMLHttpRequest 物件行為的不同,與 prototype.js 無關。prototype.js 只做 warpper 而已,裡面搞成這樣它應該是不知道。

還有另外一種解法是不要做 AJAX —— 做 AJAteXt,叫伺服器傳文字進來,再從 obj.responseText 抓出來處理。不是很正統而且還需要而額外寫解析函式的方法,但 GMail 好像就是這樣在抓信件內文的。

XMLHttpRequest on different browsers parses XML in different ways. An XMLDocument Object contains a <?xml ?> doctype declaration and a root element. Anything else should be child nodes under the root. Thus, when the XML file is fetched, obj.responseXML.childNodes.length should return 1, and firstChild should return the root element.

Sounds right? Not quite. What described above only occurs in Mozilla. In poor-designed IE6, childNodes.length returns 2 — firstChild.nodeName returns ?xml. Opera behaviors even weirder; childNodes.length returns 3, first two are same as IE, and the their one is a #text with zero-length string nodeValue.

I ended up using lastChild to retrive the root element after checking its nodeName; if lastChild.nodeName == ‘#test’ { removeChiid(); } .

The other solution is to abandon AJAX – use AJAteXt instead. By parse responseText on your own code, you could avoid these kind of inconsistent behaviors. Kind of non-standard method, but AFAIK, Gmail does use AJAteXt to retrieve mail text.

週記:生日

十月生日的人是有宿命的:從小到大有很多很多年,我的生日是獨自在陌生的新班級裡面度過的。久而久之也就不慶祝這種日子了。今年的生日延續去年,在實驗室裡面度過…做到10點半回寢室。

好處是不會被丟湖(笑);做完實驗在靜靜的跟同學說其實今天是我生日,然後回寢室看看板、貼貼文章,接著時鐘就超過AM 12:00 了。

生日願望的其中一項是拿到本學期全部的25學分,挑戰自己的極限。加退選結束之後才發現25學分要拿肝才換的到;其中星期三的課可是234Z5678連貫,中午不休息從早上9點到5點半。每過完一個星期三,就想「嗯,本週是第5週,大概,『只要』再重複13次本學期就結束了。」

真的用生日願望就能如願的話就太簡單了,還是要讀書才行。跟前面兩篇剪綵一樣,開學之後的確是把不少事情做結了,順手把一些 Feed/BBS 我的最愛刪掉以免花太多時間在網路上。花時間在網路上也就罷了,反過來心理有想法想要 feedback 或是一些「小」程式想要實現的時候花的時間才多(囧)。

不禁又想起了 Blog 的 tagline … but still I am right here right now putting something on the Web right?

還有一隻小東西+松高新網站的外觀部分沒做。寫好就不做網路了(?)。

Tim

P.S. 下面那篇 Republic Day 是積稿,你以為我有時間寫這麼大篇英文還 proof-read 嗎@@。

Republic Day

Today is the date marks the establishment of Republic of China. Many people from Taiwan might refer you that today is the “Double Ten Day”, but the term is not self explanatory, and today (1010) has really nothing to do with binary numbering. The correct term should be “Republic Day.”

Republic of China(ROC) was (is?) the first Chinese Republic, which ends Chinese monarchy in 1911. After World War II, Communists bets Nationalist who leads R.O.C. and form their second Chinese RepublicPeople’s Republic of China (PRC). Nationalists then bore the ROC name and fled to Taiwan. Before that, Taiwan was a Japanese colony for 50 years (1895-1945).

The ROC government continued acting as Chinese governments in exile until 1994, a document implicitly recognise the existence of PRC and place ROC as the government represents the Taiwanese people, willing to negotiate for peaceful unification (at the time.)

The new ruling party from 2000, which is pro independence though, push for the formal reorganisation of PRC and ourselves. Plans includes legally change the name of the state to Taiwan instead of ROC which suggested itself as a Chinese government in exile. To do that, with formally “giving up” the sovereignty over China, a constitutional change, even a new constitution has to be in place. PRC sees these reform as a move toward formal independence against China, and thread the use of force. Pro unification parties also rejects the idea.

For your information. I hope what I wrote above can clean things up for you, and answer the question “why Taiwan is formally named Republic of China?” in mind.

With legal point of view, de jule status of Taiwan can only be certain if a treaty was made by China, Japan and United States. But sadly, such treaty can never took place after a serious war, probably World War III. And nothing in the international law says the future if Taiwan should consider the will of Taiwanese residence (the US Taiwan Relation Act does though). Taiwan has operate herself independently for 57 years without a formal status, and is likely to do so for decades (or caught into a war which is the last thing I would like to see).