如何导入数据中包含撇号的XML数据

时间:2016-07-14 14:53:54

标签: php mysql xml

我有PHP代码,它采用每天更新的XML文件并将其导入MYSQL数据库。它工作得很好,直到其他数据我意识到,在他们的名字中有撇号的玩家不在数据库中。下面我列出了我目前使用的编码和XML示例。任何有关此事的帮助将不胜感激

xml在名称中使用撇号的播放器示例

<player>
<playername id="62583">Sa'Belle</playername>
<allianceid id="729"/>
<allianceroleid id="4749"/>
<race id="2"/>
</player>

编码我目前正在使用     

set_time_limit(0); // unlimited max execution time
require_once 'kodadb.php';

function Unaccent($string)
{
    if (strpos($string = htmlentities($string, ENT_QUOTES, 'UTF-8'), '&') !== false)
    {
        $string = html_entity_decode(preg_replace('~&([a-z]{1,2})(?:acute|cedil|circ|grave|lig|orn|ring|slash|tilde|uml);~i', '$1', $string), ENT_QUOTES, 'UTF-8');
    }
    return $string;
}
function getGenDate($file, $elemName,$db) {
        $count = 0;
    // Open the file
    $reader = new XMLReader();
    if (!$reader->open($file)) {
        die("Failed to open '" . $file . "'");
    }
    while ($reader->read()) {
        if ($reader->name === $elemName) {
            $count++;
            $doc = new DOMDocument;
            $node = simplexml_import_dom($doc->importNode($reader->expand(), true));
                $gendate = $node->datagenerationdatetime;
                return ($gendate);
          }
     }
     $reader->close();
     return ($gendate);
}

function xmlReader($file, $elemName,$db,$genDate) {
    $count = 0;
    // Open the file
    $reader = new XMLReader();
    if (!$reader->open($file)) {
        die("Failed to open '" . $file . "'");
    }
    while ($reader->read()) {
        if ($reader->name === $elemName) {
            $count++;
            $doc = new DOMDocument;
            $node = simplexml_import_dom($doc->importNode($reader->expand(), true));
            $player['allianceid'] = $node->allianceid['id'];
            $player['allianceroleid'] = $node->allianceroleid['id'];
            $player['race'] = $node->race['id'];
            $player['id'] = $node->playername['id'];
            $player["Player_name"] = $node->playername;
            if ($player['id'] !== null) {
                $sql = "INSERT INTO `players`(`player_id`, `Player_name`, `alliance_id`, `alliance_role_id`, `race_id`) VALUES ('" . $player['id'] . "','" . $player["Player_name"] . "','" . $player['allianceid'] . "','" . $player['allianceroleid'] . "','" . $player['race'] . "');";
                echo $sql."<br>";
                $db->query($sql);
                if ($db->connect_errno) {
                    printf("Connect failed: %s\n", $db->connect_error);
                    exit();
                }
            }
        }
    }
    $reader->close();
    return($count);
}
$rustart = getrusage();

$db = doDatabase();
$sql = "TRUNCATE TABLE `players`";
$db->query($sql);
$file = "./tmp/datafile_players.xml";
$elemName = 'server';
$genDate = getGenDate($file, $elemName,$db);

$elemName = 'player';
$numRows = xmlReader($file, $elemName,$db,$genDate);
//echo "Inserted ".$numRows. " records into 'players'.";

function rutime($ru, $rus, $index) {
    return ($ru["ru_$index.tv_sec"]*1000 + intval($ru["ru_$index.tv_usec"]/1000))
     -  ($rus["ru_$index.tv_sec"]*1000 + intval($rus["ru_$index.tv_usec"]/1000));
}

$ru = getrusage();
echo "This process used " . rutime($ru, $rustart, "utime") .
    " ms for its computations\n";
echo "It spent " . rutime($ru, $rustart, "stime") .
    " ms in system calls\n";

0 个答案:

没有答案