Generating XML document in PHP (escape characters)

前端 未结 10 1004
难免孤独
难免孤独 2020-11-27 04:20

I\'m generating an XML document from a PHP script and I need to escape the XML special characters. I know the list of characters that should be escaped; but what is the corr

10条回答
  •  伪装坚强ぢ
    2020-11-27 05:03

    Proper escaping is the way to get correct XML output but you need to handle escaping differently for attributes and elements. (That is Tomas' answer is incorrect).

    I wrote/stole some Java code a while back that differentiates between attribute and element escaping. The reason is that the XML parser considers all white space special particularly in attributes.

    It should be trivial to port that over to PHP (you can use Tomas Jancik's approach with the above appropriate escaping). You don't have to worry about escaping extended entities if your using UTF-8.

    If you don't want to port my Java code you can look at XMLWriter which is stream based and uses libxml so it should be very efficient.

提交回复
热议问题