我试图通过php脚本从网址获取xml信息,但是我遇到了一些麻烦,而且我的经验不足以覆盖这个区域。 XML就是这样形成的:
<?xml version="1.0" encoding="utf-8" ?>
<rss version="2.0" xmlns:g="http://base.google.se/ns/1.0">
<channel>
<title></title>
<description></description>
<link></link>
<item>
<g:id></g:id>
<title></title>
<g:product></g:product>
</item>
<item>
<g:id></g:id>
<title></title>
<g:product></g:product>
</item>
and so on...
使用php脚本:
<?php
include '../connection-to-db.php';
$str_xml = file_get_contents('http://www.example.com/xmls/xmlfile.xml');
$library = new SimpleXMLElement($str_xml);
$arr = json_decode( json_encode($library) , true);
var_dump ($arr);
echo "Array got " .sizeof($arr['item']) . " items.<br> <br>";
if (sizeof($arr['item']) > 155555500) {
mysql_query("TRUNCATE TABLE google_stat");
$count = 0;
foreach ($arr['item'] as $shelf)
{
$gId = mysql_real_escape_string($shelf['g:id']);
$Title = mysql_real_escape_string($shelf['title']);
$gProductType = mysql_real_escape_string($shelf['g:product']);
mysql_query("INSERT INTO google_stat (gid, title, gcategory)
VALUES ('$gID', '$Title', '$gCategory')")
or die(mysql_error());
$count ++;
}
echo " Counted: " . $count . "inserts";
} else {
echo "Non counted, no insert done";
}
?>
问题是,当SimpleXMLElement看起来所有带有g的项目时,当我看到输出时,名字就会消失,它甚至会对任何项目进行伪造。 我甚至尝试使用具有相同XML树的本地文件,甚至无法使其工作。 我感谢任何给予的帮助,因为我越来越意识到我对此深有感染。
更新:
<?php
include '../connection-to-db.php';
$str_xml = file_get_contents('http://www.example.com/xmls/xmlfile.xml');
$library = new SimpleXMLElement($str_xml);
$arr = json_decode( json_encode($library) , true);
echo "Array got " .sizeof($library->channel->item) . " items.<br> <br>";
if (sizeof($library->channel->item) > 100) {
mysql_query("TRUNCATE TABLE google_stat");
$count = 0;
foreach ($library->channel->item as $shelf)
{
$gId = (string) $shelf->children('g', TRUE)->id;
$Title = (string) $shelf->title;
$gProductType = $shelf->children('g', TRUE)->product;
echo $gId."<br />";
echo $Title."<br />";
echo $gProductType."<br />";
$count ++;
}
echo " Counted: " . $count . "inserts";
} else {
echo "Non counted, no insert done";
}
?>
现在我得到数组中的项目数,但是$ gId,$ Title等,不会回显任何值。
Edit2:必须进行高阵列检查,它才有效。
答案 0 :(得分:0)
它与您想要的命名空间前缀有关。您可以像这样访问g:项目:
<?php
$str_xml = file_get_contents('test.xml');
$library = new SimpleXMLElement($str_xml);
$count = 0;
foreach ($library->channel->item as $shelf)
{
$gId = (string) $shelf->title;
$Title = (string) $shelf->children('g', TRUE)->id;
$gProductType = (string) $shelf->children('g', TRUE)->product;
echo $gId."<br />";
echo $Title."<br />";
echo $gProductType."<br />";
$count ++;
}
echo " Counted: " . $count . " inserts";
?>
有关详细信息,请参阅https://www.sitepoint.com/simplexml-and-namespaces/。
xml.test
<?xml version="1.0" encoding="utf-8" ?>
<rss version="2.0" xmlns:g="http://base.google.se/ns/1.0">
<channel>
<title>product</title>
<description>lots of products</description>
<link>www.example.com</link>
<item>
<g:id>ID 1</g:id>
<title>Title 1</title>
<g:product>Product 1</g:product>
</item>
<item>
<g:id>ID 2</g:id>
<title>Title 2</title>
<g:product>Product 2</g:product>
</item>
</channel>
</rss>
输出:
标题1
ID 1
产品1
标题2
ID 2
产品2
计算:2次插入