www.webdeveloper.com
Results 1 to 2 of 2

Thread: [RESOLVED] Strange SimpleXMLElement behaviour

  1. #1
    Join Date
    Aug 2007
    Location
    London
    Posts
    410

    resolved [RESOLVED] Strange SimpleXMLElement behaviour

    Hi all,

    I'm trying to implement custom session handling and have decided to use files and xml. When setting variables I get a strange behaviour from SimpleXMLElement, however: it seems to duplicate the root node and place it within the original root node.

    Here's the code which sets the variables:

    PHP Code:
    private function _setFile($name$value) {
        
    $_this =& Session::getInstance();        
        
    Cleaner::html($value);
        
    Cleaner::html($name);
        
    $xml = new SimpleXMLElement(Filesystem::readFile($_this->file));
        if(
    file_exists(INCL_PATH DS $_this->file)) {
            if(
    count($xml->xpath("/session/$name")) > 0) {
                
    $xml->session->$name $value;
            } else {
                
    $sesh $xml->xpath("/session");
                
    $sesh[0]->addChild($name$value);
            }
            
    Filesystem::writeFile($_this->file$xml->asXML(), true);
            
    $_this->_addData($name$value);
            return 
    true;
        } else {
            return 
    false;
        }

    The XML I get written to the session file is as follows:

    Code:
    <?xml version="1.0"?>
    <session expires="1271013868" path="/">
    <hello>moto</hello>
    <session><hello>moto</hello></session>
    </session>
    (I added the line separations for readability)

    I can't work out why this is happening but I'm fairly sure the extra <session> node is created here:

    PHP Code:
    } else {
        
    $sesh $xml->xpath("/session");
        
    $sesh[0]->addChild($name$value);

    I don't really know why it's being populated though. Before any variables are set the xml document just contains the xml header and the root session node.

    Any help would be much appreciated.

  2. #2
    Join Date
    Aug 2007
    Location
    London
    Posts
    410
    Okay, I fixed it. It seems I misunderstood the SimpleXMLElement thinking that $xml->rootNode gives the <rootNode> element whereas actually $xml is the root element. Silly me.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles