admin管理员组文章数量:1431420
I have a pretty simple bit of javascript that attempt to parse the xml I've extracted from the metadata in a jpeg:
var xmlDoc;
try {
xmlDoc = $.parseXML(xmlString);
} catch(e) {
console.log(e);
}
Here is the exception that gets thrown:
Invalid XML: <x:xmpmeta xmlns:x="adobe:ns:meta/" x:xmptk="XMP Core 4.4.0">
<rdf:RDF xmlns:rdf="">
<rdf:Description rdf:about=""
xmlns:photoshop=".0/">
<photoshop:Instructions>C1DDZVs9Sr+DG5R9eSc%9w</photoshop:Instructions>
</rdf:Description>
<rdf:Description rdf:about=""
xmlns:aux=".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>
There doesn't seem to be anything wrong with that XML. In fact, if I cut and paste that xml in directly, no exception is thrown:
var xmlDoc;
try {
xmlDoc = $.parseXML('<x:xmpmeta xmlns:x="adobe:ns:meta/" x:xmptk="XMP Core 4.4.0"> <rdf:RDF xmlns:rdf=""> <rdf:Description rdf:about="" xmlns:photoshop=".0/"> <photoshop:Instructions>C1DDZVs9Sr+DG5R9eSc%9w</photoshop:Instructions> </rdf:Description> <rdf:Description rdf:about="" xmlns:aux=".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);
}
I can only assume that there must be some kind of unprintable special character in there somewhere that is causing the trouble. How can I test that assumption and fix it, or perhaps something else is wrong?
I have a pretty simple bit of javascript that attempt to parse the xml I've extracted from the metadata in a jpeg:
var xmlDoc;
try {
xmlDoc = $.parseXML(xmlString);
} catch(e) {
console.log(e);
}
Here is the exception that gets thrown:
Invalid XML: <x:xmpmeta xmlns:x="adobe:ns:meta/" x:xmptk="XMP Core 4.4.0">
<rdf:RDF xmlns:rdf="http://www.w3/1999/02/22-rdf-syntax-ns#">
<rdf:Description rdf:about=""
xmlns:photoshop="http://ns.adobe./photoshop/1.0/">
<photoshop:Instructions>C1DDZVs9Sr+DG5R9eSc%9w</photoshop:Instructions>
</rdf:Description>
<rdf:Description rdf:about=""
xmlns:aux="http://ns.adobe./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>
There doesn't seem to be anything wrong with that XML. In fact, if I cut and paste that xml in directly, no exception is thrown:
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/1999/02/22-rdf-syntax-ns#"> <rdf:Description rdf:about="" xmlns:photoshop="http://ns.adobe./photoshop/1.0/"> <photoshop:Instructions>C1DDZVs9Sr+DG5R9eSc%9w</photoshop:Instructions> </rdf:Description> <rdf:Description rdf:about="" xmlns:aux="http://ns.adobe./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);
}
I can only assume that there must be some kind of unprintable special character in there somewhere that is causing the trouble. How can I test that assumption and fix it, or perhaps something else is wrong?
Share Improve this question asked Feb 16, 2012 at 16:39 matt burnsmatt burns 25.5k13 gold badges111 silver badges108 bronze badges 2- 1 Note that jQuery itself doesn't parse XML at all - the browser does. – Pointy Commented Feb 16, 2012 at 16:40
- @Pointy, good point. This was failing in Chrome. I tried in FireFox and it parses ok. Now what can I do?!! Arg! – matt burns Commented Feb 16, 2012 at 16:58
3 Answers
Reset to default 5I've found the problem. As I suspected, there was some nasty unprintable character lurking at the end of the string.
I was able to remove it with this dirty bit of hacking:
xmlString = xmlString.substr(xmlString.indexOf("<"), xmlString.lastIndexOf(">") + 1);
If it's not obvious, it simply trims away anything from the start and end of the string that isn't the expected angle brackets of an xml document. The jQuery function "trim()" was not effective at removing the rogue character as that only does whitespace.
I don't know what the character was, and I'm not particularly happy with my solution, but I'm too busy to spend more time on it. Sigh.
By using this website http://www.xmlvalidation., I got the following error message, make sure you check the box "Validate against external XML schema "
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/1999/02/22-rdf-syntax-ns#">
3 <rdf:Description rdf:about=""
4 xmlns:photoshop="http://ns.adobe./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./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>
Maybe your problem is:
you can't use XML functions (in JS) on file that have no XML header.
try to open the XML file/input in the browser (type the path in the address bar),
and see if it opened as xml file or like text file.
for emample, adding XML header with php:
header ("Content-Type: text/xml");
本文标签: javascriptWhy is jquery39s parseXML function failing when the XML looks okStack Overflow
版权声明:本文标题:javascript - Why is jquery's parseXML function failing when the XML looks ok? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745441337a2658463.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论