📜  如何使用PHP将 XML 数据加载到 MySQL 中?

📅  最后修改于: 2022-05-13 01:54:11.702000             🧑  作者: Mango

如何使用PHP将 XML 数据加载到 MySQL 中?

在本文中,我们将在 XAMPP 服务器中使用PHPXML 文件中的数据存储到 MySQL 数据库中。

XML: 可扩展标记语言 (XML) 是一种标记语言,它定义了一组规则,用于以人类可读和机器可读的格式对文档进行编码。 XML 的设计目标侧重于 Internet 上的简单性、通用性和可用性。

例子:

HTML


    
        1
        sravan
    
    
        2
        Ojaswi
    
    
        3
        Rohith
    


XML


    
        PHP DATABASE CONNECTION
        
https://www.geeksforgeeks.org/php-database-connection/
        
        
            The collection of related data is called a
            database. XAMPP stands for cross-platform,
            Apache, MySQL, PHP, and Perl. It is among
            the simple light-weight local servers for
            website development.
        
        PHP, XAMPP
    
 
    
 
        Screen density and Terminologies
        
https://www.geeksforgeeks.org/screen-density-and-its-terminologies/
        
        
            Screen Density is a calculation of the
            proportion of display character positions
            on the screen or an area of the screen
            containing something.
        
        software engineering
    
 
    
        DataProcessing vs DataCleaning
        
https://www.geeksforgeeks.org/difference-between-data-cleaning-and-data-processing/
        
        
            Data Processing: It is defined as Collection,
            manipulation, and processing of collected data
            for the required use. It is a task of converting
            data from a given form to a much more usable and
            desired form i.e. making it more meaningful and
            informative. Using Machine Learning algorithms,
            mathematical modelling and statistical knowledge,
            this entire process can be automated. This might
            seem to be simple but when it comes to really big
            organizations like Twitter, Facebook,
            Administrative bodies like Parliament, UNESCO and
            health sector
        
        Data Mining
    
 
    
        Predicting Air Quality Index
        
https://www.geeksforgeeks.org/predicting-air-quality-index-using-python/
        
        
            AQI: The air quality index is an index for
            reporting air quality on a daily basis. In
            other words, it is a measure of how air
            pollution affects one’s health within a
            short time period. The AQI is calculated
            based on the average concentration of a
            particular pollutant measured over a
            standard time interval. Generally, the
            time interval is 24 hours for most pollutants,
            8 hours for carbon monoxide and ozone.
        
        Machine Learning, Python
    


PHP
children() as $row) {
    $title = $row->title;
    $link = $row->link;
    $description = $row->description;
    $keywords = $row->keywords;
     
    // SQL query to insert data into xml table
    $sql = "INSERT INTO xml(title, link,
        description, keywords) VALUES ('"
        . $title . "','" . $link . "','"
        . $description . "','" . $keywords . "')";
     
    $result = mysqli_query($conn, $sql);
     
    if (! empty($result)) {
        $affectedRow ++;
    } else {
        $error_message = mysqli_error($conn) . "\n";
    }
}
?>
 

GEEKS GOR GEEKS

XML Data storing in Database

0) {     $message = $affectedRow . " records inserted"; } else {     $message = "No records inserted"; }   ?>  
    
   
    




加载 XML 文件:我们将使用 simplexml_load_file()函数将格式良好的 XML 文档转换为给定的文件到一个对象。

句法:

SimpleXMLElement simplexml_load_file( string $filename, 
        string $class_name = "SimpleXMLElement",
        int $options = 0, string $ns = "", 
        bool $is_prefix = FALSE )

编写和执行代码的步骤:



  • 启动 XAMPP

  • 确定 xml 文件中的属性数量并在 XAMPP 中创建表。 XML 文件中有 4 个属性(input.xml 是文件名)。这些是标题,链接。描述关键字。数据库名是xmldata ,表名是xml

文件名:input.xml

XML



    
        PHP DATABASE CONNECTION
        
https://www.geeksforgeeks.org/php-database-connection/
        
        
            The collection of related data is called a
            database. XAMPP stands for cross-platform,
            Apache, MySQL, PHP, and Perl. It is among
            the simple light-weight local servers for
            website development.
        
        PHP, XAMPP
    
 
    
 
        Screen density and Terminologies
        
https://www.geeksforgeeks.org/screen-density-and-its-terminologies/
        
        
            Screen Density is a calculation of the
            proportion of display character positions
            on the screen or an area of the screen
            containing something.
        
        software engineering
    
 
    
        DataProcessing vs DataCleaning
        
https://www.geeksforgeeks.org/difference-between-data-cleaning-and-data-processing/
        
        
            Data Processing: It is defined as Collection,
            manipulation, and processing of collected data
            for the required use. It is a task of converting
            data from a given form to a much more usable and
            desired form i.e. making it more meaningful and
            informative. Using Machine Learning algorithms,
            mathematical modelling and statistical knowledge,
            this entire process can be automated. This might
            seem to be simple but when it comes to really big
            organizations like Twitter, Facebook,
            Administrative bodies like Parliament, UNESCO and
            health sector
        
        Data Mining
    
 
    
        Predicting Air Quality Index
        
https://www.geeksforgeeks.org/predicting-air-quality-index-using-python/
        
        
            AQI: The air quality index is an index for
            reporting air quality on a daily basis. In
            other words, it is a measure of how air
            pollution affects one’s health within a
            short time period. The AQI is calculated
            based on the average concentration of a
            particular pollutant measured over a
            standard time interval. Generally, the
            time interval is 24 hours for most pollutants,
            8 hours for carbon monoxide and ozone.
        
        Machine Learning, Python
    

文件名:索引PHP



PHP

children() as $row) {
    $title = $row->title;
    $link = $row->link;
    $description = $row->description;
    $keywords = $row->keywords;
     
    // SQL query to insert data into xml table
    $sql = "INSERT INTO xml(title, link,
        description, keywords) VALUES ('"
        . $title . "','" . $link . "','"
        . $description . "','" . $keywords . "')";
     
    $result = mysqli_query($conn, $sql);
     
    if (! empty($result)) {
        $affectedRow ++;
    } else {
        $error_message = mysqli_error($conn) . "\n";
    }
}
?>
 

GEEKS GOR GEEKS

XML Data storing in Database

0) {     $message = $affectedRow . " records inserted"; } else {     $message = "No records inserted"; }   ?>  
    
   
    

执行步骤:

1. 将 2 个文件保存在一个文件夹中的路径: xampp/htdocs/gfg

2. 输入 localhost/gfg/index。 PHP并查看输出

输出

现在检查存储在我们数据库中的xml中的数据