当XML看起来正常时,为什么jquery解析XML函数失败?

时间:2012-02-16 16:39:47

标签: javascript jquery xml debugging parsing

我有一个非常简单的javascript尝试解析我从jpeg中的元数据中提取的xml:

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);
}

我只能假设某处某处有某种不可打印的特殊字符会造成麻烦。我如何测试这个假设并修复它,或者其他可能是错误的?

3 个答案:

答案 0 :(得分:5)

我发现了问题。正如我所怀疑的那样,在弦乐的末尾潜伏着一些令人讨厌的不可打印的角色。

我能够通过这些肮脏的黑客攻击删除它:

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

如果它不明显,它只是修剪掉字符串的开头和结尾的任何内容,而不是xml文档的预期尖括号。 jQuery函数“trim()”在删除流氓角色方面无效,因为它只有空格。

我不知道这个角色是什么,我对我的解决方案并不是特别满意,但我太忙了,不能花更多的时间在上面。叹息。

答案 1 :(得分: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>

答案 2 :(得分:0)

也许你的问题是:
你不能在没有XML标题的文件中使用XML函数(在JS中) 尝试在浏览器中打开XML文件/输入(在地址栏中键入路径),
并查看它是否以xml文件或文本文件的形式打开。

对于emample,使用php添加XML标头:

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