2010-01-29 13 views
5

Mi è stato chiesto di analizzare un semplice file archiviato come file XML, i dati devono essere quindi inseriti in un database mysql.Analisi di dati XML tramite php da inserire nel database mysql

Tuttavia non ho assolutamente idea di cosa fare e dopo aver guardato online tutti gli esempi forniti sembrano troppo complicati per il mio problema o non la soluzione giusta. Il file XML è simile al seguente:

<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> 

Ho provato a guardare SimpleXML e penso che sia la direzione devo andare, ma ho solo non hanno idea.

Qualsiasi aiuto o suggerimento sarebbe fantastico.

+0

Hai dimenticato il tag di chiusura) nel tuo esempio xml – Thirler

+0

duplicato: http://stackoverflow.com/questions/2161722/parsing- xml-data-using-php-to-put-in-mysql-database –

risposta

4

Supponendo che il file si chiama data.xml

$string = file_get_contents('data.xml') legge l'intero file in $string.

$xml = new SimpleXMLElement($string); analizza tale stringa e la converte in un albero di oggetti simile al documento effettivo. Quindi, se questo è il documento -

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

L'oggetto SimpleXMLElement verrebbe utilizzato come:

$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

È possibile utilizzare ad esempio SimpleXMLElement e xpath

<?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

Personalmente, come la normale formattazione XMl quindi l'ho cambiato poiché è un po 'più leggibile, ma è così che puoi usarlo:

$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; 

parte di imballaggio:

$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/>'; 
} 
1
$xml = simplexml_load_file($filename); 
foreach($xml->product as $product) {    

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

Questo non ha fatto nulla per me. Ho appena ricevuto una pagina vuota. Forse avrei dovuto dire che il file aveva un altro elemento è così. Volesse che hanno fatto la differenza? – Matthew

0
$xml = simplexml_load_file($filename); 

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

È possibile aggiungere un altro testo per descrivere ciò che si sta facendo e perché. – Sgoettschkes

Problemi correlati