To answer your second question first, as far as I know, the reason that you
pass the XML DOM when performing an XMLElemNew() function call is that
complex structures are passed by reference and every XML Node in a DOM
contains a key called "XMLParent" that is a pointer to the XML DOM the node
is nested within (it is useful when you get search results back from an
XMLSearch() XPATH searc and then want to loop over the results and access
the DOM). As for your first question, ArrayAppend() will only return "true"
- the easy way to know the children array index just added is to get the
current length of the array. One good way to do this is to create a
variable who's value is the length of the children array + 1 before adding
the new node: that variable would hold the index just created after the
insert has been performed. It would look like:
<cfscript>
xmlSites = XMLNew();
xmlSites.xmlRoot = XMLElemNew(variables.xmlSites,"sites");
//add first site
nodeToAdd = arrayLen(variables.xmlSites.XMLRoot.XMLChildren) + 1;
xmlSites.XMLRoot.XMLChildren[variables.nodeToAdd] =
XMLElemNew(variables.xmlSites,"site");
structInsert(variables.xmlSites.XMLRoot.XMLChildren[variables.nodeToAdd].XML
Attributes,"ownerid","Texaco",true);
structInsert(variables.xmlSites.XMLRoot.XMLChildren[variables.nodeToAdd].XML
Attributes,"siteid","123456",true);
writeOutput("Just added number " & variables.nodeToAdd & " to the
array!<br>");
//add second site
nodeToAdd = arrayLen(variables.xmlSites.XMLRoot.XMLChildren) + 1;
xmlSites.XMLRoot.XMLChildren[variables.nodeToAdd] =
XMLElemNew(variables.xmlSites,"site");
structInsert(variables.xmlSites.XMLRoot.XMLChildren[variables.nodeToAdd].XML
Attributes,"ownerid","Texaco",true);
structInsert(variables.xmlSites.XMLRoot.XMLChildren[variables.nodeToAdd].XML
Attributes,"siteid","456789",true);
writeOutput("Just added number " & variables.nodeToAdd & " to the
array!<br>");
</cfscript>
You can see that there's a lot of repetition in that code... a more elegant
aproach would be to use a function like so:
<cfscript>
function addSite(xmlDOM, xmlArrayToAppendTo,ownerid,siteid){
var nextNode = arrayLen(arguments.xmlArrayToAppendTo) + 1;
arguments.xmlArrayToAppendTo[nextNode] =
XMLElemNew(arguments.xmlDOM,"site");
structInsert(arguments.xmlArrayToAppendTo[nextNode].XMLAttributes,"ownerid",
arguments.ownerid,true);
structInsert(arguments.xmlArrayToAppendTo[nextNode].XMLAttributes,"siteid",a
rguments.siteid,true);
return nextNode;
}
xmlSites = XMLNew();
xmlSites.xmlRoot = XMLElemNew(variables.xmlSites,"sites");
justAdded =
addSite(xmlSites,xmlSites.XMLRoot.XMLChildren,"Texaco","123456");
writeOutput("Just added number " & variables.justAdded & " to the
array!<br>");
justAdded =
addSite(xmlSites,xmlSites.XMLRoot.XMLChildren,"Texaco","456789");
writeOutput("Just added number " & variables.justAdded & " to the
array!<br>");
</cfscript>
~Simon
Simon Horwith
CTO, Etrilogy Ltd.
Member of Team Macromedia
Macromedia Certified Instructor
Certified Advanced ColdFusion MX Developer
Certified Flash MX Developer
CFDJList - List Administrator
http://www.how2cf.com/
-----Original Message-----
From: Aidan Whitehall [mailto:[EMAIL PROTECTED]
Sent: 27 January 2004 17:31
To: [EMAIL PROTECTED]
Subject: RE: [ cf-dev ] XML attribute
> do you still have questions?
Well, since you ask, only a few new ones... :-)
It's working ok so far, but am obviously missing some of the finer
points. I'm working towards something like this:
<sites>
<site ownerid="Texaco" siteid="123456"/>
<site ownerid="Texaco" siteid="456789"/>
<site...>
</sites.
I'm adding "site" nodes to the "sites" node using arrayAppend() and then
setting its attribute values using structInsert(). With the
structInsert() bit, in order to get a reference on the element you just
inserted, it seems like you have to arse about using:
xmlDoc.xmlRoot["sites"].xmlChildren[arrayLen(xmlDoc.xmlRoot["sites"].xml
Children)].xmlAttributes
Is there any form of syntax where the arrayInsert() returns a reference
to the node inserted, so you can do something like (realise this dinnae
work):
x = arrayAppend(xmlDoc.xmlRoot["sites"].xmlChildren, xmlElemNew(xmlDoc,
"site"));
structInsert(x.xmlAttributes, "ownerid", "Texaco");
structInsert(x.xmlAttributes, "siteid", "123456");
Oh, and (Q2) when you do something like
arrayAppend(xmlDoc.xmlRoot.xmlChildren, xmlElemNew(xmlDoc, "sites"));
why is one of the arguments in xmlElemNew() the XML object, when it's
also contained within one of the other arguments in the arrayAppend()
function? I would have thought that xmlElemNew() would only have taken
one argument -- the name of the element you want created.
Really appreciate everyone's help so far, btw.
--
Aidan Whitehall <mailto:[EMAIL PROTECTED]>
Macromedia ColdFusion Developer
Fairbanks Environmental Ltd +44 (0)1695 51775
Queen's Awards Winner 2003 <http://www.fairbanks.co.uk/go/awards>
Fairbanks uses anti-spam filtering. If you sent an e-mail and expected
to receive a response but didn't, please call -- it may be that your
e-mail didn't make it to the intended mailbox.
________________________________________________________________________
This e-mail has been scanned for all viruses by Star Internet. The
service is powered by MessageLabs. For more information on a proactive
anti-virus service working around the clock, around the globe, visit:
http://www.star.net.uk
________________________________________________________________________
--
** Archive: http://www.mail-archive.com/dev%40lists.cfdeveloper.co.uk/
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
For human help, e-mail: [EMAIL PROTECTED]
--
** Archive: http://www.mail-archive.com/dev%40lists.cfdeveloper.co.uk/
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
For human help, e-mail: [EMAIL PROTECTED]