0

I have a php function inside one of my classes that simply creates an XML file of all of the objects. This is the code:

function writeXML(){
        //LOAD PROFILE
        //print "Profiles: <br>";
        //$pro = new Profile();

        $profileArray = $this->getAll();

        //var_dump($profileArray);
        //CHECK TO SEE IF THE XML FILE EXISTS IN THE LIBRARY/CONFIGURATION
        if(file_exists($_SESSION['ini'][rootPath] . "v/p.xml")){
            unlink($_SESSION['ini'][rootPath] . "v/p.xml");
        }

        //CREATE XML FILE
        //CREATE ROOT NODE
        $doc = new DOMDocument("1.0");
        $doc->formatOutput = true;

        //CREATE ROOT NODE
        print "Creating nodes";
        $root = $doc->createElement("profiles");
        $root = $doc->appendChild($root);

        //ADD NODES
        //print "<br>Creating children";
        foreach($profileArray as $member){
                    //AS WE CYCLE THROUGH THE ARRAY GET THE ARRAY OF OBJECT PARAMETERS
                    $memberArray = $member->toArray();

                    //var_dump($memberArray);

            //CREATE MEMBER NODE TO ROOT
            $person = $doc->createElement("profile");
            $person = $root->appendChild($person);

            //CREATE CHILD NODES FOR MEMBER
                        /*
                         * ID IS NOT NEEDED FOR IOS APP
            $id = $doc->createElement("ID");
            $id = $person->appendChild($id);

            $idValue = $doc->createTextNode($member->get_id());
            $idValue = $id->appendChild($idValue);
            */


            $lastName = $doc->createElement("lastName");
            $lastName = $person->appendChild($lastName);

            //$lastNameValue = $doc->createTextNode($member->get_lname());
                        $lastNameValue = $doc->createTextNode($memberArray['lastName']);
            $lastNameValue = $lastName->appendChild($lastNameValue);

            $firstName = $doc->createElement("firstName");
            $firstName = $person->appendChild($firstName);

            $firstNameValue = $doc->createTextNode($memberArray['firstName']);
                        //$firstNameValue = $doc->createTextNode($member->get_fname());
            $firstNameValue = $firstName->appendChild($firstNameValue);

                        $mobile = $doc->createElement("mobile");
            $mobile = $person->appendChild($mobile);

                        $mobileValue = $doc->createTextNode($memberArray['mobile']);
                        //$mobileValue = $doc->createTextNode($member->get_mobile());
            $mobileValue = $mobile->appendChild($mobileValue);

                        $phone = $doc->createElement("phone");
            $phone = $person->appendChild($phone);

            $phoneValue = $doc->createTextNode($memberArray['phone']);
                        //$phoneValue = $doc->createTextNode($member->get_phone());
            $phoneValue = $phone->appendChild($phoneValue);

            $email = $doc->createElement("email");
            $email = $person->appendChild($email);

            $emailValue = $doc->createTextNode($memberArray['email']);
                        //$emailValue = $doc->createTextNode($member->get_email());
            $emailValue = $email->appendChild($emailValue);

            $altEmail = $doc->createElement("altEmail");
            $altEmail = $person->appendChild($altEmail);

            $altEmailValue = $doc->createTextNode($memberArray['altEmail']);
                        //$altEmailValue = $doc->createTextNode($member->get_altemail());
            $altEmailValue = $altEmail->appendChild($altEmailValue);

            $street = $doc->createElement("address");
            $street = $person->appendChild($street);

            $streetValue = $doc->createTextNode($memberArray['street']);
                        //$streetValue = $doc->createTextNode($member->get_street());
            $streetValue = $street->appendChild($streetValue);

            $city = $doc->createElement("city");
            $city = $person->appendChild($city);

            $cityValue = $doc->createTextNode($memberArray['city']);
                        //$cityValue = $doc->createTextNode($member->get_city());
            $cityValue = $city->appendChild($cityValue);

            $state = $doc->createElement("state");
            $state = $person->appendChild($state);

            $stateValue = $doc->createTextNode($memberArray['state']);
                        //$stateValue = $doc->createTextNode($member->get_state());
            $stateValue = $state->appendChild($stateValue);

            $zip = $doc->createElement("zip");
            $zip = $person->appendChild($zip);

            $zipValue = $doc->createTextNode($memberArray['zip']);
                        //$zipValue = $doc->createTextNode($member->get_zip());
            $zipValue = $zip->appendChild($zipValue);

                        $image = $doc->createElement("image");
                        $image = $person->appendChild($image);

                        $imageValue = $doc->createTextNode($memberArray['image']);
                        $imageValue = $image->appendChild($imageValue);

            $titles = $doc->createElement("title");
            $titles = $person->appendChild($titles);

                        $titlesValue = $doc->createTextNode($memberArray['title']);
            //$titlesValue = $doc->createTextNode($member->get_field5());
            $titlesvalue = $titles->appendChild($titlesValue);

                        $group = $doc->createElement("group");
                        $group = $person->appendChild($group);

                        $groupValue = $doc->createTextNode($memberArray['group']);
                        $groupVAlue = $person->appendChild($groupValue);
        }
        print "<br>Saving XML<br><br>";
        $xml_string = $doc->saveXML();
        print "XML<br>";
        print $xml_string;
                print "<br>";
                print "Path: " . $_SESSION['ini']['rootPath'] . "public_html/v/p.xml";

        //if($doc->save($_SESSION['ini']['htmlPath'] . "v/p.xml")) return true;
                //else return false;
                print "Bytes: " . $doc->save($_SESSION['ini']['rootPath'] . "public_html/v/p.xml");

    }

I really need another set of eyes on this as the saveXML completes and even spits out the $xml_string to the screen. However, the $doc->save does not show anything at all.

Ideas?

1 Answer 1

1

If you want to save the xml to a file you need to use save, not saveXML.

Do you pass the filename to the save? the save will be like:

$filename=$_SESSION['ini'][rootPath] . "v/p.xml";
$doc->save($filename);

Other question: $_SESSION['ini'][rootPath] are you sure if it's setted?

Sign up to request clarification or add additional context in comments.

9 Comments

Yeah the $_SESSION is set. I actually print it out to the screen to confirm. I changed it up a bit and did the $fileName = session thing and then pass that do the save function - however still no XML file.
folder permissions maybe? you can try to do: $alldone=$doc->save($filename);and if $alldone is FALSE check the error log to see the error (or display error)
What log would I check? Or how would I get the error to display? I've set the directory permission: sudo chmod ugo+rwx <directory> so permissions shouldn't be an issue.
yes but apache user has to have access to this folder, f.e. /var/www/folder1/folder2/here/comes/the/xml/ if xml is 777 there's no efective if apache user can't move to "folder2" because folder2 is otheruser:otheruser and 770 (I don't find another way to explain it better ... sorry) Apache must have read permissions in all folders in path
I'm getting out of ideas :S if it prints good with the saveXML if there is no problem with permisions it have to save it to a file. you can also try to put the saveXML string to a file manually: $fp = fopen($filename, 'w');fwrite($fp, $doc->saveXML);fclose($fp);
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.