SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Aug 2007
    Posts
    79
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Add element to top in xml

    I use this code for adding elements to a xml file, but now the new element created is added last in the xml, is there a way to add it as the first element instead?

    PHP Code:
    $xdoc = new DomDocument;
    $xdoc->Load($xmlPath);
    $videos $xdoc->getElementsByTagName('videos')->item(0);
    $addItem $xdoc ->createElement('item');
    $addQuestion $xdoc ->createElement('question'$Question);
    $addAnswers $xdoc ->createElement('answers'$Answers);
    $addURL $xdoc ->createElement('url'$umaURL);
    $addPosition $xdoc ->createElement('position'$Position);
    $addCountry $xdoc ->createElement('country'$country);
    $addCategory $xdoc ->createElement('category'$category);
    $videos -> appendChild($addItem);
    $addItem -> appendChild($addQuestion);
    $addItem -> appendChild($addAnswers);
    $addItem -> appendChild($addURL);
    $addItem -> appendChild($addPosition);
    $addItem -> appendChild($addCountry);
    $addItem -> appendChild($addCategory);

    $saveXML $xdoc->save($xmlPath);
    return 
    "New question added!"

  2. #2
    SitePoint Wizard spence_noodle's Avatar
    Join Date
    Jan 2004
    Location
    uk, Leeds (area)
    Posts
    1,264
    Mentioned
    2 Post(s)
    Tagged
    1 Thread(s)
    Yes, you will need to read the file first then overwrite it with the new element first and adding the rest after.
    "Don't you just love it when you solve a programming bug only to create another."

  3. #3
    SitePoint Enthusiast
    Join Date
    Aug 2007
    Posts
    79
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok cool, how do I do that?


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
  •