英文:
Unmarshal XML with unescaped & character inside
问题
我有一个包含一个xml文件中的和号符号的url,我正在尝试解组它。我无法更改我得到的响应数据,所以我该如何转义它以便我可以解组它。
错误:第2行的XML语法错误:无效的字符实体&user(没有分号)
http://play.golang.org/p/XB8aPJY4Nw
英文:
I have a url with a ampersand symbol inside of a xml file that I am trying to unmarshal. I cannot change the response data I get so how can I escape it so that I can unmarshal it.
error: XML syntax error on line 2: invalid character entity &user (no semicolon)
答案1
得分: 8
在阅读了encoding/xml
文档后,我发现可以关闭严格模式,该模式强制执行字符值。在我的情况下,我知道将返回的常量,并且可以安全地这样做。感谢所有的回复。
这是来自文档的一个片段:
在属性值和字符数据中,未知或格式错误的字符实体(以
&
开头的序列)将保持不变。d.Strict = false;
英文:
After reading through the encoding/xml
docs I found that one can turn off strict mode which enforces the char values. In my case I know my constants that will be returned and am safe doing so. Thanks for all the responses.
This is a snippet from the docs:
> In attribute values and character data, unknown or malformed
> character entities (sequences beginning with &) are left alone.
>
> d.Strict = false;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论