Javascript 当XML看起来正常时,为什么jquery的parseXML函数会失败?
我有一个非常简单的javascript,它试图解析我从jpeg格式的元数据中提取的xml:Javascript 当XML看起来正常时,为什么jquery的parseXML函数会失败?,javascript,jquery,xml,debugging,parsing,Javascript,Jquery,Xml,Debugging,Parsing,我有一个非常简单的javascript,它试图解析我从jpeg格式的元数据中提取的xml: var xmlDoc; try { xmlDoc = $.parseXML(xmlString); } catch(e) { console.log(e); } 以下是引发的异常: var xmlDoc; try { xmlDoc = $.parseXML(xmlString); } catch(e) { console.log(e); } Invalid XML: &
var xmlDoc;
try {
xmlDoc = $.parseXML(xmlString);
} catch(e) {
console.log(e);
}
以下是引发的异常:
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(xmlString);
} catch(e) {
console.log(e);
}
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);
}
我只能假设一定有某种不可打印的特殊字符在那里的某个地方引起了麻烦。如何测试该假设并修复它,或者可能有其他错误?通过使用此网站,我收到以下错误消息,请确保选中“根据外部XML模式验证”框
var xmlDoc;
try {
xmlDoc = $.parseXML(xmlString);
} catch(e) {
console.log(e);
}
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>
通过使用此网站,我收到了以下错误消息,请确保选中“根据外部XML模式验证”复选框
var xmlDoc;
try {
xmlDoc = $.parseXML(xmlString);
} catch(e) {
console.log(e);
}
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>
也许你的问题是:
您不能在JS中对没有XML头的文件使用XML函数。
尝试在浏览器中打开XML文件/输入,在地址栏中键入路径,
并查看它是否以xml文件或类似文本文件的形式打开
var xmlDoc;
try {
xmlDoc = $.parseXML(xmlString);
} catch(e) {
console.log(e);
}
例如,使用php添加XML标头:
var xmlDoc;
try {
xmlDoc = $.parseXML(xmlString);
} catch(e) {
console.log(e);
}
header ("Content-Type: text/xml");
也许你的问题是:
您不能在JS中对没有XML头的文件使用XML函数。
尝试在浏览器中打开XML文件/输入,在地址栏中键入路径,
并查看它是否以xml文件或类似文本文件的形式打开
var xmlDoc;
try {
xmlDoc = $.parseXML(xmlString);
} catch(e) {
console.log(e);
}
例如,使用php添加XML标头:
var xmlDoc;
try {
xmlDoc = $.parseXML(xmlString);
} catch(e) {
console.log(e);
}
header ("Content-Type: text/xml");
我发现了问题所在。正如我所怀疑的,在字符串的末尾潜伏着一些讨厌的不可打印字符
var xmlDoc;
try {
xmlDoc = $.parseXML(xmlString);
} catch(e) {
console.log(e);
}
我能够通过这种肮脏的黑客行为将其删除:
var xmlDoc;
try {
xmlDoc = $.parseXML(xmlString);
} catch(e) {
console.log(e);
}
xmlString = xmlString.substr(xmlString.indexOf("<"), xmlString.lastIndexOf(">") + 1);
如果不明显,它只会从字符串的开始和结束处删除任何不是xml文档预期的尖括号的内容。jQuery函数trim不能有效地删除流氓字符,因为它只删除空格
var xmlDoc;
try {
xmlDoc = $.parseXML(xmlString);
} catch(e) {
console.log(e);
}
我不知道这个角色是什么,我对我的解决方案不是特别满意,但我太忙了,没时间花更多的时间在上面。唉。我发现问题了。正如我所怀疑的,在字符串的末尾潜伏着一些讨厌的不可打印字符
var xmlDoc;
try {
xmlDoc = $.parseXML(xmlString);
} catch(e) {
console.log(e);
}
我能够通过这种肮脏的黑客行为将其删除:
var xmlDoc;
try {
xmlDoc = $.parseXML(xmlString);
} catch(e) {
console.log(e);
}
xmlString = xmlString.substr(xmlString.indexOf("<"), xmlString.lastIndexOf(">") + 1);
如果不明显,它只会从字符串的开始和结束处删除任何不是xml文档预期的尖括号的内容。jQuery函数trim不能有效地删除流氓字符,因为它只删除空格
var xmlDoc;
try {
xmlDoc = $.parseXML(xmlString);
} catch(e) {
console.log(e);
}
我不知道这个角色是什么,我对我的解决方案不是特别满意,但我太忙了,没时间花更多的时间在上面。叹气。注意jQuery本身根本不解析XML——浏览器解析。@Pointy,很好的观点。这是失败的铬。我在FireFox中试过了,它解析的很好。现在我能做什么?!!阿格!请注意,jQuery本身根本不解析XML—浏览器解析。@Pointy,很好。这是失败的铬。我在FireFox中试过了,它解析的很好。现在我能做什么?!!阿格!我认为这不是问题所在。没有定义外部模式,所以我不知道该站点正在验证什么。我还有其他例子,在Chrome中解析XML时没有失败,但在该网站上导致了相同的错误。我认为这不是问题所在。没有定义外部模式,所以我不知道该站点正在验证什么。我还有其他一些例子,在Chrome中解析xml时没有失败,但在该网站上会导致相同的错误。xml输入数据实际上只是我在javascript中手动从二进制jpeg文件中提取的字符串。最后我解决了我自己的问题,请看我的答案,但谢谢你的建议。xml输入数据实际上只是我用javascript从二进制jpeg文件中手动提取的字符串。最后我解决了我自己的问题。请看我的答案,但是谢谢你的建议。这个字符是二进制的吗?仅仅因为你看不到它并不意味着它不在那里,但是通过这种方式修剪它可以确保你没有把它包含在变量中。好的,真的。我担心的是,在最终>之后根本不应该有任何数据。如果在结尾的某个地方碰巧有一个随机的>,我的黑客程序就不太管用了,我会再次遇到这个问题。但正确的几率是多少;您是如何从jpg中提取元数据的?我更厚颜无耻地对这一部分感兴趣:我扩展了Jacob Seidelin的一些伟大作品:用于我的站点:从JSP页面获得了一个不太干净的输出,导致了XML的下降。在使用了您的代码后,一切都正常了!那个字符是二进制的吗?仅仅因为你看不到它并不意味着它不在那里,但是通过这种方式修剪它可以确保你没有把它包含在变量中。好的,真的。我担心的是,在最终>之后根本不应该有任何数据。如果在结尾的某个地方碰巧有一个随机的>,我的黑客程序就不太管用了,我会再次遇到这个问题。但正确的几率是多少;您是如何从jpg中提取元数据的?我更无耻地对那部分感兴趣:我
var xmlDoc;
try {
xmlDoc = $.parseXML(xmlString);
} catch(e) {
console.log(e);
}
扩展了Jacob Seidelin的一些伟大工作:用于我的站点:从一个JSP页面获得了一个不太干净的输出,它导致了一个XML。在使用了您的代码后,一切都正常了!