Importing XML Data to a Database

本文介绍了使用PHP将XML数据导入数据库的方法。包括连接数据库、解析XML文档、插入数据、关闭连接等步骤,还提到了使用DOM和SAX两种解析方式,以及一种不指定表名和字段名的导入方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Importing XML Data to a Database

In addition to exporting database records to XML documents, PHP lets you import data from an XML document into a database. Using the DOM and SAX approach, you can construct SQL queries and insert data into a database from an XML document. To import XML data into a database:

  1. Parse the XML data using the DOM or the SAX approach.

  2. Create a list of field value pairs.

  3. Insert the field value pairs in the database.

  4. Run the database query to test whether data is inserted or not and close the database connection.

Connecting to a Database to Import Data

You need to establish a connection with the MySQL database to import data from an XML document to the database. The following code shows how to connect to the MySQL database:

$connection = mysql_connect($hostname, $username, $password) or die ("Unable to connect!");
mysql_select_db($db) or die ("Unable to select database!");

In the above code, a connection to the database is established using the mysql_connect() function. You need to specify the hostname, username, and password as arguments to the mysql_connect() function.

Parsing an XML Document

To import XML document into a database, you need to parse the XML document. You can parse the XML document using both the SAX and DOM approach. For example, to import the XML document, student.xml file, that stores the student information in a database, you need to parse the student.xml file.

Listing 8-8 shows the contents of the student.xml file:

Listing 8-8: Content of the student.xml File
<?xml version="1.0" encoding="UTF-8"?>
<list>
<item>
<name>John</name>
<age>15</age>
<address>New York</address>
<standard>10</standard>
</item>
<item>
<name>Joseph</name>
<age>16</age>
<address>New York</address>
<standard>12</standard>
</item>
<item>
<name>Mary</name>
<age>14</age>
<address>New Jersey</address>
<standard>8</standard>
</item>
</list> 

The above listing shows the content of the student.xml file that stores student information, such as name, age, address, and standard.

You need to create a table in the database that can store the data imported from the student.xml file.

Listing 8-9 shows how to create the student table in the information database:

Listing 8-9: Creating the Student Table
use information
mysql> CREATE TABLE student
-> (
-> name Varchar(30) NOT NULL,
-> age Integer NOT NULL,
-> address Varchar(30) NOT NULL,
-> standard Integer
-> );

The above listing creates the student table, in which you can insert data from the student.xml file.

Listing 8-10 shows how to import the student.xml file into a database, by parsing the student.xml file using the SAX approach:

Listing 8-10: Parsing the student.xml File using SAX
<?php	
$cTag = "";
// Array to hold the values for the SQL statement.
$values = array();
$elements = array("name", "age", "address", "standard");
// XML file to parse
$xmlFile = "student.xml";
// Database parameters
$hostname = "localhost";
$username = "root";
$password = "root123";
$database = "information";
$table = "student";
// Processes on encountering a opening tag in the XML. 
function startElementHandler($parser, $n1, $attributes)
{
   global $cTag;
   $cTag = $n1;
}
// Processes on encountering a closing tag in the XML.
function endElementHandler($parser, $n1)
{
   global $values, $cTag;
   // Import database link and table name.
   global $connection, $table;
   // if ending <item> tag
   // Implies end of record.
   if (strtolower($n1) == "item")
   {
      // Generating the query string.
      $query = "INSERT INTO student";
      $query .= "(name, age, address, standard) ";
      $query .= "VALUES(/"" . join("/", /"", $values) . "/");";
      // Processing the query.
      $result = mysql_query($query) or die ("Error in query: $query. " .
      mysql_error());
      // Reset all internal counters and arrays.
      $values = array();
      $cTag = "";
   }
}
// Processes on encountering a closing tag in the XML.
function characterDataHandler($parser, $data)
{
    global $cTag, $values, $elements;
   // lowercase tag name
   $cTag = strtolower($cTag);
   // Look for tag in $elements[] array.
   if (in_array($cTag, $elements) && trim($data) != "")
$values[$cTag] = mysql_escape_string($data); } // Initializing the SAX parser. $xml_parser = xml_parser_create(); // Set callback functions. xml_set_element_handler($xml_parser, "startElementHandler", "endElementHandler"); xml_set_character_data_handler($xml_parser, "characterDataHandler"); // Open connection to database. $connection = mysql_connect($hostname, $username, $password) or die ("Unable to connect!"); mysql_select_db($database) or die ("Unable to select database!"); // read XML file if (!($fp = fopen($xmlFile, "r"))) { die("File I/O error: $xmlFile"); } // parse XML while ($data = fread($fp, 4096)) { // error handler if (!xml_parse($xml_parser, $data, feof($fp))) { $error_code = xml_get_error_code($xml_parser); die("XML parser error (error code " . $error_code . "): " . xml_error_string($error_code) . "<br>Error occurred at line " . xml_get_current_line_number($xml_parser)); } } ?>

The above listing shows how to parse the student.xml file using the SAX approach. In the above listing:

  • The instance of the SAX parser is initialized using the xml_parser_create() function, and is configured to call various functions, such as startElemenHandler() and endElementHandler().

  • The startElemenHandler() function executes when a starting tag is processed in the XML document, and the endElementHandler() function executes when the closing tag in the XML document is processed.

You need to pass the tag name and the parser as arguments to the tag handler functions, such as startElemenHandler() and endElementHandler(). The characterDataHandler() function executes when the character data in the XML document is processed. You need to specify the Character Data (CDATA) text as arguments in the characterDataHandler() function.

The SAX parser retrieves and stores data from the student.xml file in the associative array, $values. The SAX parser retrieves data after finding character data having element name, such as name, age, address, and standard. The SAX parser creates a query string from the values obtained from the $values array.

You can also parse the XML document using the DOM approach.

Listing 8-11 shows how to parse the XML document using DOM:

Listing 8-11: Parsing XML Document using DOM
<?php
$xml_file = "student.xml";
$doc = xmldocfile($xml_file);
$name = array();
$age = array();
$address = array();
$standard = array();
$root = $doc->root();
$nodes = $root->children();
$a=0;
$b=0;
$c=0;
for($x=0;$x<sizeof($nodes);$x++)
{
   if ($nodes[$x]->type==XML_ELEMENT_NODE)
   {
      $text=$nodes[$x]->children();
      for($i=0;$i<sizeof($text);$i++)
      {
         $temp=$text[$i]->children();
         for($j=0;$j<sizeof($temp);$j++)
         {
            echo $temp[$j]->content;
         }
      }
   }
}
?>

The above listing shows how to parse the student.xml file using the DOM approach.

Closing the Connection to a Database After Importing Data

You need to close the database connection after importing the XML document in the database. The code to close the database connection is:

xml_parser_free($xml_parser);
mysql_close($connection);

The above code shows how to close the connection to the database using the mysql_close() function. The xml_parser_free() function frees the parser and returns true if $xml_parser is valid otherwise returns the value, False.

Alternative Method to Import XML Documents

Using PHP, you can import XML documents in a database without specifying the table name, field names, and values in the SQL statement. For example, you want to import the datastudent.xml file into a table, stud.

Listing 8-12 shows the contents of the datastudent.xml file:

Listing 8-12: Contents of datastudent.xml File
<?xml version="1.0" encoding="UTF-8"?>
<table name1="stud">
<record>
<age>12</age>
<address>New York</address>
<standard>12</standard>
<name>Mary</name>
</record>
<record>
<age>14</age>
<address>New York</address>
<standard>10</standard>
<name>John</name>
</record>
<record>
<age>15</age>
<address>New Jersey</address>
<standard>12</standard>
<name>Tom</name>
</record>
</table>

The above listing stores student information, such as name, record, address, age, and standard. The name of the table, in which the contents of the datastudent.xml file are imported, is specified as an attribute to the <table> element of the XML document. The student information, such as name, address, standard, and age, are specified with the <record> element.

Listing 8-13 shows how to import an XML document in a database without specifying the table and field names:

Listing 8-13: Importing the XML Document
<?php
// Initialize some variables
$cTag = "";
$fields = array();
$values = array();
// XML file to parse
$xml_file="datastudent.xml";
// Database parameters
// Get these via user input
$hostname = "localhost";
$username = "root";
$password = "";
$db = "information";
// Called when parser finds start tag.
function startElementHandler($parser, $name1, $attributes)
{
   global $cTag, $table;
   $cTag = $name1;
   // Get table name.
   if (strtolower($cTag) == "table")
   {
      foreach ($attributes as $v)
      {
         $table=$v;
      }
   }
}
// Called when parser finds end tag.
function endElementHandler($parser, $name1)
{
   global $fields, $values, $count, $cTag;
   // Import database link and table name.
   global $connection, $table;
    if (strtolower($name1) == "record")
    {
      // Generate the query string.
      $query = "INSERT INTO $table";
      $query .= "(" . join(", ", $fields) . ")";
      $query .= " VALUES(/"" . join("/", /"", $values) . "/");";
      // Execute query
      mysql_query($query) or die ("Error in query: $query. " .mysql_error());
      // Reset all internal counters and arrays.
      $fields = array();
      $values = array();
      $count = 0;
      $cTag = "";
    }
 }
// Called when parser finds cdata
function characterDataHandler($parser, $data)
{
   global $fields, $values, $cTag, $count;
   if (trim($data) != "")
   {
      // Add field-value pairs to $fields and $values array.
      // The index of each array is used to correlate the field-value pairs.
      $fields[$count] = $cTag;
      // Escape quotes with slashes
      $values[$count] = mysql_escape_string($data);
      $count++;
   }
}
// Initialize parser
$xml_parser = xml_parser_create();
// Set callback functions.
xml_set_element_handler($xml_parser, "startElementHandler", "endElementHandler");
xml_set_character_data_handler($xml_parser, "characterDataHandler");
// Open connection to database.
$connection = mysql_connect($hostname, $username, $password) or die ("Unable to connect!");
mysql_select_db($db) or die ("Unable to select database!");
// read XML file
if (!($fp = fopen($xml_file, "r")))
{
   die("File I/O error: $xml_file");
}
// Parse XML
while ($data = fread($fp, 4096))
{
   // Error handler
   if (!xml_parse($xml_parser, $data, feof($fp)))
   {
      $ec = xml_get_error_code($xml_parser);
      die("XML parser error (error code " . $ec . "): " . xml_error_string($ec) .
      "<br>Error occurred at line " . xml_get_current_line_number($xml_parser));
   }
}
// All done, clean up!
xml_parser_free($xml_parser);
mysql_close($connection);
?>

The above listing imports the datastudent.xml file without specifying the table name and field name values in the PHP script. You need to specify the hostname, username, and password of the MySQL database to connect to the database.

In the above listing, the xml_parser_create() function initializes the SAX parser, and the SAX parser processes various handler functions, such as xml_set_elemet_handler and xml_set_character_data_handler.

Figure 8-7 shows the output of Listing 8-13:

### 解决FBX库加载失败问题的方法 当遇到“FBX library failed to load”的错误时,通常是因为FBX SDK未正确安装、配置不匹配或者路径设置有问题。以下是可能的原因分析以及解决方案: #### 1. **确认FBX SDK版本** 确保使用的FBX SDK版本与开发环境兼容。如果使用的是较新的SDK版本,而目标平台(如Visual Studio 2015)可能存在支持不足的情况[^1]。建议下载并安装官方最新版的FBX SDK,并验证其是否适用于当前项目。 #### 2. **检查动态链接库文件** FBX SDK依赖于多个DLL文件来完成功能调用。如果这些DLL文件缺失或路径未正确指定,则可能导致“failed to load”。需将`fbxsdk.dll`及其关联文件复制到可执行程序的工作目录下,或者将其所在路径添加至系统的PATH变量中[^4]。 #### 3. **编译器和架构一致性** 确保所选编译器与FBX SDK的目标架构一致。例如,在Windows平台上运行64位应用程序时,应选用对应版本的64位FBX库而非32位版本。此外,还需注意C++标准库的选择(MD/MT),这会影响最终构建过程中的符号解析。 #### 4. **代码实现示例** 下面提供了一段简单的Python脚本用于测试基本的FBX导入操作: ```python from fbx import FbxManager, FbxIOSettings, FbxImporter def initialize_fbx_manager(): manager = FbxManager.Create() ios = FbxIOSettings.Create(manager, "IOSRoot") importer = FbxImporter.Create(manager, "") file_path = r"path_to_your_file.fbx" if not importer.Initialize(file_path.encode(), -1, manager.GetIOSettings()): raise Exception(f"Failed to load FBX at {file_path}") initialize_fbx_manager() print("FBX loaded successfully.") ``` 此代码片段展示了如何初始化FbxManager对象并通过它创建一个FbxImporter实例尝试打开特定路径下的FBX模型文件[^2]。 --- ### 总结 通过上述方法可以有效排查并修复“FBX library failed to load”这一常见问题。重点在于核实SDK版本适配情况、补充必要的外部资源文件以及保持工具链间的一致性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值