无法找到正确的想法

时间:2011-05-10 01:28:58

标签: php xml dom

好吧,我正在编写一个PHP代码来编辑这些标签内的标签和数据,但是我很难解决这个问题。

基本上我有一个类似于此但更大的xml文件

<users>
 <user1>
  <password></password>
  </user1>
</users>

和我用来尝试更改user1标签的php代码就是这个

function mod_user() {
    // Get global Variables
    global $access_level;

    // Pull the data from the form
    $entered_new_username = $_POST['mod_user_new_username'];
    $entered_pass = $_POST['mod_user_new_password'];
    $entered_confirm_pass = $_POST['mod_user_confirm_new_password'];
    $entered_new_roll = $_POST['mod_user_new_roll'];
    $entered_new_access_level = $_POST['mod_user_new_access_level'];

    // Grab the old username from the last page as well so we know who we are looking for
    $current_username = $_POST['mod_user_old_username'];

    // !!-------- First thing is first. we need to run checks to make sure that this operation can be completed ----------------!!

    //  Check to see if the user exist. we just use the normal phaser since we are only reading and it's much easier to make loop through
    $xml = simplexml_load_file('../users/users.xml');

    // read the xml file find the user to be modified
    foreach ($xml->children() as $xml_user_get)
    {
        $xml_user = ($xml_user_get->getName());

        if ($xml_user == $entered_new_username){
            // Set array to send data back
            //$a = array ("error"=>103, "entered_user"=>$new_user, "entered_roll"=>$new_roll, "entered_access"=>$new_access_level);

            // Add to session to be sent back to other page
            // $_SESSION['add_error'] = $a;
            die("Username Already exist - Pass");
            // header('location: ../admin.php?page=usermanage&task=adduser');
        }
    }

    // Check the passwords and make sure they match
    if ($entered_pass == $entered_confirm_pass) {
        // Encrypt the new password and unset the old password variables so they don't stay in memory un-encrytped
        $new_password = hash('sha512', $entered_pass);

        unset ($entered_pass, $entered_confirm_pass, $_POST['mod_user_new_password'], $_POST['mod_user_confirm_pass']);
    }

    else {

        die("passwords did not match - Pass");
    }

    if ($entered_new_access_level != "") {
        if ($entered_new_access_level < $access_level){
            die("Access level is not sufficiant to grant access - Pass");
        }
    }

    // Now to load up the xml file and commit changes.
    $doc = new DOMDocument;
    $doc->formatOutput = true;
    $doc->perserveWhiteSpace = false;
    $doc->load('../users/users.xml');

    $old_user = $doc->getElementsByTagName('users')->item(0)->getElementsByTagName($current_username)->item(0);

    // For initial debugging - to be deleted
    if ($old_user == $current_username)
        echo    "old username found and matches";

    // Check the variables to see if there is something to change in the data.
    if ($entered_new_username != "") {
        $xml_old_user = $doc->getElementsByTagName('users')->item(0)->getElementsByTagName($current_username)->item(0)->replaceChild($entered_new_username, $old_user);

        echo "Username is now: " . $current_username;
    }

    if ($new_pass != "") {
        $current_password = $doc->getElementsByTagName($current_user)->item(0)->getElementsByTagName('password')->item(0)->nodeValue;

        //$replace_password = $doc
    }
}

当只使用输入的用户名进行更改时,我会收到此错误

Catchable fatal error: Argument 1 passed to DOMNode::replaceChild() must be an instance of DOMNode, string given, called in E:\xampp\htdocs\CGS-Intranet\admin\html\useraction.php on line 252 and defined in E:\xampp\htdocs\CGS-Intranet\admin\html\useraction.php on line 201

有人可以向我解释如何做到这一点或向我展示他们是如何做到的......对我来说,看看它是如何做的可能会有点意义:s

感谢

2 个答案:

答案 0 :(得分:1)

$entered_new_username是一个字符串,所以你需要通过类似$doc->createElement()

之类的东西用DOM对象包装它
    $xml_old_user = $doc->getElementsByTagName('users')->item(0)->getElementsByTagName($current_username)->item(0)->replaceChild($doc->createElement($entered_new_username), $old_user);

这可能不太对,但希望它指出你正确的方向。

答案 1 :(得分:0)

好吧,它写了并替换了我想要的节点,但我遇到了其他问题,我必须解决(IE:它取代了整个树,而不仅仅是更改节点名称)

无论如何我使用的代码是

    // For initial debugging - to be deleted
if ($old_user == $current_username)
    echo    "old username found and matches";

// Check the variables to see if there is something to change in the data.
if ($entered_new_username != "") {
    try {
    $new_node_name = $doc->createElement($entered_new_username);

    $old_user->parentNode->replaceChild($new_node_name, $old_user);
    }
    catch (DOMException $e) {
        echo $e;
    }
    echo "Username is now: " . $current_username;
}

if ($new_pass != "") {
    $current_password = $doc->getElementsByTagName($current_user)->item(0)->getElementsByTagName('password')->item(0)->nodeValue;

    //$replace_password = $doc
}

$doc->save('../users/users.xml');