【问题标题】:Parsing XML data using php to put into mysql database使用php解析XML数据放入mysql数据库
【发布时间】:2011-01-10 19:53:10
【问题描述】:

我被要求解析一个存储为 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,我认为这是我必须走的方向,但我只是不知道。

任何帮助或指示都会很棒。

【问题讨论】:

标签: php mysql xml parsing


【解决方案1】:
$xml = simplexml_load_file($filename);

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

【讨论】:

  • 您可能需要添加更多文字来描述您正在做什么以及为什么。
【解决方案2】:
$xml = simplexml_load_file($filename);
foreach($xml->product as $product) {            

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

【讨论】:

  • 这对我没有任何帮助。我刚得到一个空白页。也许我应该提到该文件有另一个元素是这样的。 会有什么不同吗?
【解决方案3】:

我个人喜欢正常的 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】:

    您可以使用例如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/>");
     }
    }
    
    ?>
    

    【讨论】:

      【解决方案5】:

      假设文件名为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"
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-02-07
        • 2013-03-27
        • 1970-01-01
        • 2010-12-17
        • 1970-01-01
        • 2016-07-26
        • 2013-07-08
        • 2020-07-22
        相关资源
        最近更新 更多