tags:

views:

52

answers:

3
+1  Q: 

using XML with PHP

I've got a little problem with an XML file. I need to take the content of the tags (in the XML file) and save them in a MySql data base , using PHP .

My supervisor asked me to use DOM, but everything I do isn't workin'.

My XML file looks like this :

<?xml version="1.0" encoding="windows-1252"?>
<BIENS>
<BIEN>
   <CODE_SOCIETE>0024</CODE_SOCIETE>
   <CODE_SITE>02</CODE_SITE>
   <TYPE_OFFRE>1</TYPE_OFFRE>
   <NO_ASP>3637017</NO_ASP>
   <NO_DOSSIER>00059</NO_DOSSIER>
   <NO_MANDAT>6523</NO_MANDAT>
</BIEN>
<BIEN>
....
</BIEN>
</BIENS>

So I would like Some help please. It's kind of urgent :s .

A: 

Try using SimpleXml. This way, you can simply loop over the BIENs with foreach, and get the items like $sitecode = $biens->CODE_SITE

Sjoerd
A: 

Use SimpleXML, it is pretty easy to use it.

http://www.php.net/manual/en/simplexml.examples-basic.php

Flakron Bytyqi
+2  A: 

Basic usage example for your XML to iterate over all BIEN elements and it's child nodes:

$dom = new DOMDocument;
$dom->preserveWhiteSpace = FALSE;
$dom->load('file.xml');
foreach($dom->getElementsByTagName('BIEN') as $bien) {
    foreach($bien->childNodes as $childNode) {
        echo $childNode->tagName . '=>' . $childNode->nodeValue . PHP_EOL;
    }
}

Should be trivial to exchange the echo'ing code to insert into your database instead.

Basic usage of DOM has been covered extensively on StackOverflow, so you should not have a problem finding further usage examples (linked are mine).

Gordon
+1 for actually using DOM.
LeguRi