简体   繁体   English

使用NSXMLParser的XML属性中的特殊字符

[英]Special characters in XML attributes with NSXMLParser

I get something like the following from a third party API: 我从第三方API获得以下内容:

<el attr="test.

another test." />

I use NSXMLParser to read the file into my app. 我使用NSXMLParser将文件读入我的应用程序。

However, in the delegate, the attribute gets converted to test. another test. 但是,在委托中,属性被转换为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: 最初,我认为这是一个错误,但是根据XML Spec,它做对了:

a whitespace character (#x20, #xD, #xA, #x9) is processed by appending #x20 to the normalized value 通过将#x20附加到归一化值来处理空白字符(#x20,#xD,#xA,#x9)

( See section 3.3.3. ) 见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. 请注意,它是第三方API,因此即使错误,也无法更改。

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 :) 只要您喜欢[:newline:]换行符即可:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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