PHP technique for parsing XML and generating SQL statements

Source: Internet
Author: User
Tags php example
This article mainly introduces the PHP implementation of parsing XML and generate SQL statement method, involving PHP for XML format file reading, parsing and SQL string splicing related operations skills, the need for friends can refer to the next

The example in this paper describes how PHP implements parsing XML and generating SQL statements. Share to everyone for your reference, as follows:

PHP parsing XML There are many ways, in the document, search for a lot of.

Today there is a need to extract the node attributes from an XML and then update the fields in a table in the database.

Ideas:

Parse XML, get all node properties –> Loop node collection, get corresponding properties –> splicing SQL string into an array –> save the array to a string in a file

XPath is used here, and two questions are encountered during code writing:

1. The history path attribute of XML is D:\xx\ ... Cannot load the file, instead "/" (the delimiter under Linux) is available.

2, get the properties of a node, using:: Attributes, the editor will keep the red hint, find half-day document, and finally with->getattribute () on the line (guess, because it is too strange, it supports->previoussibling and- >nodevalue), according to the document on the Domelement::getattribute directly on the error.

Here is the sample code:

<title>xml converted to Sql</title><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 "<p class=" Tip_ Info ' > found:<span> in total. $len. "  </span> nodes </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. '--$name. '--$inf. "  </p> ";  $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 "Generated! ";? >

Because the data is generated from a database table, the number of nodes found is the total number of records in the table. After the build, you can probably see if the content is correct, and then execute the SQL script to achieve the goal.

PS: Here are a few more online tools for you to refer to using XML operations:

Online XML/json Mutual Conversion Tool:
Http://tools.jb51.net/code/xmljson

Online formatting of XML/Online compressed XML:
Http://tools.jb51.net/code/xmlformat

XML Online Compression/formatting tools:
Http://tools.jb51.net/code/xml_format_compress

XML Code online format Beautification tool:
Http://tools.jb51.net/code/xmlcodeformat

Articles you may be interested in:

PHP implementation of the regular expression grouping capture operation example PHP tips

PHP implementation of QQ login principle and implementation process PHP instance

Automatic loading PHP Example of PHP file

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.