473,327 Members | 1,896 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

PHP Loop with appendChild for XML

16
I have a XML file with multiple <product> ... </product>
I want to have a php file that adds a child to each product.
I'm using the following php code for this:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.     $xdoc = new DomDocument;
  3.     $xdoc->Load('test.xml');
  4.     $product = $xdoc->getElementsByTagName('product')->item(0);
  5.     $newElement = $xdoc ->createElement('store');
  6.     $txtNode = $xdoc ->createTextNode ("admin");
  7.     $newElement -> appendChild($txtNode);
  8.     $product -> appendChild($newElement);
  9.     $test = $xdoc->save("test2.xml");
  10. echo "<B>Data Added<B>"
  11. ?>
This code adds <store>admin</store> only to the first product element because I have item(0) and then creates my second xml file. I tried to make a loop and change the 0 to a variable and use the loop to increase the number, so that it adds "<store>admin</store>" to all products, but I can't figure it out.

Any idea how this can be done?

Thanks in advance...
Aug 28 '08 #1
2 4073
Dormilich
8,658 Expert Mod 8TB
You could go a totally different way and use a xsl stylesheet to do that.
Aug 28 '08 #2
Atli
5,058 Expert 4TB
Hi.

You could use a foreach loop.
Like:
Expand|Select|Wrap|Line Numbers
  1. $products = $xdoc->getElementsByTagName('product');
  2. foreach($products as $_product) {
  3.   // Create your $newElement
  4.   $_product->appendChild($newElement);
  5. }
  6.  
Aug 28 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

25
by: kie | last post by:
hello, i have a table that creates and deletes rows dynamically using createElement, appendChild, removeChild. when i have added the required amount of rows and input my data, i would like to...
16
by: TheKeith | last post by:
I'm writing a script with a while loop in it. How can I get it to go slower so that it doesn't appear to happen all at once--so that it looks animated--basically. I tried the setTimeout(500) in the...
2
by: samuel.adam | last post by:
Hi all, I am coding an AJAX DHTML whatever application and I was fed up with always typing a lot of appendChild() functions. I created a custom one called append_children() and wanted to share...
9
by: Bill Nguyen | last post by:
I need a VB routine to loop thru a select top folder to find all subfolders and list all subfolders/files under each of these subfolders. Any help is greatly appreciated. Bill
10
by: Sean Dockery | last post by:
I have the following HTML file that I've been using for testing... <html> <head> <script type="text/javascript"> <!-- function handleWindowLoad() { var items = ; for (var i = 0; i < 11; i++)...
2
by: mrjoka | last post by:
hi experts, i'm developing a page in ASP but i'm doing also some javascript insode the page. i'm creating a frame and i want to loop this frame with a duplicateloop function so the form will be...
6
by: s_m_b | last post by:
Got this code, which works fine in FF3 - it dynamically redraws a menu from user input. #### function so_clearInnerHTML(obj) { // so long as obj has children, remove them while(obj.firstChild)...
2
by: DragonLord | last post by:
Ok basically this is mostly working, What i want to do is create an xml file with multiple updates that have the same attribute. <root> <update> <element1>value</element1> ...
2
by: vsanjit | last post by:
Hi all, I've been trying to create a table dynamically upon the generation of en event using the appendChild method in Javascript. This seems to work fine in Firefox, but not in IE7. There's also...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.