很早以前写的代码,写出来以备忘

<?php
$mysql_server_name='localhost';
$mysql_username='';
$mysql_password='';
$mysql_database='';
$link = mysqli_connect($mysql_server_name,$mysql_username,$mysql_password,$mysql_database);
mysqli_set_charset ($link,'utf8');
if(!$link) {
printf("Can't connect to MySQL Server. Errorcode: %s ", mysqli_connect_error());
exit;
}
function CheckEmptyString($C_char){
    if (!is_string($C_char)) return false; 
    if (empty($C_char)) return false; 
    if ($C_char=='') return false; 
return true;
}
$xml = simplexml_load_file("nmap.xml");
foreach ($xml as $host => $value) {
    if((string)$host == "host"){
        $addr = (string) $value->address["addr"];
        echo $addr;
        $str1='';
        foreach ($value->ports->port as $port){
            if (CheckEmptyString($port) == false){
                $ports = $port["portid"].",";
                $str1 = $str1.$ports;
            }
            }
        $str2 = trim($str1,',');
        $sql = "insert into port (ip,port,date) VALUES ('$addr','$str2',now())";
            mysqli_query($link,$sql);
        }
    }
echo "\r\nend!!";
?>