Android Studio编译XML报错:URL中&符号致格式无效求助
& in Android Studio Alright, I’ve been in your shoes dealing with XML validation errors for unmodifiable URLs (especially Amazon affiliate links like yours). Here’s the simplest, safest fix to get past the error without changing your URL content:
Wrap the URL in a CDATA Section
XML parsers flip out when they see entity-like strings even if they’re escaped properly, but a CDATA section tells the parser to treat everything inside as plain text—no parsing, no validation checks for markup. Perfect for your case since you can’t alter the URL.
Here’s Your Modified XML
<?xml version="1.0" encoding="UTF-8"?> <Items> <Item> <DetailPageURL> <![CDATA[https://www.amazon.de/Chemie-76128-Felgenreiniger-Alu-Teufel-Spezial/dp/B004LAXG9C?psc=1&SubscriptionId=mykey&tag=mytag&linkCode=xm2&camp=2025&creative=165953&creativeASIN=B004LAXG9C]]> </DetailPageURL> <URL>https://images-eu.ssl-images-amazon.com/images/I/412-16OaNFL._SL160_.jpg</URL> <Feature>Der neue "Alu-Teufel-Spezial" ist ein moderner Hochleistungs-Felgenreiniger. Die neue, säurefreie Rezeptur ermöglicht die problemlose Reinigung von Stahl- und Aluminiumfelgen sowie Radkappen. </Feature> <FormattedPrice>EUR 13,50</FormattedPrice> <Title>Tuga Chemie 76128 Felgenreiniger Alu-Teufel Spezial</Title> </Item> </Items>
Why This Works
- The
<![CDATA[...]]>block acts as a "raw text" wrapper. The XML parser won’t try to interpret&(or any other sequences) as XML markup, so it won’t throw that "invalid token" error. - When you read the
DetailPageURLvalue in your Android code, it’ll still have the original&strings—which will correctly resolve to&when you use the URL to open a web page or make a network request.
Avoid This Quick Fix (It’s Risky)
Some people suggest disabling XML validation in Android Studio to make the error go away, but I’d strongly advise against it. Turning off validation means you’ll miss real issues like missing tags or malformed XML later on. Save yourself the headache and use CDATA instead.
内容的提问来源于stack exchange,提问作者Blnpwr

