1
votes

In ColdFusion's XML DOM abstraction, there is a function to create an element, and you can set the text content of an element through a property. But I can't see how to create a snippet like this:

<a>some<b>text</b>here</a>

I would expect to create text nodes containing the strings, but I don't know how to create a text node. There doesn't seem to be a factory function for that. Is this possible using pure ColdFusion, or do I need to use Java?

3
Have you tried using cdata? <cfset xmlObj.root.childName.xmlText = '<![CDATA[<a>some<b>text</b>here</a>]]>'>. I have never used ColdFusion's XML DOM abstraction so this is just a guess. - Jules
<a> and <b> should be elements containing text; I'm not trying to insert the escaped literal string "<a>". - Thom Smith
So 'a' and 'b' are xml elements, and not Anchor and Bold as part of HTML string you want insert? - Jules
Right. There is no HTML here. - Thom Smith
Then I think your problem is in your nesting. You cant have a="some", a.b="test", and then a="here" afterwards. - Jules

3 Answers

1
votes

The XML document object actually uses text nodes, but doesn't show it in the dump. Try this:

a = xmlParse("<a>some<b>text</b>here</a>");
writeDump(a.xmlRoot.xmlNodes);

writeDump(a.xmlRoot.xmlNodes)

The xmlNodes array is well documented: The XML document object

AFAIK there is no possibility to create a new text node in ColdFusion. But you can always find an existing text node in the document, duplicate it, replace its xmlValue and insert it in any xmlNodes array.

0
votes

You can always use the <cfsavecontent> tag to have complete control over the XML structure. It allows you to do something like this:

<cfset somevariable = "some dynamic value">

<cfsavecontent variable="myXML">
    <cfoutput>
        <?xml version='1.0' encoding='UTF-8'?>
        <a>some<b>text</b>here</a>
        <c>#somevariable#</c>
    </cfoutput>
</cfsavecontent>

<cfdump var="#myXML#">

Then your XML structure is stored in the myXML variable.

Documentation for the cfsavecontent tag.

In cfscript it would look something like this:

<cfscript>
    somevariable = "some dynamic value";

    savecontent variable="myXML" { WriteOutput("<?xml version='1.0' encoding='UTF-8'?><a>some<b>text</b>here</a><c>#somevariable#</c>"); }
    writeDump(myXML);
</cfscript>
0
votes

This is closest I can get for you, asking for XML and not an array.

<cfxml variable="xmlapiData">
    <a b="text">some here</a>
</cfxml>
<cfdump var="#xmlapiData#">

will produce:

enter image description here

That is of course tag based.