php 使用php解析XML数据放入mysql数据库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2161722/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 05:17:29  来源:igfitidea点击:

Parsing XML data using php to put into mysql database

phpmysqlxmlparsing

提问by Matthew

I have been asked to parse a simple file which is stored as an XML file, the data is to be then put into a mysql database.

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

However I have absolutely no clue what to do and after looking online all the examples given seem either too complicated for my problem or not the right solution. The XML file looks like this:

但是,我完全不知道该怎么做,并且在网上查看后,所有给出的示例对于我的问题来说似乎都太复杂了,或者不是正确的解决方案。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>

I've tried looking at SimpleXML and I think that's the direction I have to go but I just have no idea.

我试过查看 SimpleXML,我认为这是我必须走的方向,但我不知道。

Any help or pointers would be great.

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

回答by RJD22

I personally like the normal XMl formatting so I changed it since its a bit more readable but this is how you can use it:

我个人喜欢普通的 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;

Handling part:

处理部分:

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

回答by Anurag

Assuming the file is called data.xml

假设文件被调用 data.xml

$string = file_get_contents('data.xml')reads the entire file into $string.

$string = file_get_contents('data.xml')将整个文件读入$string.

$xml = new SimpleXMLElement($string);parses that string, and converts it into an object tree similar to the actual document. So if that's the document -

$xml = new SimpleXMLElement($string);解析该字符串,并将其转换为类似于实际文档的对象树。所以如果那是文件——

<root>
  <b>
    <c>first</c>
    <c>second</c>
  </b>
</root>

The SimpleXMLElement object would be used like:

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"

回答by Patrick

You can use for example SimpleXMLElementand xpath

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

?>

回答by Thomas

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

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

回答by Abid Malik

$xml = simplexml_load_file($filename);

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