Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Adding an element in an existing XML document 1

Status
Not open for further replies.

VERSA

IS-IT--Management
Feb 28, 2003
22
US
Hello everyone,
I am finally trying XML and coldfusion. I' am able to parse an xml file and
display it; however I am running into problems when I attempt to add an
element.

The XML file looks like this:

<department name="Shipping Department">
<section name="Links">
<item>
<text>UPS website</text>
<link> <description>Official UPS web Address</description>
</item>
<item>
<text>FedEx</text>
<link> <description>Get Tracking information from
FedEx</description>
</item>
</section>
</department>

I am trying to add a new section with name "Forms".

The code looks as follow:

<cffile action="read" FILE="#menu_file#" variable="menu_data">
<cfset menu=XMLParse(menu_data)>

<cfset xnDepartment = menu.XmlRoot >
<cfset sDepartmentName = xnDepartment.XmlAttributes["name"] >
<cfset nNumSections = ArrayLen(xnDepartment.XmlChildren)>

<!--- create our nwe secton--->
<cfset xnSectiona = XmlElemNew(xnDepartment, "section")>

<!--- specify any attribues.---->
<cfset xnSectiona.XmlAttributes["name"] = "Forms">

<!--- now attach it to the XML Document --->
<cfset ArrayAppend(xnDepartment.XmlChildren, xnSectiona)>


The last line, is the one giving me problems.

Thanks

Percy
 
Hello Everyone, Just wanted to let you know that I figured it out and in case someone else has similar problem.

Apparently, I was not the last line that was giving me the problem. When I entered XMLElemNew, I need to enter the XML Document as the first parameter. In this case:

<cfset xnSectiona = XmlElemNew(menu, "section")>

Thank you anyway,

Percy
 
Star for you. Thanks for sharing your solution with the rest of the forum. People forget to do that.

Human beings, who are almost unique in having the ability to learn from the experience of others, are also remarkable for their apparent disinclination to do so.
-Douglas Adams (1959-2001)
 
Yeah, good job..

Hey you might create this as a tek tips faq...

Call it something like "How do I add an element to an XML Document?"

And explain it a little bit... when you submit it, admin will review and surely approve it so that people can consult with it easily..

ALFII.com
---------------------
If this post answered or helped to answer your question, please reply with such so that forum members with a similar question will know to use this advice.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top