简体   繁体   中英

Best Way to Store XML in MySQL

I'm getting an XML API response, parsing the data I need, but want to store full XML response in MySQL for add'l data use later.

I intially stored the XML in a BLOB, but found special characters sometimes in values break the INSERT.

So, I first convert XML with htmlentities into a BLOB to keep original API response data integrity. Is this a good way to do it, or is there a better method?

Properly escaped, any data can be INSERTed into a BLOB . htmlentities is not the way. For the mysqli API, use mysqli::real_escape_string. For PDO use binding.

If you are running on Windows, be sure to fetch the data in binary mode, else CR/LF may get "fixed" during the read.

In theory XML should be UTF-8, so simply putting into TEXT CHARACTER SET utf8mb4 should work instead of BLOB .

If all else fails, consider converting (in the client) the blob into Base64. It will be 4/3 bigger.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM