this article illustrates the way PHP implements the parsing of XML and generates a SQL statement. Share for you for your reference, the following:

php parsing XML there are many ways, the document has, a search for a lot of.

meets a requirement today: extracting the node properties in a XML and updating the fields in a table of the database.


XML analysis, thinking all the attributes of the > access node?; loop node set, to obtain the corresponding attribute? > SQL string into a mosaic array? > convert an array to a string stored in the

file in the

here the use of XPath, there are two problems in the process of writing code in the history of

1, the XML attribute of the path for D:xx... Load not to file "/" (separated by Linux under the symbol


) can obtain a node's attribute, use: attributes, red tip editor does not stop, find half document, and finally with -> (getAttribute) on the line (guess, because too strange, it supports -> and -> previousSibling; nodeValue), according to the document, the DOMElement:: getAttribute direct Baocuo..


the following is a sample code:

 < title> /title> XML converted to sql< < meta http-equiv='content-type'content='text/html; charset=utf-8' /> < style; type= "text/css" >.Tip_info {margin-bottom:10px;.Tip_info span {color:#f00 <}}; /style> <?php $xml = "D:/res/dressConfig.xml"; $doc = new DOMDocument(); $doc->load($xml); $xpath = new DOMXPath($doc); $query = "//i"; $entries = $xpath->query($query); $len = $entries->length; echo "<div class='tip_info'>总共找到:<span>".$len."</span>个节点</div>"; $arr = array(); $idx = 0; while ($idx < $len) {   $nodeItem = $entries->item($idx);   $id = $nodeItem->getAttribute("i");   $name = $nodeItem->getAttribute("n");   $inf = $nodeItem->getAttribute("inf"); //  echo "<div>".$id.'--'.$name.'--'.$inf."</div>";   $idx++;   array_push($arr, "update dress_item t SET t.s_name='".$name."',t.s_intro='".$inf."' WHERE t.n_doid=".$id.";"); } $dir = "d:/sql/"; if (!is_ Dir ($dir)) {MKDIR ($dir);} file_put_contents ("d:/sql/dress_item.sql", implode ("nr", $arr)); echo "finished! ";? > 

because the data is generated from the database table, so the number of nodes found is the total number of records in the table. It is possible to see if the content is correct, and then execute the SQL script again.

XML/ JSON conversion tools:

online format XML/ XML:

ht Tp://


PHP more about the related content of interested readers can see the special station: "PHP for XML" file operation summary, "PHP array (Array) technique," php string (string) " PHP error and exception handling methods," "target=" _blank ">PHP " basic grammar tutorial "php object-oriented programming tutorial," " Rget= "_blank" >php+mysql database operation introductory tutorial and "php, common database operation skills summary "

, I hope this article is helpful for everyone's program design.

This paper fixed link: | Script Home | +Copy Link

Article reprint please specify:PHP implements the method of parsing XML and generating SQL statements | Script Home

You may also be interested in these articles!