【发布时间】:2023-03-15 07:18:01
【问题描述】:
当我尝试解析 xml 网页并将数据写入 mysql 数据库表时,我收到错误 Fatal error: Uncaught Error: Call to a member function children() on string
我尝试使用以下代码来做到这一点 任何人都可以建议我解析 xml 网页端点并将数据写入 mysql 数据库表的正确方法
<?php
function sanitize_for_xml($input) {
// Convert input to UTF-8.
$old_setting = ini_set('mbstring.substitute_character', '"none"');
$input = mb_convert_encoding($input, 'UTF-8', 'auto');
ini_set('mbstring.substitute_character', $old_setting);
// Use fast preg_replace. If failure, use slower chr => int => chr conversion.
$output = preg_replace('/[^\x{0009}\x{000a}\x{000d}\x{0020}-\x{D7FF}\x{E000}-\x{FFFD}]+/u', '', $input);
if (is_null($output)) {
// Convert to ints.
// Convert ints back into a string.
$output = ords_to_utfstring(utfstring_to_ords($input), TRUE);
}
return $output;
}
?>
<?php
$affectedRow = 0;
$url ="https://ngcareers.com/xmlfeed.xml";
$xml = simplexml_load_file($url) or die("Error: Cannot create object");
$xml = sanitize_for_xml($xml);
echo "xml is ".$xml;
$xml = str_replace("[^\\x20-\\x7e]", "",$xml);
$xml = str_replace("<b>", "",$xml);
$xml = str_replace("<u>", "",$xml);
$xml = str_replace("</u>", "",$xml);
$xml = str_replace("<providerUrl>", "",$xml);
$xml = str_replace("</providerUrl>", "",$xml);
$xml = str_replace("‘", "",$xml);
$xml = str_replace("&", "",$xml);
$xml = str_replace("’", "",$xml);
$xml = str_replace("•", "",$xml);
$xml = str_replace("<strong>", "",$xml);
$xml = str_replace("</strong>", "",$xml);
$xml = str_replace("<p>", "",$xml);
$xml = str_replace("</p>", "",$xml);
$xml = str_replace("<br />", "",$xml);
$xml = str_replace("</b>", "",$xml);
$xml = str_replace("<li>", "",$xml);
$xml = str_replace("</li>", "",$xml);
$xml = str_replace("<ul>", "",$xml);
$xml = str_replace("</ul>", "",$xml);
$xml = str_replace("'", "",$xml);
$xml = str_replace(" ", "",$xml);
$xml = str_replace(""", "",$xml);
$xml = str_replace("<!--[endif]-->", "",$xml);
$xml = str_replace("<!--[if !supportLists]-->;", "",$xml);
foreach ($xml->children() as $row) {
$title = $row->title;
$link = $row->url;
$description = $row->description;
$date = $row->date;
$city = $row->city;
$state = $row->state;
$country = $row->country;
$company = $row->company;
$requirements= $row->requirements;
$category = $row->category;
$experience = $row->experience;
$salary = $row->salary;
$id = $row->id;
$sql = "INSERT INTO requirement(title,link,description,
jobdate,city,state,
country,company,requirements,
category,experience,salary,jobid) VALUES ('" . $title . "','" . $link . "','" . $description .
"','" . $date . "','" . $city . "','" . $state . "','" . $country . "','" . $company . "','" .
$requirements . "','" . $category . "','" . $experience . "','" . $salary . "','" . $id
. "')";
$result = mysqli_query($con, $sql);
if (! empty($result)) {
$affectedRow ++;
} else {
$error_message = mysqli_error($con) . "\n";
}
}
?>
<h2>Insert Job Data to Table </h2>
<?php
if ($affectedRow > 0) {
$message = $affectedRow . " records inserted";
} else {
$message = "No records inserted";
}
?>
【问题讨论】: