Page 2 of 2 FirstFirst 12
Results 11 to 17 of 17

Thread: Reading, adding, writing xml...unexpected result

  1. #11
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    something like this should work:
    PHP Code:
    <?php

    /**** NOT TESTED  ****/

    $insertcount $_POST["newcount"];
    $insertquote $_POST["iquote"];
    $insertref $_POST["iref"];

    $file '../sidebar.xml';
    $xml simplexml_load_file($file);

    /************************************************
    I assume "newcount" is the id for the new entry?
    is this submitted by the user?
    if so, it could be better handled by PHP to avoid
    duplicate ID's:

    // find the last entry 
    // (assuming entries are all in numerical order,
    //  starting with # 1)
    $last = count(xml->myquote);
    // assign next entry number
    $insertcount = $last + 1;
    ************************************************/ 

    // this line assumes <sidebar> is your xml root
    // add a new <myquote> entry
    $newItem $xml->addChild('myquote');

    // add a new <id> to the new <myquote>
    $newItem->addChild('id'$insertcount);
    // add a new <thequote>
    $newItem->addChild('thequote'$insertquote);
    // add a new <reference>
    $newItem->addChild('reference'$insertref);

    // convert the $xml object to an xml string
    // and save it to the sidebar.xml file
    $xml $xml->asXML($file);

    ?>
    Edit:

    Basic reading of your xml file (also not tested, but I use this method often):


    PHP Code:
    <?php

    $file 
    '../sidebar.xml';
    $xml simplexml_load_file($file);

    foreach(
    $xml->children() as $myquote){
        echo 
    '
            <hr>
            id: '
    .$myquote->id.'<br>
            the quote: '
    .$myquote->thequote.'<br>
            reference: '
    .$myquote->reference;
    }

    /****************************************
    if you wanted to list the entries in reverse order (e.g.,
    most recent first), you could use this instead:

    $i = count($xml->myquote)-1;
    for($x=$i;$x>=0;$x--){
        echo '
            <hr>
            id: '.$myquote[$x]->id.'<br>
            the quote: '.$myquote[$x]->thequote.'<br>
            reference: '.$myquote[$x]->reference;
    }

    //  ! EDIT: fixed index references !

    ****************************************/

    ?>
    Edit:

    Quote Originally Posted by I am Abby
    //ok this is where I get lost...I need to put each of the elements of the array
    No, you don't - I think that's where your confusion is. simplexml_load_file() already put everything into a simplexml object (not an array, but imagine it like an array). Nothing more to set up. You can just start using it, as in my example above.

    Last edited by traq; 04-20-2010 at 07:51 PM.

  2. #12
    Join Date
    Apr 2010
    Location
    University of Illinois
    Posts
    86
    Thanks
    13
    Thanked 2 Times in 2 Posts

    Default

    So your top bit of code there gets the form elements and adds it to the existing xml? If so, I didn't think you could do that...I thought you had to read, add to, write?

  3. #13
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    simplexml_load_file() reads the existing xml file and turns it into a simplexml object (which is ready for us to manipulate).

    the ->addChild() statements add the new info (in this case, your $_POST info) to the simplexml object.

    ->asXML() converts the simplexml object into an xml string (and writes it to file, too, if a file path is included).

  4. #14
    Join Date
    Apr 2010
    Location
    University of Illinois
    Posts
    86
    Thanks
    13
    Thanked 2 Times in 2 Posts

    Default

    Wow!

    You guys were so right...this is so much easier than creating a DOMDocument.

  5. #15
    Join Date
    Apr 2010
    Location
    University of Illinois
    Posts
    86
    Thanks
    13
    Thanked 2 Times in 2 Posts

    Default

    Is there an easy way to delete an entry?

  6. #16
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    there's no simplexml function, but you can use unset():
    PHP Code:
    <?php

    $xml 
    simplexml_load_file('file.xml');
    unset(
    $xml->node->to_delete);
    $xml $xml->asXML;

    ?>
    Include any applicable indices, or you may end up losing ALL nodes that match the path you enter.

  7. #17
    Join Date
    Apr 2010
    Location
    University of Illinois
    Posts
    86
    Thanks
    13
    Thanked 2 Times in 2 Posts

    Default

    So, all I would have to do is run everything through a foreach...find the one I don't like and unset() it.

    Thanks.

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
  •