簡體   English   中英

使用php解析XML數據放入mysql數據庫

[英]Parsing XML data using php to put into mysql database

我被要求解析一個存儲為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格式,所以我更改了它,因為它更具可讀性,但這是你如何使用它:

$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/>';
}

假設該文件名為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"

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

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

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM