Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我的 Perl 程序正在处理一个 XML 文件。某些条目可能包含 & 符号。解析器中断,说:“实体中的名称无效”。
如何处理文件并在所有不正确的实体中编码 &-s?
所以,它会是这样的:
<words>text1 & text2</words> --> <words>text1 & text2</words>
它不是 XML 文件。如果是 XML,&则将写为&. 处理非 XML 文件很困难,因为您不能使用 XML 解析器。最好修复创建此文件的程序,将其更改为生成正确格式的 XML。
&
&
这很棘手,很重要,通常需要权衡取舍。当我遇到类似的问题时,用(和“尾随字符”)替换&后跟大写字符或空格(/\&[A-Z ]/在正则表达式中)&的字符可以解决大多数情况——这通常就足够了,因为你已经加倍努力了接受格式不正确的 XML 输入。
/\&[A-Z ]/