Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to clone a child node with attributes

Posted on 2004-05-03
11
Medium Priority
?
418 Views
Last Modified: 2012-06-27
Hi.

I want to clone a node, but I am not sure how to deal with attributes. I have a structure similar to this:

<emp id="b12">Paul Anderson</emp>

I know how to clone the child nodes, but have not been able to clone the 'id' attribute.

Any help is appreciated since this is a time-sensitive project.

Thanks. J~
0
Comment
Question by:chappi77
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
11 Comments
 
LVL 10

Expert Comment

by:Yury_Delendik
ID: 10977664
empNode.cloneNode(true)
0
 

Author Comment

by:chappi77
ID: 10977848
Yes, I got that. However, I also need to change the values of the cloned items, the attribute value in particular.

Thanks.
0
 
LVL 10

Expert Comment

by:Yury_Delendik
ID: 10978343
what parser are you using?

0
AWS Certified Solutions Architect - Associate

This course has been developed to provide you with the requisite knowledge to not only pass the AWS CSA certification exam but also gain the hands-on experience required to become a qualified AWS Solutions architect working in a real-world environment.

 

Author Comment

by:chappi77
ID: 10978380
MSXML? I'm kinda new to this part, but I think that's what you're looking for :)
0
 
LVL 10

Expert Comment

by:Yury_Delendik
ID: 10978390
empNode.setAttribute("id", "new-value")

empNode.text = "value" or empNode.InnerText = "value"
0
 

Author Comment

by:chappi77
ID: 10978477
I tried that and it didn't work. Here is the function I am using:

            function addCD()
            {
                  var old = XMLdoc.getElementsByTagName("cd")[0];
                  var clone = old.cloneNode(true);

                  clone.childNodes[0].nodeValue = document.myForm.cdName.value;
                  clone.childNodes[1].firstChild.nodeValue = document.myForm.artist.value;
                  clone.childNodes[2].firstChild.nodeValue = document.myForm.price.value;

                  var tracksLength = allTracks.length;
                  var counter;
                  var myTrack;
                  var myLength;

                  for (counter = 0; counter <= tracksLength; counter++)
                  {
                        myTrack = allTracks[counter];
                        myLength = allLengths[counter];

                        if (myTrack == "0")
                        {
                              break;
                        }
                        clone.childNodes[3 + counter].firstChild.nodeValue = myTrack;
                        clone.childNodes[3 + counter].firstChild.setAttribute("length", myLength);
                  }
      
                  XMLdoc.documentElement.appendChild(clone);

                  transformDOC();
            }

I am trying to change the attribute of the track childnode to the attribute I get in my form. I get an error message returned: Object does not support this property or method... Any idea?

Thanks.
0
 
LVL 10

Expert Comment

by:Yury_Delendik
ID: 10978542
what is clone.childNodes[3 + counter].firstChild ?
use clone.childNodes[3 + counter].firstChild.nodeTypeString
It has to be an element.
0
 

Author Comment

by:chappi77
ID: 10978916
clone.childNodes[3 + counter].firstChild refers to the first child (track) of the child node "cd". This works fine. The only one I can't seem to figure out is the attribute. if I use clone.childNodes[3 + counter].firstChild.nodeTypeString then it does not work at all.
0
 
LVL 10

Accepted Solution

by:
Yury_Delendik earned 2000 total points
ID: 10979040
Is allLengths array?

clone.childNodes[3 + counter].firstChild.setAttribute("length", myLength);
will work if clone.childNodes[3 + counter].firstChild exists and it's element
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
Create a Windows 10 custom Image with custom task bar and custom start menu using XML for deployment.
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question