Home  >  Article  >  Backend Development  >  PHP method to parse xml and generate sql statement php tips

PHP method to parse xml and generate sql statement php tips

jacklove
jackloveOriginal
2018-06-28 17:31:361275browse

This article mainly introduces the method of php parsing xml and generating sql statements, involving php's operation skills related to reading, parsing and sql string splicing of xml format files. Friends in need can refer to the following

The example in this article describes how PHP parses XML and generates SQL statements. I share it with you for your reference. The details are as follows:

There are many ways for PHP to parse xml. There are many methods in the document. You can find a lot of them by searching.

I encountered a requirement today: extract the node attributes from a certain xml, and then update the fields in a certain table of the database.

Idea:

Parse the XML and get all the node attributes-> Loop the node collection and get the corresponding attributes-> Splice the sql string and store it in an array –> Convert the array to a string and save it in a file

Xpath is used here. Two problems were encountered during the process of writing the code:

1. The historical path of xml The file cannot be loaded when the attribute is D:\xx\..., just change it to "/" (the delimiter under Linux)

2. Get the attributes of a node, use ::attributes, and the editor will There are constant red prompts. I found the document for a long time, and finally used ->getAttribute() (I guess, because it is too strange, it supports ->previousSibling and ->nodeValue). According to the DOMElement:: on the document getAttribute reported an error directly..

The following is the sample code:

xml 转换为 sql


load($xml);
$xpath = new DOMXPath($doc);
$query = "//i";
$entries = $xpath->query($query);
$len = $entries->length;
echo "

总共找到:".$len."个节点

"; $arr = array(); $idx = 0; while ($idx < $len) { $nodeItem = $entries->item($idx); $id = $nodeItem->getAttribute("i"); $name = $nodeItem->getAttribute("n"); $inf = $nodeItem->getAttribute("inf"); // echo "

".$id.'--'.$name.'--'.$inf."

"; $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("\n\r", $arr)); echo "生成完毕!"; ?>

Because the data is from Generated from the database table, so the number of nodes found is the total number of records in the table. After generation, you can take a look to see if the content is correct, and then execute the sql script to achieve the purpose.

PS: Here are several online tools for xml operations for your reference:

## Online XML/JSON mutual conversion tool:
http://tools.jb51.net/code/xmljson

Online format TransformationXML/Online compressionXML
http://tools.jb51.net/code/xmlformat

XMLOnline compression/formatting tool:
http://tools.jb51.net/code/xml_format_compress

XML Code online formatting and beautification tool:
http://tools.jb51.net/code/xmlcodeformat

Articles you may be interested in:

PHP Implementation of Regular Expression Grouping Capture Operation Example PHP Tips

PHP realizes the opening principle and implementation process of QQ login php example

Detailed explanation of the automatic loading of PHP files php example

The above is the detailed content of PHP method to parse xml and generate sql statement php tips. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn