PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
lammspillning
Forum Newbie
Posts: 14 Joined: Fri Aug 17, 2007 10:50 am
Post
by lammspillning » Wed Apr 15, 2009 3:20 am
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, and how would I do it?
Code: Select all
$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!";
requinix
Spammer :|
Posts: 6617 Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA
Post
by requinix » Wed Apr 15, 2009 3:46 am
It's XML. It doesn't matter.
If you really care then just rearrange the code.
requinix
Spammer :|
Posts: 6617 Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA
Post
by requinix » Wed Apr 15, 2009 4:10 am
If you don't like the order then shuffle the lines of code around until you do.
susrisha
Forum Contributor
Posts: 439 Joined: Thu Aug 07, 2008 11:43 pm
Location: Hyderabad India
Post
by susrisha » Wed Apr 15, 2009 5:11 am
This might be what you are looking for
lammspillning
Forum Newbie
Posts: 14 Joined: Fri Aug 17, 2007 10:50 am
Post
by lammspillning » Wed Apr 15, 2009 7:23 am
I guess that's what I'm looking for, but can't get it to work.. Is this how I should use it?
on line 11 in my previous code:
$videos -> appendChild($addItem->insertBefore(item[0]));
php_east
Forum Contributor
Posts: 453 Joined: Sun Feb 22, 2009 1:31 pm
Location: Far Far East.
Post
by php_east » Wed Apr 15, 2009 11:43 am
maybe easier if
Code: Select all
$xdoc2 = new DomDocument;
$xdoc2 = appendChild(new xml tag)
$xdoc2 = appendChild($xdoc1, deep)
something like that.