【问题标题】:Export specific strings from xml to mysql table将特定字符串从 xml 导出到 mysql 表
【发布时间】:2016-01-11 02:09:15
【问题描述】:

XML Codes

我有一个游戏服务器的项目列表 xml 文件,我需要将项目名称、ID 和项目类型插入 mysql 服务器。我提供了 4 个情况示例,第一个是 isstaffitem="true",第二个是 iscashitem="true",第三个是 iscashitem="false",第四个没有关于项目类型的内容。现在我需要读取项目的 id="string"、name="string" 和类型并插入数据库。如果是staff item,设置type为staff,如果cash item设置type为cash,其他设置为regular。

我该怎么做 php ?我正在尝试使用$xml = simplexml_load_string($_POST['zitem']);,但无法正常工作..

【问题讨论】:

    标签: php mysql xml


    【解决方案1】:

    这是一些示例代码。我创建了一个简单的 HTML 表单,带有一个文本区域来输入 XML 代码。点击“提交”后,触发 PHP 脚本(服务器方法 = 发布)。连接到您的数据库(来自w3schools 的示例)。将 XML 字符串解析为 DomDocument(比 simple_xml 提供更多选项)。将 xml 中的每个项目插入 MySQL。

    <?php
    if($_SERVER['REQUEST_METHOD'] == 'POST') {
        // Make connection to database first
        // example from http://www.w3schools.com/php/php_mysql_insert.asp
        $servername = "localhost";
        $username = "username";
        $password = "password";
        $dbname = "myDB";
    
        // Create connection
        $conn = new mysqli($servername, $username, $password, $dbname);
        // Check connection
        if ($conn->connect_error) {
            die("Connection failed: " . $conn->connect_error);
        }
    
        // Use DomDocument (more options / flexibility)
        $doc = new DOMDocument();
        $doc->loadXML($_POST['zitem']);
        $items = $dom->getElementsByTagName('ITEM');
        foreach ($items as $item) {
            // The attributed in the XML item "ITEM", can be retrieved by using $item->getAttribute('nameofattribute')
            $id = $item->getAttribute('id');
            $name = $item->getAttribute('name');
            $mesh_name = $item->getAttribute('mesh_name');
    
            // Some logic, see for yourself.
            $cashitem = false;
            if ($item->getAttribute('iscashitem') == true) {
                $cashitem = true;
            }
    
            $staffitem = false;
            if ($item->getAttribute('isstaffitem') == true) {
                $staffitem = true;
            }
    
            // Insert the item from XML into MySQL-table
            $sql = "
                  INSERT INTO Items
                    (id, name, mesh_name, cashitem, staffitem)
                  VALUES (
                    '" . $mysqli->real_escape_string($id) . "',
                    '" . $mysqli->real_escape_string($name) . "',
                    '" . $mysqli->real_escape_string($mesh_name) . "',
                    'somevaluehere',
                    'somevaluehere'
                  )";
    
            if ($conn->query($sql) === TRUE) {
                echo "New record created successfully";
            } else {
                echo "Error: " . $sql . "<br>" . $conn->error;
            }    
        }
        $conn->close();
    }
    ?>
    
    <form method="post">
        <textarea name="zitem"></textarea>
        <input type="submit" value="Submit your XML file" />
    </form>
    

    祝你好运!

    【讨论】:

    • somevaluehere 不是$cashitem$staffitem 变量吗?
    • 取决于他想要的数据库中的内容。但是,是的。只是不像 php 布尔值,我假设。
    猜你喜欢
    • 2011-08-16
    • 1970-01-01
    • 2015-06-28
    • 1970-01-01
    • 2016-03-17
    • 1970-01-01
    • 2021-08-03
    • 2016-11-07
    • 2013-04-07
    相关资源
    最近更新 更多