简体   繁体   中英

Special characters in XML attributes with NSXMLParser

I get something like the following from a third party API:

<el attr="test.

another test." />

I use NSXMLParser to read the file into my app.

However, in the delegate, the attribute gets converted to test. another test. test. another test. Obviously I'd like to see it with the line breaks intact.

I initially assumed that this was a bug but, according to the XML Spec it's doing the right thing:

a whitespace character (#x20, #xD, #xA, #x9) is processed by appending #x20 to the normalized value

( See section 3.3.3. )

What are my options? Note that it's a third party API so I can't change it, even though it's wrong.

NSData *xmlData = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"url://"]];
NSMutableString *myXmlStr = [[NSMutableString alloc] initWithData:xmlData encoding:NSUTF8StringEncoding];
NSRange range = [myXmlStr rangeOfString:@"\n"];
[myXmlStr replaceCharactersInRange:range withString:@"[:newline:]"];
NSData *newXmlData = [myXmlStr dataUsingEncoding:NSUTF8StringEncoding];

Just replace [:newline:] with new line character whenever u like it :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM