业务中遇到要解析xml的情况:
    先来看下xml数据的结构:

    1. <?xml version="1.0" encoding="utf-8"?>
    2. <string xmlns="http://tempuri.org/">{
    3. "code": "200",
    4. "message": "",
    5. "allCount": "3",
    6. "data": [
    7. {
    8. "name": "zhangsan",
    9. "sex": 0
    10. },
    11. {
    12. "name": "lisi",
    13. "sex": 1
    14. },
    15. {
    16. "name": "wangwu",
    17. "sex": 0
    18. },
    19. ]
    20. }</string>

    最初用的以下这种方法实现了:

    1. $res = file_get_contents("test.xml");
    2. print_r(xmlToArray($res));
    3. function xmlToArray($res){
    4. $r = simplexml_load_string($res);
    5. return json_decode($r[0],true);
    6. }

    但后来发现,如果数据量很大时,你想象我data中有50000多条数据,整个文件在30MB左右时出现个错误:
    simplexml_load_string(): Entity: line 307585: parser error : xmlSAX2Characters: huge text node
    image.png

    经过多方找资料,后解决办法如下(参考资料:https://www.cnblogs.com/caicaizi/p/7837286.html):

    1. $res = file_get_contents("test.xml");
    2. print_r(xmlToArray2($res));
    3. public static function xmlToArray2($res){
    4. $p = xml_parser_create();
    5. xml_parser_set_option($p, XML_OPTION_CASE_FOLDING, 1);
    6. xml_parser_set_option($p, XML_OPTION_SKIP_WHITE, 1);
    7. xml_parse_into_struct($p, $res, $vals, $index);
    8. xml_parser_free($p);
    9. $jsonString = $vals[$index['STRING'][0]]['value'];
    10. return json_decode($jsonString,true);
    11. }