Adding elements on top of xml

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

Post Reply
lammspillning
Forum Newbie
Posts: 14
Joined: Fri Aug 17, 2007 10:50 am

Adding elements on top of xml

Post by lammspillning »

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!"; 
 
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Adding elements on top of xml

Post by requinix »

It's XML. It doesn't matter.

If you really care then just rearrange the code.
lammspillning
Forum Newbie
Posts: 14
Joined: Fri Aug 17, 2007 10:50 am

Re: Adding elements on top of xml

Post by lammspillning »

ok, how do you mean?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Adding elements on top of xml

Post by requinix »

Code: Select all

echo "world!";
echo "Hello ";
If you don't like the order then shuffle the lines of code around until you do.
User avatar
susrisha
Forum Contributor
Posts: 439
Joined: Thu Aug 07, 2008 11:43 pm
Location: Hyderabad India

Re: Adding elements on top of xml

Post by susrisha »

Code: Select all

 
DOMNode->insertBefore();
 
This might be what you are looking for
lammspillning
Forum Newbie
Posts: 14
Joined: Fri Aug 17, 2007 10:50 am

Re: Adding elements on top of xml

Post by lammspillning »

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]));
User avatar
php_east
Forum Contributor
Posts: 453
Joined: Sun Feb 22, 2009 1:31 pm
Location: Far Far East.

Re: Adding elements on top of xml

Post by php_east »

maybe easier if

Code: Select all

$xdoc2 = new DomDocument;
$xdoc2 = appendChild(new xml tag)
$xdoc2 = appendChild($xdoc1, deep)
 
something like that.
Post Reply