Jump to content

Formatting XML file using php


davemoody

Recommended Posts

Hello,

 

I've just created code in my shopping cart to save an order to an xml file. Now the file has all the elements and 'children' all in one long line, which gets longer with every new order.

 

Is it possible to control how the elements etc are saved to the xml file so that they are nested?

 

Here's my php code for adding elements to the file:

 

$xdoc = new DomDocument;

$xdoc->Load('orders/orders.xml');

 

        $orders = $xdoc->getElementsByTagName('Orders')->item(0);

        $newOrderElement = $xdoc ->createElement('Order');

 

        $newUsernameElement = $xdoc ->createElement('username');

        $newEmailElement = $xdoc ->createElement('email');

        $newFirstNameElement = $xdoc ->createElement('firstname');

        $newLastNameElement = $xdoc ->createElement('lastname');

        $newStreetElement = $xdoc ->createElement('street');

        $newCityElement = $xdoc ->createElement('city');

        $newStateElement = $xdoc ->createElement('state');

        $newPostcodeElement = $xdoc ->createElement('postcode');

        $newCountryElement = $xdoc ->createElement('country');

        $newCCnumberElement = $xdoc ->createElement('ccnumber');

        $newCCexpirymonthElement = $xdoc ->createElement('ccexpirymonth');

        $newCCexpiryyearElement = $xdoc ->createElement('ccexpiryyear');

        $newOrdertotalElement = $xdoc ->createElement('ordertotal');

        $newProductorderElement = $xdoc ->createElement('productorder');

 

        $usernameNode = $xdoc ->createTextNode ($username);

$emailNode = $xdoc ->createTextNode ($email);

$firstnameNode = $xdoc ->createTextNode ($firstname);

$lastnameNode = $xdoc ->createTextNode ($lastname);

$streetNode = $xdoc ->createTextNode ($street);

$cityNode = $xdoc ->createTextNode ($city);

$stateNode = $xdoc ->createTextNode ($state);

$postcodeNode = $xdoc ->createTextNode ($postcode);

$countryNode = $xdoc ->createTextNode ($country);

$ccnumberNode = $xdoc ->createTextNode ($ccnumber);

$ccexpirymonthNode = $xdoc ->createTextNode ($ccexpirymonth);

$ccexpiryyearNode = $xdoc ->createTextNode ($ccexpiryyear);

$ordertotalNode = $xdoc ->createTextNode ($ordertotal);

$productorderNode = $xdoc ->createTextNode ($productorder);

 

        $newUsernameElement -> appendChild($usernameNode);

        $newEmailElement -> appendChild($emailNode);

        $newFirstNameElement -> appendChild($firstnameNode);

        $newLastNameElement -> appendChild($lastnameNode);

        $newStreetElement -> appendChild($streetNode);

        $newCityElement -> appendChild($cityNode);

        $newCountryElement -> appendChild($countryNode);

        $newCCnumberElement -> appendChild($ccnumberNode);

        $newCCexpirymonthElement -> appendChild($ccexpirymonthNode);

        $newCCexpiryyearElement -> appendChild($ccexpiryyearNode);

        $newOrdertotalElement -> appendChild($ordertotalNode);

        $newProductorderElement -> appendChild($productorderNode);

 

        $newOrderElement -> appendChild($newUsernameElement);

        $newOrderElement -> appendChild($newEmailElement);

        $newOrderElement -> appendChild($newFirstNameElement);

        $newOrderElement -> appendChild($newLastNameElement);

        $newOrderElement -> appendChild($newStreetElement);

        $newOrderElement -> appendChild($newCityElement);

        $newOrderElement -> appendChild($newStateElement);

        $newOrderElement -> appendChild($newCountryElement);

        $newOrderElement -> appendChild($newCCnumberElement);

        $newOrderElement -> appendChild($newCCexpirymonthElement);

        $newOrderElement -> appendChild($newCCexpiryyearElement);

        $newOrderElement -> appendChild($newOrdertotalElement);

        $newOrderElement -> appendChild($newProductorderElement);

 

        $orders -> appendChild($newOrderElement);

 

$xdoc->save('orders/orders.xml');

 

Any advice will be greatly appreciated.

 

Dave.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.