We help IT Professionals succeed at work.

How to add to XML obj

Tacobell777
Tacobell777 asked
on
252 Views
Last Modified: 2013-12-24
<root>
  <child1>
    <name>Fred</name>
  </child1>
</root>

How do I add the following to the child
  <child1>
    <name>Fred</name>
  </child1>

and get a result like

<root>
  <child1>
    <name>Fred</name>
    <child1>
      <name>Fred</name>
    </child1>
  </child1>
</root>

With CFSCRIPT...
Comment
Watch Question

Commented:
Here you go:

<cfxml variable="objXML">
<root>
  <child1>
    <name>Fred</name>
  </child1>
</root>
</cfxml>
<cfoutput>
<pre><xmp>#ToString(objXML)#</xmp></pre><br>
<cfscript>
      objXML.root.child1.XmlChildren[2] = XmlElemNew(objXML, "child1");
      objXML.root.child1.child1.XmlChildren[1] = XmlElemNew(objXML, "name");
      objXML.root.child1.child1.XmlChildren[1].XmlText = "Fred";
</cfscript>
<pre><xmp>#ToString(objXML)#</xmp></pre><br>
</cfoutput>

Author

Commented:
The existing xml structure is also stored in a variable, i.e. not as


<cfxml variable="objXML">
<root>
  <child1>
    <name>Fred</name>
  </child1>
</root>
</cfxml>


I'm pretty new to XML in CF, so I could have missed something?
Can you explain what it does exactly, personally I think its not what I ama fter, but as I said XML in CF is a bit new to me..
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Commented:
Tacobell777, Just wanted to followup with this question and see if you still had any questions.  Also, I noticed that the example data you presented was from the Cold Fusion documentation.  By chance could I help explain adding nodes better by dealing with the data structure you are actually using?  Have a good weekend.

Author

Commented:
Hi shooksm,

I've been going through the doco's myself lately and think I pretty well got it, and also I'm not getting paid for it so its more of personal thing at the moment - so I might give it a rest as well ;-)) cheers.

However I'm sure your answer is the correct one.
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.