NSXMLParser 错误,如何摆脱 XML 标签内的无效字符?
大家好,提前感谢您的帮助。
情况是这样的: 我正在使用一个 Web 服务,该服务通过以下方式返回肥皂消息:
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<getMessagesResponse xmlns="urn:DefaultNamespace">
<getMessagesReturn xmlns="">
<?xml version="1.0" encoding="ISO-8859-1" ?>
<contact>
A message with escaped values like & < >
</contact>
</getMessagesReturn>
</getMessagesResponse>
</soapenv:Body>
</soapenv:Envelope>
我使用 NSUTF8StringEncoding 读取 getMessagesReturn 子项,它生成以下内容
<?xml version="1.0" encoding="ISO-8859-1">
<contact>
A message with escaped values like & < >
</contact>;
: < >当然,NSXMLParser 会抛出错误,因为这些字符在 XML 标记内是无效的。
我的问题是,我怎样才能避免这种情况?在将信息传递给解析器之前,有没有办法只转义标签消息内容?
任何帮助将不胜感激。
编辑我使用:
[NSString stringWithCString:(char*)elementText encoding:NSUTF8StringEncoding];
Hi everyone and thanks in advance for your help.
This is the situation:
I'm consuming a webservice that returns me a soap message in the following way:
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<getMessagesResponse xmlns="urn:DefaultNamespace">
<getMessagesReturn xmlns="">
<?xml version="1.0" encoding="ISO-8859-1" ?>
<contact>
A message with escaped values like & < >
</contact>
</getMessagesReturn>
</getMessagesResponse>
</soapenv:Body>
</soapenv:Envelope>
I use NSUTF8StringEncoding to read the getMessagesReturn child and it generates me the following:
<?xml version="1.0" encoding="ISO-8859-1">
<contact>
A message with escaped values like & < >
</contact>;
My problem is that it also unescape the & < > inside the contact tag, and of course the NSXMLParser throws an error because these are invalid characters inside a XML tag.
My Question is, How can I avoid this? Is there a way to escape back only the tags message contents before passing the info to the Parser?
Any help would be greatly appreciated.
Edit I use:
[NSString stringWithCString:(char*)elementText encoding:NSUTF8StringEncoding];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您控制网络服务吗?传递 getMessageReturn 的正确方法是使用 CDATA。否则,正确的编码将是这样的(注意消息本身和额外的 &),
但 CDATA 更容易,这就是它的用途。如果没有别的办法,您可以在解析之前使用字符串替换来插入 CDATA。
Do you control the web service? The correct way to pass getMessageReturn is with a CDATA. Otherwise, the correct encoding would be like this (note the message itself and the extra &'s)
But CDATA is much easier, and this is what it's for. If nothing else, you can use string substitution to insert the CDATA before parsing.