all groups > flash data integration > february 2005 >
You're in the

flash data integration

group:

Empty XML node & data binding


Empty XML node & data binding mevenden
2/3/2005 6:40:58 PM
flash data integration:
Hello -

I'm noticing an issue when using XMLConnector and binding one of the values to
a textinput component.

If I do the following:
======================
- Start a new Flash document

- Drag an XMLConnector onto the stage

- Use the following XML file as a schema for the new XMLConnector:

<?xml version="1.0">
<test>
<anode>hello</anode>
</test>

- Drag a TextInput component to the stage

- Bind the text property of the TextInput component to the XMLComponent
results.test.anode property
======================

If I run the above steps and have the XMLComponent load the xml as shown
above, then everything works as expected: the string "hello" appears in the
TextInput field.

On the other hand, if the xml file I use to load into the XMLConnector reads
like this:

<?xml version="1.0">
<test>
<anode></anode>
</test>

Then the string that appears in the TextInput field shows up as "<anode />" ,
not empty like I would expect.

Is this normal behavior? Is there some setting or configuration I can use to
get this to show up empty in the TextInput field?

Thanks for any help!

- Muir
Re: Empty XML node & data binding Macromedia
2/4/2005 2:49:28 PM
Hello,

I've the same problem and i'm becoming crazy !!

Is there any solution?

Thanks

[quoted text, click to view]

Re: Empty XML node & data binding mevenden
2/4/2005 7:51:22 PM
After a little experimenting, I discovered that the XMLConnector when receiving
xml input will convert <element></element> to <element />, and this causes the
textfield to display <element /> if it is bound to the element.

As a workaround I wrote a function that runs after the xml content is
retrieved by the XMLConnector that takes all elements of the form <element />
and converts it to <element></element> - it does this by simply looping thru
all element nodes that have no children and inserting a empty child text node,
as follows:

elementNode.appendChild(my_xml.createTextNode(""));

Not sure if this is the best way to do it, but it seems to work for me, and
has fixed my problem.
Re: Empty XML node & data binding Loxam
2/7/2005 10:27:56 AM
Thanks a lot!

If I understand, when the XMLConnector fire the event "OnResult" I replace
its "results" property by another XML. This XML is provided by a function
which parse the "results" property and replace all empty child.

Is-it ok? could you publish the function?
;-))

Thanks

Re: Empty XML node & data binding Loxam
2/8/2005 2:19:22 AM
Please help!

I wrote this function :

----
function fillEmptyTagWithBlankText(oXML:XML, oXMLNode:XMLNode) {
if(oXMLNode.childNodes.length == 0){
oXMLNode.appendChild(oXML.createTextNode(""));
}else{
for (var i = 0; i < oXMLNode.childNodes.length; i++) {
fillEmptyTagWithBlankText(oXML, oXMLNode.childNodes[i]);
}
}
----

and i call it in the onresult event :

----
var xmlListener:Object = new Object();

function fn_onResult(eventObj:Object) {

trace("Before " + eventObj.target.results);

fillEmptyTagWithBlankText(eventObj.target.results,
eventObj.target.results.firstChild);

trace("After " + eventObj.target.results);

}

xmlListener.result = fn_onResult;

form1.xc.addEventListener("result", xmlListener);
----

like you say all <element /> was replace by <element></element>

(or just to test : <element>foo</element>) but I'll still have the
problem on the input.

Why? It's seems that I done it later...

Thanks,
Loxam
Re: Empty XML node & data binding Loxam
2/8/2005 2:22:36 AM
Please help!

I wrote this function :

----
function fillEmptyTagWithBlankText(oXML:XML, oXMLNode:XMLNode) {
if(oXMLNode.childNodes.length == 0){
oXMLNode.appendChild(oXML.createTextNode(""));
}else{
for (var i = 0; i < oXMLNode.childNodes.length; i++) {
fillEmptyTagWithBlankText(oXML, oXMLNode.childNodes[i]);
}
}
----

and i call it in the onresult event :

----
var xmlListener:Object = new Object();

function fn_onResult(eventObj:Object) {

trace("Before " + eventObj.target.results);

fillEmptyTagWithBlankText(eventObj.target.results,
eventObj.target.results.firstChild);

trace("After " + eventObj.target.results);

}

xmlListener.result = fn_onResult;

form1.xc.addEventListener("result", xmlListener);
----

like you say all <element /> was replace by <element></element>

(or just to test : <element>foo</element>) but I'll still have the
problem on the input.

Why? It's seems that I done it later...

Thanks,
Loxam
Re: Empty XML node & data binding mevenden
2/8/2005 2:00:20 PM
Hello -

Here's the function I wrote - it uses the XPath actionscript library that can
be downloaded at http://www.xfactorstudio.com/ActionScript/AS2/XPath/


import com.xfactorstudio.xml.xpath.*;

function formatEmptyXMLElements(xmlIn:XML):XML {
var tmpXML: Object = new XPathDocument(xmlIn.toString());

// Select all element nodes
var nodes:Array = tmpXML.selectNodes("//*");

// Insert empty text node for elements that have no child nodes
for (var i = 0; i < nodes.length; i++) {
if (!nodes.hasChildNodes()) {
nodes.appendChild(tmpXML.createTextNode(""));
}
}

return XML(tmpXML);
}


-------
As for the problem you are having, I think you have to be sure the XML is
updated before it is actually bound to the text field. My solution to this was
it use an intermediary holder for the data (using a DataHolder component), i.e.:

xml_con >1> xml_dh >2> text_field

The XMLConnector xml_con is not bound to the DataHolder xml_dh - #1 above is
a function that takes the xml result from xml_con, formats the empty nodes with
empty text nodes, and assignes it to the data property of temp_dh. xml_dh is
the component that is actually bound to the text_field (#2).

The purpose of all this was so I could run #1 after the binding #2 had taken
place, certain that the XML data had been processed first before sending it to
the dataholder that is bound to the text field.

In your case, updating the xml connector results after the binding to the text
field had taken place won't I believe update the text field with your updated
xml (since populating the text field is done when the result event fires from
the connector after the xml is loaded).

Anyhow, I think this is what is happening, and the solution I chose seems to
work for me...probably not the most efficient or elegant, but what can I say!
Good luck...

Re: Empty XML node & data binding mevenden
2/8/2005 2:06:01 PM
The code I posed in my previous message seems to have gotten a bit screwed up -
I'm attaching it again....

import com.xfactorstudio.xml.xpath.*;

public function formatEmptyXMLElements(xmlIn:XML):XML {
var tmpXML:Object = new XPathDocument(xmlIn.toString());

// Select all element nodes
var nodes:Array = tmpXML.selectNodes("//*");

// Insert empty text node for elements that have no child nodes
for (var i = 0; i < nodes.length; i++) {
if (!nodes[i].hasChildNodes()) {
nodes[i].appendChild(tmpXML.createTextNode(""));
}
}

return XML(tmpXML);
}
Re: Empty XML node & data binding Loxam
2/8/2005 4:42:25 PM
Thanks a lot for your help!

As you say : "not the most efficient or elegant" but one solution is better
than none ;-))

If i found better I post it ;-))

Thuss,
Loxam


[quoted text, click to view]

AddThis Social Bookmark Button