4 Replies - 1274 Views - Last Post: 24 April 2014 - 10:32 AM Rate Topic: -----

#1 midasxl  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 195
  • Joined: 03-December 08

XmlSearch for node with specific attribute value; add child element

Posted 23 April 2014 - 06:08 AM

Hello, and thanks for your time!

I have the following xml file (excerpt):

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="blog.xsl"?>
<demoChanges>
	<entry ID="12345">
		<heading>test</heading>
		<descr>test</descr>
		<author>Mark</author>
		<date>April 22, 2014</date>
		<cert>Mark</cert>
		<draft>no</draft>		
	</entry>
</demoChanges>



I am able to read the xml file, parse it, then using XmlSearch I am able to locate the entry node with the id of "12345", which is a value passed in via a form field (#form.threadid#).

<cffile action="read" file="/path/to/my/xml/file/blog.xml"
variable="xmlFile">

<cfset myXml = XmlParse(xmlFile)>

<cfset myNode = XmlSearch(myXml, "/demoChanges/entry[@ID=#threadid#]") />



What I would like to do now is add a child element called "comment" to this node only, and populate it with a form field value (#formThreadDescr#). I have tried several ways but to no success. Here is but one that has failed me:

<cfset myXml.myNode.comment = XmlElemNew(myXml, "comment")>

<cfset myXml.myNode.comment.xmltext = #formThreadDescr#>



I have tried several others with various resulting errors. Not sure how to do this. Any help will be greatly appreciated as always.

Cheers!

This post has been edited by midasxl: 23 April 2014 - 06:09 AM


Is This A Good Question/Topic? 0
  • +

Replies To: XmlSearch for node with specific attribute value; add child element

#2 Craig328  Icon User is offline

  • I make this look good
  • member icon

Reputation: 1947
  • View blog
  • Posts: 3,488
  • Joined: 13-January 08

Re: XmlSearch for node with specific attribute value; add child element

Posted 23 April 2014 - 06:43 AM

Hey midasxl:

Well, I'm probably the worst CFer to give advice on this because working with XML gives me fits. That said, I do see one possible issue here. When you set myNode when you do this:
<cfset myXml = XmlParse(xmlFile)>

<cfset myNode = XmlSearch(myXml, "/demoChanges/entry[@ID=#threadid#]") />


...your myNode object is already a sub of myXml, right? But then later you try to add to it like this:
<cfset myXml.myNode.comment = XmlElemNew(myXml, "comment")>

<cfset myXml.myNode.comment.xmltext = #formThreadDescr#>


It seems like you should be able to do it like this though:
<cfset myNode.comment = XmlElemNew(myXml, "comment")>

<cfset myNode.comment.xmltext = #formThreadDescr#>



You cut out the myXml reference because that's already established in the myNode alias, right? Truly, I'm just guessing here because, like I said, I suck at the XML. But this seems like a case of mistaken stuct object identities.

Try that and let us know how that works. Good luck!
Was This Post Helpful? 0
  • +
  • -

#3 midasxl  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 195
  • Joined: 03-December 08

Re: XmlSearch for node with specific attribute value; add child element

Posted 23 April 2014 - 06:54 AM

Craig328,
Thanks for your response!

This line:

<cfset myNode.comment = XmlElemNew(myXml, "comment")>


Throws this error:

You have attempted to dereference a scalar variable of type class coldfusion.runtime.Array as a structure with members.

XmlSearch returns an array, so perhaps I need to use array functions to add elements?
Was This Post Helpful? 0
  • +
  • -

#4 Craig328  Icon User is offline

  • I make this look good
  • member icon

Reputation: 1947
  • View blog
  • Posts: 3,488
  • Joined: 13-January 08

Re: XmlSearch for node with specific attribute value; add child element

Posted 23 April 2014 - 07:29 AM

Me...I'd try everything. :D
Was This Post Helpful? 0
  • +
  • -

#5 midasxl  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 195
  • Joined: 03-December 08

Re: XmlSearch for node with specific attribute value; add child element

Posted 24 April 2014 - 10:32 AM

I am certainly trying everything. So far dead ends at each attempt. It's probably simple, I just need to wrap my head around it. So let's recap. Here is a sample of the xml I am dealing with:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="blog.xsl"?>
<demoChanges>
	<entry ID="212332">
		<heading>Test</heading>
		<descr>&lt;p&gt;Test&lt;/p&gt;</descr>
		<author>Mark</author>
		<date>April 24, 2014</date>
		<cert>Mark</cert>
		<draft>no</draft>
		<id>212332</id>
		<comments/>
	</entry>
	<entry ID="43235">
		<heading>Test</heading>
		<descr>&lt;p&gt;&lt;br /&gt;Test&lt;/p&gt;</descr>
		<author>Mark</author>
		<date>April 24, 2014</date>
		<cert>Mark</cert>
		<draft>no</draft>
		<id>43235</id>
		<comments/>
	</entry>
	<entry ID="740515">
		<heading>New</heading>
		<descr>&lt;p&gt;New&lt;/p&gt;</descr>
		<author>Mark</author>
		<date>April 24, 2014</date>
		<cert>Mark</cert>
		<draft>no</draft>
		<id>740515</id>
		<comments/>
	</entry>
</demoChanges>



Using ColdFusion I read the file in, parse it, and then locate the exact entry node that has the id attribute that matches the number passed in from the form scope.

<cffile action="read" file="/path/to/the/blog.xml" variable="xmlFile">

<cfset myXml = XmlParse(xmlFile)>

<cfset myNode = XmlSearch(myXml, "demoChanges/entry[@ID=#threadid#]") />



Dumping #myNode# to the page shows me the correct xml node that I am looking for (see array.jpg).

There is a child element within this entry node called "comments". (see array2.jpg). Now that I have identified the correct entry node, I want to now use that and target the "comments" child element and add an element called "comment"

The end result would look something like this (see the entry node with id="740515"):

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="blog.xsl"?>
<demoChanges>
	<entry ID="212332">
		<heading>Test</heading>
		<descr>&lt;p&gt;Test&lt;/p&gt;</descr>
		<author>Mark</author>
		<date>April 24, 2014</date>
		<cert>Mark</cert>
		<draft>no</draft>
		<id>212332</id>
		<comments/>
	</entry>
	<entry ID="43235">
		<heading>Test</heading>
		<descr>&lt;p&gt;&lt;br /&gt;Test&lt;/p&gt;</descr>
		<author>Mark</author>
		<date>April 24, 2014</date>
		<cert>Mark</cert>
		<draft>no</draft>
		<id>43235</id>
		<comments/>
	</entry>
	<entry ID="740515">
		<heading>New</heading>
		<descr>&lt;p&gt;New&lt;/p&gt;</descr>
		<author>Mark</author>
		<date>April 24, 2014</date>
		<cert>Mark</cert>
		<draft>no</draft>
		<id>740515</id>
		<comments>
                    <comment>Comment Text information goes here.</comment>
                </comments>
        </entry>
</demoChanges>



I'm trying something like this:

<cfset myXml.demoChanges.entry[which one?].comments.XmlChildren = XmlElemNew(myXml,"comment")>
<cfset myXml.demoChanges.entry[which one?].comments.comment.xmltext = #formThreadDescr#>



As you can see my problem ( I think ) is that I do not know how to target the entry that has the attribute id value I'm looking for. Now, I know I have identified that node using XmlSearch, but the result is an array, so I cannot use that in the <cfset>. For example, this does not work:

<cfset myXml.myNode.comments.XmlChildren = XmlElemNew(myXml,"comment")>



It errors out on the 'myNode' part saying you cannot use an array in the dot notation.

I will continue to research, but if anyone reads this and has any ideas, I will be very grateful for your time and assistance.

Thanks!

Attached image(s)

  • Attached Image
  • Attached Image

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1