6 Replies - 682 Views - Last Post: 26 September 2012 - 10:03 PM Rate Topic: -----

#1 cam0988  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 13-September 12

Write form to xml

Posted 26 September 2012 - 03:08 AM

Hi
Just wondering if anyone has any advice, ways, links on writing a form to an XML file which is amended each time.
Unfortunately unable to use a database.

Cheers
Is This A Good Question/Topic? 0
  • +

Replies To: Write form to xml

#2 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1002
  • View blog
  • Posts: 3,562
  • Joined: 05-June 09

Re: Write form to xml

Posted 26 September 2012 - 03:12 AM

You can use PHP's built in SimpleXml classes for (very) easy XML writing/manipulation.
Was This Post Helpful? 3
  • +
  • -

#3 nightscream  Icon User is offline

  • D.I.C Head

Reputation: 19
  • View blog
  • Posts: 237
  • Joined: 04-December 08

Re: Write form to xml

Posted 26 September 2012 - 01:55 PM

I think the DomDocument is a bit easier to create xml.
Was This Post Helpful? 1
  • +
  • -

#4 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3712
  • View blog
  • Posts: 5,963
  • Joined: 08-June 10

Re: Write form to xml

Posted 26 September 2012 - 06:49 PM

Depending on the complexity of the XML to be generated, for simple XML files it's often just easier to use a string template and inject values into it using simple string replacement functions.

KISS, right? :)

Of course, if the XML is in any way dynamic, then you're most likely better of using SimpleXML or Domdocument. I'd probably vote for SimpleXML myself, although I avoid XML wherever possible.
Was This Post Helpful? 1
  • +
  • -

#5 cam0988  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 13-September 12

Re: Write form to xml

Posted 26 September 2012 - 09:04 PM

Thanks for all your help, I'll give it a shot. Its for a university project and unfortunately they are prohibiting the use of database. We just need to be able to record the contents of a form which includes a password and email, write it to a file and then call back the information when needed.
Thanks again
Was This Post Helpful? 0
  • +
  • -

#6 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3712
  • View blog
  • Posts: 5,963
  • Joined: 08-June 10

Re: Write form to xml

Posted 26 September 2012 - 09:36 PM

You know, technically a XML file saved on the server hard-drive does qualify as a database. That word doesn't just cover large SQL type RDBMS servers, like MySQL or Microsoft SQL Server. :)

I hope the project description was more specific about that. Otherwise you may want to ask for clarification before you hand that in.

Quote

We just need to be able to record the contents of a form which includes a password and email, write it to a file and then call back the information when needed.

I'd say XML is overkill for that. A very simple comma separated list of values would be more than sufficient for that type of deal.
// Lets say your data starts like this
$email = "example@example.com";
$password = "youwish";

// You can "serialize" it by doing this:
$str = implode(",", array($email, $password));

// $str is now:
// "example@example.com,youwish"

// Save that to a file
if (file_put_contents("thefile.csv", $str)) {
    echo "Data saved!";
}

// And than you can recall that later by doing:
$rawData = file_get_contents("thefile.csv");
if ($rawData) {
    list($email, $password) = explode(",", $rawData);

    // And now your data is back!
    echo $email; // example@example.com
    echo $password; // youwish
}


Of course, you'd have to make sure neither the email nor password contained a comma, otherwise the whole thing falls apart. There are functions like serialize and json_encode you can use to do similar things, if the data is more complex than what I demonstrated.
Was This Post Helpful? 3
  • +
  • -

#7 cam0988  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 13-September 12

Re: Write form to xml

Posted 26 September 2012 - 10:03 PM

Brilliant,
thanks Atli. Thats a big help, I will give it a go.
Wasn't aware that xml was considered a database, looked through the specs though and it says txt or xml is allowed so assuming they want to prevent the use of sql.
Thanks again
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1