Solved

How to clone a child node with attributes

Posted on 2004-05-03
11
402 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
  • 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
 

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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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 500 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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

The Problem How to write an Xquery that works like a SQL outer join, providing placeholders for absent data on the outer side?  I give a bit more background at the end. The situation expressed as relational data Let’s work through this.  I’ve …
Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

708 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now