SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Enthusiast rajasreekm's Avatar
    Join Date
    Sep 2009
    Posts
    32
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    xml database in php

    How to use xml as a database

    Creating users database using xml.while submitting Registration Form , create xml file...

    <users>
    <user id="1">
    <username>Raj</username>
    <password>test</password>
    </user>
    <user id="2">
    <username>deepa</username>
    <password>test2</password>
    </user>
    </users>



    and i used the code like


    // Creating xml file
    $doc=new DOMDocument();
    $root = $doc->createElement("users"); // Create Root Element
    $doc->appendChild($root);



    // Parent element
    $user = $doc->createElement("user");
    $root->appendChild($user);
    $user->setAttribute('id',$id); // set attribute i.e. id

    // username
    $username = $doc->createElement("username");
    $user->appendChild($username);
    $txtunames=$doc->createTextNode($txtuser);
    $username->appendChild($txtunames);

    // password
    $password = $doc->createElement("password");
    $user->appendChild($password);
    $txtpas=$doc->createTextNode($txtpass);
    $password->appendChild($txtpas);


    $m=$doc->save("users.xml");


    One user created..Second time it overwrites data

  2. #2
    Non-Member
    Join Date
    Oct 2009
    Posts
    1,852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, do not create new document second time. Read old one first.

  3. #3
    SitePoint Enthusiast rajasreekm's Avatar
    Join Date
    Sep 2009
    Posts
    32
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    XML as database in PHP

    How to use xml as database in php...Reading already existing xml file and insert new values to that....
    Pls Explain with code....

    Dont know how to read and write xml ...

  4. #4
    Non-Member
    Join Date
    Oct 2009
    Posts
    1,852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why don't you ask it in your previous thread?
    And why do you want to use XML database at all? It cannot be real database. Only a toy, for hundreds records or so.

  5. #5
    SitePoint Wizard lorenw's Avatar
    Join Date
    Feb 2005
    Location
    was rainy Oregon now sunny Florida
    Posts
    1,094
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    It's probably the attribute for fopen, you want to only open the file for writing and place the pointer at the end of the file.

    Cant see the code so only guessing.
    What I lack in acuracy I make up for in misteaks

  6. #6
    PHP Guru lampcms.com's Avatar
    Join Date
    Jan 2009
    Posts
    921
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There are 3 totally different ways to read/write xml file in php: SimpleXML, DOMDocument and XMLWriter/XMLReader

    You just have to pick the one that best fits your situation. If you expect to have very large xml files with thousands of records, then you should use XMLReader/XMLWriter because it handles XML document as a stream, so it does not have to load entire document into memory. That's why it can work with very large files.

    For very simple xml file where elements don't contain CData types SimpleXML is propably the best. DOMDocument is the appropriate choice when you expect to have CData elements and also when you need or want to use standard DOM API
    My project: Open source Q&A
    (similar to StackOverflow)
    powered by php+MongoDB
    Source on github, collaborators welcome!

  7. #7
    SitePoint Wizard PHPycho's Avatar
    Join Date
    Dec 2005
    Posts
    1,200
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think storing xml in db can be done in a similar way as storing images in db (using blob fields with some encoding)


Bookmarks

Posting Permissions

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