简体   繁体   中英

DOMElement::setAttribute not working php

I've an xml file containing tag like this.

<server>
  <conversation ip="12.0.0.1" email="none">
     <chat userstatus="1" adminstatus="2" username="admin">muja</chat>
  </conversation>
</server>

Now I want to update the email attribute of the conversation tag. When I use $conv->getAttribute("email") it echo's me the correct result ie none.But if I try to set it using $conv->setAttribute("email","abc") it does not update the value. Here's what I am doing.

This is the GetClientConversation():

private function GetClientConversation()
{
    foreach($this->conversation as $convTag)
    {
        if($convTag->getAttribute("ip") == $this->clientip)
        {
            return $convTag;
        }
    }

    return "noConversation";
}

This function returns me the correct conversationTag that I needed.

And i get these conversationsTags array using

  $this->conversation=$this->xmlDom->getElementsByTagName("conversation");

Edit:

public function GetConversation()
{
        $conv=$this->GetClientConversation();
        if($conv!="noConversation")
        {
            if($conv->getAttribute("email")=="none")   
            {
                $conv->setAttribute("email","abc");    // -- Here
                return json_encode($this->RetrieveConversation($conv));
            }
            else if($conv->getAttribute("email")==$this->adminEmail)
            {
                return json_encode($this->RetrieveConversation($conv));
            }
            else
            {
                return "Admin Already Chatting";
            }
        }
        else
        {
            $this->CreateNewConversation();
            return "no";
        }
}

This is the code from where I am trying to set the attribute.

You have correctly used setAttribute() .

You are retrieving your XML and passing the string back to json_encode() . However, if the RetrieveConversation() method has not correctly called saveXML() prior to returning the string, your modifications will not be available in the output XML string. Be sure you have called saveXML() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM