SitePoint Sponsor |
|
User Tag List
Results 1 to 2 of 2
-
Nov 27, 2007, 09:54 #1
- Join Date
- Feb 2003
- Location
- Knoxville, TN
- Posts
- 531
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
appendChild to a parent in an XML document
- i've opened up an xml document in javascript
- i determine a node (a parent) i want to append a new node (child) to
- i append the child to the parent node
here's the very simple javascript i'm using...
Code JavaScript:// 'parent' is the node i'm wanting to append the new child node to in the xml document var device = myXML.createElement('device'); /* setting some attributes */ parent.appendChild(device);
and here's the XML before i append...
Code XML:<config> <interface name="Interface 1"> <device name="Device 1"> <device name="Device 2"> <!--- the node i want to append to ---> <device name="Device 3"> <property name="Property 1"></property> </device> </device> </device> <device name="Device 4"></device> <device name="Device 5"></device> <device name="Device 6"></device> </interface> </config>
after the append...
Code XML:<config> <interface name="Interface 1"> <device name="Device 1"> <device name="Device 2"> <!--- the node i want to append to ---> <device name="Device 3"> <property name="Property 1"></property> </device> </device> <device name="Device 7"></device> <!--- where the new node is wrongly placed ---> <device name="Device 4"></device> <device name="Device 5"></device> <device name="Device 6"></device> </interface> </config>
and what it should look like...
Code XML:<config> <interface name="Interface 1"> <device name="Device 1"> <device name="Device 2"> <!--- the node i want to append to ---> <device name="Device 3"> <property name="Property 1"></property> </device> <device name="Device 7"></device> <!--- the new node ---> </device> </device> <device name="Device 4"></device> <device name="Device 5"></device> <device name="Device 6"></device> </interface> </config>
-
Nov 30, 2007, 17:30 #2
- Join Date
- Oct 2003
- Location
- Mountain View
- Posts
- 567
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
You should consider not having childnodes with the same names as their parents (kids don't like that anyway).
For example:
Code:<items> <!-- note plural --> <item>one</item> <!-- note singular --> <item>two</item> </items>
Code:<devices> <device>foo</device> <device>bar</device> </devices>
./with Imagination: Dustin Diaz
Buy my book: Pro JavaScript Design Patterns
DOM Scripting Basics
Guard the Homeless
Bookmarks