Home > Backend Development > PHP Tutorial > Implementation method of php parsing xml and generating sql statement

Implementation method of php parsing xml and generating sql statement

小云云
Release: 2023-03-19 21:18:01
Original
1849 people have browsed it

There are many ways for PHP to parse XML. There are a lot of them in the documentation. Just search for them.

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. This article mainly introduces you to the method of PHP parsing XML and generating SQL statements. It involves PHP's operation skills related to reading, parsing and SQL string splicing of XML format files. Friends in need can refer to it. I hope it can help you.

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 history path attribute of xml The file cannot be loaded when it is D:\xx\..., just change it to "/" (the delimiter under Linux)

2. Get the attributes of a node, use ::attributes, the editor will not The red prompt stops, find the half-day document, and finally use ->getAttribute() (guess, because it is too strange, it supports ->previousSibling and ->nodeValue), follow the DOMElement::getAttribute on the document An error was reported directly..

The following is the sample code:


##

<title>xml 转换为 sql</title>
<meta http-equiv=&#39;content-type&#39; content=&#39;text/html; charset=utf-8&#39; />
<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 "<p class=&#39;tip_info&#39;>总共找到:<span>".$len."</span>个节点</p>";
$arr = array();
$idx = 0;
while ($idx < $len) {
  $nodeItem = $entries->item($idx);
  $id = $nodeItem->getAttribute("i");
  $name = $nodeItem->getAttribute("n");
  $inf = $nodeItem->getAttribute("inf");
//  echo "<p>".$id.&#39;--&#39;.$name.&#39;--&#39;.$inf."</p>";
  $idx++;
  array_push($arr, "update dress_item t SET t.s_name=&#39;".$name."&#39;,t.s_intro=&#39;".$inf."&#39; 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 "生成完毕!";
?>
Copy after login

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

Related recommendations:

Method to automatically generate SQL statements

PHP multi-keyword and multi-field function to generate SQL statements

PowerDesigner links Oracle to generate sql statements

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

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template