The HTML document which I am parsing contains some ASCII control codes. I noticed that PHP's DOMDocument parser truncates text nodes when it finds ASCII control characters within the node, such as
Device Control 0x13
End of Medium 0x19
File Separator 0x1C
Group Separator 0x1D
Is this a bug or a feature? Is there any way to have DOMDocument act otherwise? I resorted to remove this characters before DOM processing, but I wonder if that's the right开发者_如何学Python solution.
Probably both a bug and a feature.
XML 1.0 is very restrictive about the ASCII control characters that it will accept. So it seems like your DOMDocument is trying to protect you from yourself by truncating (although it should return some indication of a problem, so I'd call that a bug).
XML 1.1 is less restrictive; the only thing that it doesn't allow is NUL. So, one possible solution is to configure your DOMDocument object so that it knows it should be managing 1.1.
Edit: it looks like you can pass the XML version number to the DOMDocument constructor (but I'm not a PHP programmer, so don't know if I'm reading the docs correctly).
Edit 2: I just reread your question, and realized that your parsing, not constructing. If you prepend a valid 1.1 prologue to the input, that should be a workaround. Or perhaps by constructing the DOMDocument with the correct version number, it will parse correctly without that prologue.
To add to what kdgregory said about allowable characters, those control characters are not just invalid in XML, but also in SGML. So legacy HTML or XHTML, you're not ever going to be able to parse any of those characters in an HTML document.
(And I have no idea why you would want to. Binary data doesn't belong in [X][HT]ML.)
精彩评论