XML DOMDocument->load not working

Discussion in 'PHP' started by noahssite, Jul 11, 2010.

  1. #1
    I am getting an error message:
    Line 14 is this line:
    $doc->load($configFilePath);
    Code (markup):
    Full Code of file xmlDOMDocHandler.php:
    
    <?php
    $skip = true;
    $user="";
    if(isset($_SESSION['loggedOn']) && $_SESSION['loggedOn'] == "true"){
    $skip = false;
    $user=$_SESSION['username'];
    }
    $configFilePath = 'users/'.$user.'/config.xml' . rand(1,10000);
    $doc = new DOMDocument();
    $doc->preserveWhiteSpace = false;
    //Functions------------------------------------------
    function loadXML(){
    if($skip){return;}
    $doc->load($configFilePath);
    }
    function setSiteName($newName){
    if($skip){return;}
    loadXML();
    $doc->getElementsByTagName('websitename')->nodeValue = $newName;
    $doc->save($configFilePath);
    }
    function getSiteName(){
    if($skip){return;}
    loadXML();
    return $doc->getElementsByTagName('websitename')->nodeValue;
    }
    loadXML();
    ?>
    
    Code (markup):
    The above file is being included in another file called index.php within an If..Statement. I didn't understand the error message so I was playing around with the code trying to get it to work, that's why it may seem as if I just complicated it too much... (e.g. I don't really need the $skip variable and a few other things)..

    Why am I getting the error message? When I tried replacing this code:

    $doc = new DOMDocument();
    Code (markup):
    with

    $doc = DOMDocument::load($configFilePath);
    Code (markup):
    it worked... except the next function, the $doc->getElementsByTagName... returned the same error message.. It's like the method doesn't exist, except I know it exists, I've used it before.
     
    noahssite, Jul 11, 2010 IP
  2. Oxi

    Oxi Peon

    Messages:
    78
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Hey there,
    From what I see you are declaring the $doc variable as a new domdocument and then trying to call it from within a function. To do this you need to pass in the variable or move the declaration. In your case i would pass the variable in as a parameter. Example:

    
    <?php
    $skip = true;
    $user="";
    if(isset($_SESSION['loggedOn']) && $_SESSION['loggedOn'] == "true"){
    $skip = false;
    $user=$_SESSION['username'];
    }
    $configFilePath = 'users/'.$user.'/config.xml' . rand(1,10000);
    $doc = new DOMDocument();
    $doc->preserveWhiteSpace = false;
    //Functions------------------------------------------
    function loadXML($skip, $doc){
    if($skip){return;}
    $doc->load($configFilePath);
    }
    function setSiteName($newName, $skip, $doc){
    if($skip){return;}
    loadXML($skip, $doc);
    $doc->getElementsByTagName('websitename')->nodeValue = $newName;
    $doc->save($configFilePath);
    }
    function getSiteName($skip, $doc){
    if($skip){return;}
    loadXML($skip, $doc);
    return $doc->getElementsByTagName('websitename')->nodeValue;
    }
    loadXML($skip, $doc);
    ?>
    
    PHP:
     
    Oxi, Jul 11, 2010 IP
  3. noahssite

    noahssite Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks, I didn't realize that variables declared in the main scope couldn't be accessed from the scope of a function.
     
    noahssite, Jul 11, 2010 IP