A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: Add new node

  1. #1
    Member
    Join Date
    Jun 2002
    Posts
    53

    Add new node

    this is supose to be quite simple yet i haven't found any thread that explains how to-
    I need to add a new node (in flash), and than had 4 childs to that node?
    please, help

  2. #2
    Senior Member catbert303's Avatar
    Join Date
    Aug 2001
    Location
    uk
    Posts
    11,222
    Hi,

    here's a short example

    code:

    main_xml = new XML("<main></main>"); // create an XML object to add the new nodes to

    parentElement_xml = main_xml.createElement("parent_element"); // create a new element, named parent_element to hold the child nodes

    // now create the child nodes and add them to parent_element
    childNode_xml = main_xml.createElement("child_node1");
    // add some text to the child node
    childNode_xml.appendChild(main_xml.createTextNode( "this is some text in the first child node"));
    // append the child node to the parent
    parentElement_xml.appendChild(childNode_xml);

    // now create another child node
    childNode_xml = main_xml.createElement("child_node2");
    childNode_xml.appendChild(main_xml.createTextNode( "this is some text in the second child node"));
    parentElement_xml.appendChild(childNode_xml);

    // add as many more child nodes as you need
    // then append parent_element (with its child nodes) to our XML object, main_xml.firstChild is the main node created when the XML object was created, new XML("<main></main>")
    main_xml.firstChild.appendChild(parentElement_xml) ;

    trace(main_xml);


  3. #3
    Member
    Join Date
    Jun 2002
    Posts
    53
    thanks, i'll try it,is it possible adding the new node not at the end of the xml?

  4. #4
    Senior Member catbert303's Avatar
    Join Date
    Aug 2001
    Location
    uk
    Posts
    11,222
    Yes there is an insertBefore method that allows you to add a new node before another one.

    for example,

    code:

    // create an XML object
    test_xml = new XML("<something></something>");
    // create a new node for it
    newNode_xml = test_xml.createElement("first_element");
    // add this new node to test_xml
    test_xml.firstChild.appendChild(newNode_xml);
    // see how that looks
    trace(test_xml);
    // now create another new node
    newNode_xml = test_xml.createElement("second_element");
    // and add this node before first_element
    test_xml.firstChild.insertBefore(newNode_xml, test_xml.firstChild.firstChild);
    // see how that looks now
    trace(test_xml);



    in the line,

    test_xml.firstChild.insertBefore(newNode_xml, test_xml.firstChild.firstChild);

    test_xml.firstChild is the something node of the xml, the one we want to add nodes to

    test_xml.firstChild.firstChild is the first_element node of the XML which the new node is being inserted before.

  5. #5
    Member
    Join Date
    Jun 2002
    Posts
    53
    thanks.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center