开发者

Take the content of the XML tags using DOM

I need to take the content of the tags (in the XML file) and save them in a MySql database, 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&g开发者_JS百科t;1</TYPE_OFFRE>
   <NO_ASP>3637017</NO_ASP>
   <NO_DOSSIER>00059</NO_DOSSIER>
   <NO_MANDAT>6523</NO_MANDAT>
</BIEN>
<BIEN>
....
</BIEN>
</BIENS>


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


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


Use SimpleXML, it is pretty easy to use it.

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜