nsxmlparser 无法解决 '

发布于 2024-08-11 22:49:34 字数 1234 浏览 3 评论 0原文

我使用 NSXMLParser 来剖析 xml 包,我在包文本中接收到 &apos 。

我为 xmlParser 定义了以下内容:

[xmlParser setShouldResolveExternalEntities: YES];

永远不会调用以下方法

- (void)parser:(NSXMLParser *)parser foundExternalEntityDeclarationWithName:(NSString *)entityName publicID:(NSString *)publicID systemID:(NSString *)systemID

解析器不考虑 &apos 之前字段中的文本。

我正在寻找如何解决这个问题,有什么想法吗???

提前致谢 附上Alex

XML 包部分:

<?xml version="1.0" encoding="ISO-8859-1"?><SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:tns="urn:appwsdl"><SOAP-ENV:Body><ns1:getObjects2Response xmlns:ns1="http://schemas.xmlsoap.org/soap/envelope/"><return xsi:type="tns:objectsResult"><totalRecipes xsi:type="xsd:string">1574</totalObjects><Objects xsi:type="tns:Item"><id xsi:type="xsd:string">4311</id><name xsi:type="xsd:string"> item title 1 </name><procedure xsi:type="xsd:string">item procedure 11......

Im using NSXMLParser to dissect a xml package, I'm receiving &apos inside the package text.

I have the following defined for the xmlParser:

[xmlParser setShouldResolveExternalEntities: YES];

The following method is never called

- (void)parser:(NSXMLParser *)parser foundExternalEntityDeclarationWithName:(NSString *)entityName publicID:(NSString *)publicID systemID:(NSString *)systemID

The text in the field before the &apos is not considered by the parser.

Im searching how to solve this, any idea???

Thanks in advance
Alex

XML package portion attached:

<?xml version="1.0" encoding="ISO-8859-1"?><SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:tns="urn:appwsdl"><SOAP-ENV:Body><ns1:getObjects2Response xmlns:ns1="http://schemas.xmlsoap.org/soap/envelope/"><return xsi:type="tns:objectsResult"><totalRecipes xsi:type="xsd:string">1574</totalObjects><Objects xsi:type="tns:Item"><id xsi:type="xsd:string">4311</id><name xsi:type="xsd:string"> item title 1 </name><procedure xsi:type="xsd:string">item procedure 11......

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

萌面超妹 2024-08-18 22:49:35

这就是我在参考这里的不同答案后所做的。
当从 NSURLConnection 对象接收数据时,我用 "'" 替换了 xml 中所有出现的 ' 。然后我将该数据提供给解析器。

所以我要做的是:

NSData* parserData = [self resolveHTMLEntities: self.receivedData];
NSXMLParser* parser = [[NSXMLaParser alloc] initwithData:parserData];

这是resolveHTMLEntitites 方法:

NSString *xmlCode = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSMutableString *temp = [NSMutableString stringWithString:xmlCode];

// Replace all the entities
[temp replaceOccurrencesOfString:@"&apos;" withString:@"'" options:NSLiteralSearch range:NSMakeRange(0, [temp length])];

NSData *finalData = [temp dataUsingEncoding:NSUTF8StringEncoding];
return finalData;

问题是 ' 被转换为 &apos; 这就是为什么我们需要替换它发生。

注意:上面的代码块中没有执行内存管理。

希望这有帮助。

Here is what I did, after referring a different answer from here.
I replaced all the occurrences of the ' in the xml with "'" when the data is received from NSURLConnection object. Then I give that data to the parser.

So what I do is:

NSData* parserData = [self resolveHTMLEntities: self.receivedData];
NSXMLParser* parser = [[NSXMLaParser alloc] initwithData:parserData];

Here is the resolveHTMLEntitites method:

NSString *xmlCode = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSMutableString *temp = [NSMutableString stringWithString:xmlCode];

// Replace all the entities
[temp replaceOccurrencesOfString:@"&apos;" withString:@"'" options:NSLiteralSearch range:NSMakeRange(0, [temp length])];

NSData *finalData = [temp dataUsingEncoding:NSUTF8StringEncoding];
return finalData;

The catch is that ' gets converted to &apos; thats why we need to replace that occurrence.

Note: No memory management is performed in the above block of code.

Hope this helps.

千里故人稀 2024-08-18 22:49:35

标准实体为 <>&". ' 是 html 实体引用。您的 XML 是否引用了 XHTML 命名空间或定义了 ' 的其他命名空间?

(顺便说一句,如果能看到一小段 XML 包括标头,那就太好了。)

The standard entities are <, >, &, and ". ' is an html entity reference. Does your XML refer to the XHTML namespace or some other namespace that has ' defined?

(BTW, would be nice to see a small segment of the XML including the header.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文