使用PHP解析XML数据并将其放入MySQL数据库

5

我被要求解析一个存储为XML文件的简单文件,然后将数据放入mysql数据库中。

然而,我完全不知道该怎么做,在网上查找所有给出的示例似乎对我的问题来说要么太复杂了,要么不是正确的解决方案。XML文件看起来像这样:

<shop>
<products>
    <product id="1" name="Cornetto" price="1.20" description="Traditional Cornetto" />
    <product id="2" name="Smarties" price="1.00" description="Smarties Icecream" />
</products>

<stocks>
    <stock id="1" amount="242" price="pounds" />
    <stock id="2" amount="11" price="pounds" />
</stocks>

我已经尝试查看了SimpleXML,我认为这是我必须走的方向,但我完全不知道怎么做。

任何帮助或指导都将非常感谢。


你在示例 XML 中忘记了闭合标签 )</shop>。 - Thirler
duplicate: https://dev59.com/_UvSa4cB1Zd3GeqPf6P7 - Gabriel Solomon
5个回答

5

我个人喜欢普通的XML格式,因为这样更易读,但是你可以这样使用:

$xmlstr = <<<XML
<?xml version='1.0' standalone='yes'?>
<shop>
<products>
    <product>
        <id>1</id>
        <name>Cornetto</name>
        <price>1.20</price>
        <description>Traditional Cornetto</description>
    </product>
    <product>
        <id>2</id>
        <name>Smarties</name>
        <price>1.00</price>
        <description>Smarties Icecream</description>
    </product>
</products>
<stocks>
    <stock>
        <id>1</id>
        <amount>242</amount>
        <price>pounds</price>
    </stock>
    <stock>
        <id>2</id>
        <amount>11</amount>
        <price>pounds</price>
    </stock>
</stocks>
</shop>
XML;

处理部分:

$xml = new SimpleXMLElement($xmlstr);
echo 'single value: <br />';
echo $xml->products->product[0]->id; // get single value

echo '<br /><br />';

//Loop trough multiple products
echo 'multiple values: <br />';
foreach($xml->products->product as $product)
{
    echo $product->id.' - ';
    echo $product->name.' - ';
    echo $product->price.' - ';
    echo $product->description;
    echo '<br/>';
}

4
假设文件名为data.xml$string = file_get_contents('data.xml')将整个文件读入$string中。 $xml = new SimpleXMLElement($string)解析该字符串,并将其转换为类似于实际文档的对象树。因此,如果这是文档-
<root>
  <b>
    <c>first</c>
    <c>second</c>
  </b>
</root>

SimpleXMLElement对象的使用方式如下:
$xml->b              // gets all children of b (c[0] and c[1])
print $xml->b->c[0]  // gets the first c, will print "first"

4
您可以使用例如SimpleXMLElementxpath
<?php
$xmlStr = <<<EOF
<?xml version="1.0"?>
<shop>
 <products>
    <product id="1" name="Cornetto" price="1.20" description="Traditional Cornetto" />
    <product id="2" name="Smarties" price="1.00" description="Smarties Icecream" />
 </products>
 <stocks>
    <stock id="1" amount="242" price="pounds" />
    <stock id="2" amount="11" price="pounds" />
 </stocks>
</shop>
EOF;

$xml=new SimpleXMLElement($xmlStr);

// get product line with xpath for example
$products=$xml->xpath("/shop/products/product");
if ($products) {
 // loop over each product node
 foreach ($products as $product) {
   // do whatever you want with the data
   echo("id=>".$product["id"].", name=>".$product["name"]."<br/>");
 }
}

// same for stock
// get product line with xpath for example
$stocks=$xml->xpath("/shop/stocks/stock");
if ($stocks) {
 // loop over each product node
 foreach ($stocks as $stock) {
   // do whatever you want with the data
   echo("id=>".$stock["id"].", amount=>".$stock["amount"]."<br/>");
 }
}

?>

1
$xml = simplexml_load_file($filename);
foreach($xml->product as $product) {            

  foreach($product->attributes() as $name => $attribute) {
    echo "$name = $attribute";
  }         
}

这对我没有任何帮助,我只收到了一个空白页面。也许我应该提到该文件有另一个元素,就像这样。 <商店> <产品> </产品> <库存> </库存> </商店>那样会有所不同吗? - Matthew

0
$xml = simplexml_load_file($filename);

foreach($xml->products->product as $not)
{
    foreach($not->attributes() as $a => $b)
    {
        echo $a,'="',$b,"\"<br />";
    }
}

你可能希望添加更多文本来描述你正在做什么以及为什么这样做。 - Sgoettschkes

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接