【问题标题】:Getting XML file from another domain to Mysql database从另一个域获取 XML 文件到 Mysql 数据库
【发布时间】:2017-12-18 15:29:22
【问题描述】:

我正在尝试从要通过 php 脚本导入 mysql 的 url 获取 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:必须进行高位数组检查,才有效。

【问题讨论】:

标签: php mysql json xml


【解决方案1】:

它与你想要的命名空间前缀有关。您可以像这样访问 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 次插入

【讨论】:

  • 非常感谢!!但是由于某种原因我无法呼应这些值?
  • 你能告诉我你到目前为止做了什么,也许我可以帮助诊断这个问题吗?
  • 我使用了你刚刚给我看的代码,现在它计算了 6258 个项目而不是 0,所以这很有效。但试图呼应标题或 ID 或产品根本不会显示任何内容。所以 $library->channel->item 可以正常工作,因为它会将它们全部计算在内。一会儿我会更新。
  • 刚刚意识到我自己的错误......我添加的数组检查在一切正常之前不会打印到数据库......哈哈谢谢你的帮助!
  • 太棒了!很高兴你已经弄清楚了!
猜你喜欢
  • 1970-01-01
  • 2012-10-07
  • 2015-06-13
  • 1970-01-01
  • 2016-10-18
  • 1970-01-01
  • 2020-09-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多