XML到PHP数组到mysql

时间:2014-06-15 17:13:26

标签: php xml simplexml

我正在尝试使用简单的xml从google xml文档导入xml数据来实现这一点,代码示例就在这里

<entry>
    <id>
        tag:google.com,2013:googlealerts/feed:11187837211342886856
    </id>
    <title type="html">
        <b>London</b> Collections: Topman Design&#39;s retro mash-up
    </title>
    <link href="https://www.google.com/url?q=http://www.telegraph.co.uk/men/fashion-and-style/10901146/London-Collections-Topman-Designs-retro-mash-up.html&ct=ga&cd=CAIyAA&usg=AFQjCNEib0lLtkzUzFtR2Hk37wGefTVAZQ"/>
    <published>2014-06-15T14:15:00Z</published>
    <updated>2014-06-15T14:15:00Z</updated>
    <content type="html">
        Today is a very important day for England, and I&#39;m not referring to the World Cup; it&#39;s the first day of <b>London</b> Collections: Men, a three day celebration ...
    </content>
    <author>
        <name/>
    </author>
</entry>

这样做的最佳解决方案是什么?我对如何将每个变量传递给mysql

感到困惑

这正是我被困的地方

$xml = simplexml_load_file("xml.xml");
$feed = simplexml_load_string($xml);
$ns=$feed->getNameSpaces(true);

foreach ($feed->entry as $entry) {

}

提前谢谢大家

1 个答案:

答案 0 :(得分:1)

您可以使用XPath。当你有命名空间时,它可能比 SimpleXML 更简单。您还必须注册名称空间,该名称空间不包含在您包含的摘要中作为示例。

我在这里找到了一个任意的Feed:http://www.google.com/alerts/feeds/01662123773360489091/16526224428036307178

<feed xmlns="http://www.w3.org/2005/Atom" xmlns:idx="urn:atom-extension:indexing">
    <id>
        tag:google.com,2005:reader/user/01662123773360489091/state/com.google/alerts/16526224428036307178
    </id>
    <title>Google Alert - test</title>
    <link href="http://www.google.com/alerts/feeds/01662123773360489091/16526224428036307178" rel="self"/>
    <updated>2014-06-15T17:30:04Z</updated>
    <entry>
        <id>
            tag:google.com,2013:googlealerts/feed:5957360885559055905
        </id>
        <title type="html">
            Dad&#39;s <b>Test</b> Out Products Made For the Family
        </title>
        <link href="https://www.google.com/url?q=http://gma.yahoo.com/video/dads-test-products-made-family-141428658.html&ct=ga&cd=CAIyAA&usg=AFQjCNHHBPoS6Poz-Y5A3vFfbsGL3fkrBA"/>
        <published>2014-06-15T17:30:04Z</published>
        <updated>2014-06-15T17:30:04Z</updated>
        <content type="html">
            Watch the video Dad&#39;s <b>Test</b> Out Products Made For the Family on Yahoo Good Morning America . Becky Worley enlists a group of fathers to see if &quot;As ...
        </content>
        <author>
            <name/>
        </author>
    </entry>
    <entry>
    ...

我会用它来提供你的答案。

在第一行中有一个默认的名称空间声明xmlns。您必须在PHP中注册它以在XPath中使用命名空间。即使原始文件中没有前缀,您也应该将其映射到前缀(可以是任何一个)。所以这就是你如何初始化解析器。

这两行初始化DOM解析器并解析文件,从Internet加载:

$document = new DOMDocument(); 
$document->load( "http://www.google.com/alerts/feeds/01662123773360489091/16526224428036307178" );

这两个初始化XPath环境,使用前缀(我选择atom)注册文件的默认命名空间:

$xpath = new DOMXpath($document);
$xpath->registerNamespace("atom", "http://www.w3.org/2005/Atom"); 

设置完成后,您可以使用evaluate()表达式选择节点,该表达式可以是绝对值或相对值。要获取所有条目节点,可以使用绝对表达式:

$entries = $xpath->evaluate("//atom:entry");

XPath表达式为//atom::entry。它从entry命名空间返回一组"http://www.w3.org/2005/Atom"个节点,这就是你想要的。

要在每个entry的上下文中提取节点和信息,您可以使用DOM方法和属性,例如firstChildnextSibling等,或者您可以执行其他XPath 上下文搜索。上下文搜索将上下文节点作为第二个参数传递给evaluate()表达式。这是一个循环,它获取<entry>的每个子节点中的数据并将其放在HTML子列表中:

$entries = $xpath->evaluate("//atom:entry");
echo '<ul>'."\n";
foreach ($entries as $entry) {
    echo '<li><b>Entry ID: '.$xpath->evaluate("atom:id/text()", $entry)->item(0)->nodeValue.'</b></li>'."\n";
    echo '<ul>'."\n";
    echo '<li>Title: '.$xpath->evaluate("atom:title/text()", $entry)->item(0)->nodeValue.'</li>'."\n";
    echo '<li>Link: '.$xpath->evaluate("atom:link/@href", $entry)->item(0)->nodeValue.'</li>'."\n";
    echo '<li>Published: '.$xpath->evaluate("atom:published/text()", $entry)->item(0)->nodeValue.'</li>'."\n";
    echo '<li>Updated: '.$xpath->evaluate("atom:updated/text()", $entry)->item(0)->nodeValue.'</li>'."\n";
    echo '<li>Content: '.$xpath->evaluate("atom:content/text()", $entry)->item(0)->nodeValue.'</li>'."\n";
    echo '<li>Author: '.$xpath->evaluate("atom:author/atom:name/text()", $entry)->item(0)->nodeValue.'</li>'."\n";
    echo '</ul>'."\n";
}
echo '</ul>'."\n";

请注意,表达式是 relative entry(它们不以/开头),元素选择器也是前缀(它们也属于 atom namespace),我使用item(0)nodeValue来提取结果。由于节点可能包含许多子节点,因此上面使用的evaluate()表达式将返回节点集。如果只有一个文本子项,则它位于item(0)中。 nodeValue将其转换为字符串。

运行上述程序的结果将是:

<ul>
  <li><b>Entry ID: tag:google.com,2013:googlealerts/feed:5957360885559055905</b></li>
  <ul>
    <li>Title: Dad&#39;s <b>Test</b> Out Products Made For the Family</li>
    <li>Link: https://www.google.com/url?q=http://gma.yahoo.com/video/dads-test-products-made-family-141428658.html&ct=ga&cd=CAIyAA&usg=AFQjCNHHBPoS6Poz-Y5A3vFfbsGL3fkrBA</li>
    <li>Published: 2014-06-15T17:30:04Z</li>
    <li>Updated: 2014-06-15T17:30:04Z</li>
    <li>Content: Watch the video Dad&#39;s <b>Test</b> Out Products Made For the Family on Yahoo Good Morning America . Becky Worley enlists a group of fathers to see if &quot;As ...</li>
    <li>Author: </li>
  </ul>
  <li><b>Entry ID: tag:google.com,2013:googlealerts/feed:11008408359408830921</b></li>
  <ul>
    <li>Title: Germany faces major <b>test</b> of strength in its World Cup opener against Portugal</li>
    <li>Link: https://www.google.com/url?q=http://www.foxnews.com/sports/2014/06/15/germany-faces-major-test-strength-in-its-world-cup-opener-against-portugal/&ct=ga&cd=CAIyAA&usg=AFQjCNHOU94QyciRpCEdJawOwl3diEEO0A</li>
    <li>Published: 2014-06-15T16:18:45Z</li>
    <li>Updated: 2014-06-15T16:18:45Z</li>
    <li>Content: Cristiano Ronaldo stretches during a training session of Portugal in Campinas, Brazil, Saturday, June 14, 2014. Portugal plays in group G of the Brazil ...</li>
    <li>Author: </li>
  </ul>
  <li><b>Entry ID: tag:google.com,2013:googlealerts/feed:8664961950651004785</b></li>
  ...

现在,您可以编辑代码以使其适应您要提取的数据。

您可以在此 PHP Fiddle

中查看此应用程序的工作示例