SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Evangelist NokX's Avatar
    Join Date
    Feb 2003
    Location
    Knoxville, TN
    Posts
    525
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    appendChild to a parent in an XML document

    1. i've opened up an xml document in javascript
    2. i determine a node (a parent) i want to append a new node (child) to
    3. i append the child to the parent node
    for some reason, when the child is appended, it is without a closing tag. is there something specific i'm missing when appending a new node to an xml document that will apply a closing tag?

    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>

  2. #2
    Google Engineer polvero's Avatar
    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>
    In your case try this:
    Code:
    <devices>
      <device>foo</device>
      <device>bar</device>
    </devices>


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
  •