2012-02-16 39 views
1

我的JavaScript的一個非常簡單的位試圖解析我從元數據的JPEG提取的XML:爲什麼XML看起來好的時候jquery的parseXML函數失敗了?

var xmlDoc; 
try { 
    xmlDoc = $.parseXML(xmlString); 
} catch(e) { 
    console.log(e); 
} 

這裏是一個被拋出的異常:

Invalid XML: <x:xmpmeta xmlns:x="adobe:ns:meta/" x:xmptk="XMP Core 4.4.0"> 
    <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"> 
     <rdf:Description rdf:about="" 
      xmlns:photoshop="http://ns.adobe.com/photoshop/1.0/"> 
     <photoshop:Instructions>C1DDZVs9Sr+DG5R9eSc%9w</photoshop:Instructions> 
     </rdf:Description> 
     <rdf:Description rdf:about="" 
      xmlns:aux="http://ns.adobe.com/exif/1.0/aux/"> 
     <aux:SerialNumber>1</aux:SerialNumber> 
     <aux:Lens>AF-S DX VR Zoom-Nikkor 18-200mm f/3.5-5.6G IF-ED [II]</aux:Lens> 
     <aux:LensID>1</aux:LensID> 
     <aux:ImageNumber>6651</aux:ImageNumber> 
     <aux:FlashCompensation>0/1</aux:FlashCompensation> 
     </rdf:Description> 
    </rdf:RDF> 
</x:xmpmeta> 

有似乎沒有任何錯誤的XML。事實上,如果我剪切並粘貼直接在XML中,沒有任何異常被拋出:

var xmlDoc; 
try { 
    xmlDoc = $.parseXML('<x:xmpmeta xmlns:x="adobe:ns:meta/" x:xmptk="XMP Core 4.4.0">  <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">   <rdf:Description rdf:about=""    xmlns:photoshop="http://ns.adobe.com/photoshop/1.0/">    <photoshop:Instructions>C1DDZVs9Sr+DG5R9eSc%9w</photoshop:Instructions>   </rdf:Description>   <rdf:Description rdf:about=""    xmlns:aux="http://ns.adobe.com/exif/1.0/aux/">    <aux:SerialNumber>1</aux:SerialNumber>    <aux:Lens>AF-S DX VR Zoom-Nikkor 18-200mm f/3.5-5.6G IF-ED [II]</aux:Lens>    <aux:LensID>1</aux:LensID>    <aux:ImageNumber>6651</aux:ImageNumber>    <aux:FlashCompensation>0/1</aux:FlashCompensation>   </rdf:Description>  </rdf:RDF> </x:xmpmeta>'); 
} catch(e) { 
    console.log("error parsing xml: " + e); 
} 

我只能假設,必須有某種不可打印的特殊字符在那裏的地方,是造成麻煩。我如何測試這個假設並修復它,或者其他的東西是錯的?

+1

請注意,jQuery本身並不解析XML - 瀏覽器。 – Pointy 2012-02-16 16:40:56

+0

@好點,好點。這在Chrome中失敗了。我嘗試了FireFox,它解析得很好。現在我該怎麼辦?精氨酸! – 2012-02-16 16:58:37

回答

4

我發現這個問題。正如我懷疑的那樣,在字符串的末尾有一些令人討厭的不可打印的字符。

我能與黑客的這種髒位將其刪除:

xmlString = xmlString.substr(xmlString.indexOf("<"), xmlString.lastIndexOf(">") + 1); 

如果不是很明顯,它只是修剪遠離開始和結束的字符串不是預期的尖括號什麼一個XML文檔。 jQuery函數「trim()」在刪除流氓角色方面效果不佳,因爲它只能處理空白。

我不知道角色是什麼,我對我的解決方案並不特別滿意,但我太忙了,不能花更多時間在這個問題上。嘆。

+0

那個角色是二進制的嗎?只是因爲你沒有看到它並不意味着它不存在,而是通過這種方式修剪它,這樣可以確保你沒有將它包含在變量中。好的。 – 2012-02-16 17:53:28

+0

確實。我擔心的是,在最後的「>」之後應該沒有任何數據。如果碰巧在某處有一個隨機的「>」,我的黑客就不會工作,我會再次遇到這個問題。但是賠率是多少? ;) – 2012-02-16 18:00:56

+0

你是如何從jpg中提取元數據的?我更無恥地感興趣的那部分:) – 2012-02-16 21:25:24

0

使用本網站http://www.xmlvalidation.com,我得到了以下錯誤消息,請確保您勾選「驗證對外部XML模式」

An error has been found! 

1: 62 cvc-elt.1: Cannot find the declaration of element 'x:xmpmeta'. 

XML document: 
1 <x:xmpmeta xmlns:x="adobe:ns:meta/" x:xmptk="XMP Core 4.4.0"> 
2  <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"> 
3   <rdf:Description rdf:about="" 
4    xmlns:photoshop="http://ns.adobe.com/photoshop/1.0/"> 
5   <photoshop:Instructions>C1DDZVs9Sr+DG5R9eSc%9w</photoshop:Instructions> 
6   </rdf:Description> 
7   <rdf:Description rdf:about="" 
8    xmlns:aux="http://ns.adobe.com/exif/1.0/aux/"> 
9   <aux:SerialNumber>1</aux:SerialNumber> 
10   <aux:Lens>AF-S DX VR Zoom-Nikkor 18-200mm f/3.5-5.6G IF-ED [II]</aux:Lens> 
11   <aux:LensID>1</aux:LensID> 
12   <aux:ImageNumber>6651</aux:ImageNumber> 
13   <aux:FlashCompensation>0/1</aux:FlashCompensation> 
14  </rdf:Description> 
15  </rdf:RDF> 
16 </x:xmpmeta> 
+0

我不認爲這是問題。沒有定義外部模式,因此我不知道該網站正在驗證哪些內容。我還有其他的例子,在Chrome中parseXML中沒有失敗,但在該網站上導致相同的錯誤。 – 2012-02-16 17:27:15

0

也許你的問題是:
你不能使用XML函數(在JS中)文件上沒有XML頭。
嘗試在瀏覽器中打開XML文件/輸入(在地址欄中鍵入路徑),
並查看它是否以xml文件或類似文本文件的形式打開。

爲emample,添加XML頭用PHP:

header ("Content-Type: text/xml"); 
+0

xml輸入數據實際上只是一個字符串,我手動翻出了JavaScript中的二進制jpeg文件。我最終解決了自己的問題(請參閱我的答案),但謝謝您的建議。 – 2012-02-16 17:46:49

相關問題