AS3 E4X, custom namespaces but no default namespace



I've seen plenty of posts about getting namespaced xml data, but this is making me bonkers. I am hitting a web service that is responding with some xml:



<?xml version="1.0" encoding="utf-8"?>
<response xmlns:xsi="http://ift.tt/ra1lAU" xmlns:xsd="http://ift.tt/tphNwY">
<host_name>hostname</host_name>
<keyField>
<fieldType>1</fieldType>
<fieldTypeKey>job_id</fieldTypeKey>
<stringValue>38252BCD-9FEF-4904-9AC0-8BF88F8B35B0</stringValue>
</keyField>
<field>
<fieldType>1</fieldType>
<fieldTypeKey>alarm</fieldTypeKey>
<stringValue>channel 1</stringValue>
</field>
<field>
<fieldType>1</fieldType>
<fieldTypeKey>asset_state</fieldTypeKey>
<stringValue>IDLE</stringValue>
</field>
<field>
<fieldType>1</fieldType>
<fieldTypeKey>code_out</fieldTypeKey>
<stringValue>00:00:00.00</stringValue>
</field>
</response>


I'm trying to extract stringValue where fieldTypeKey == "asset_state":



var response:XML = XML(evt.target.data);
trace("1: "+response.namespace());
trace("2: "+getQualifiedClassName(response.namespace()));
trace("3: "+String(response.field.(fieldTypeKey == "asset_state").stringValue));
trace("4: "+getQualifiedClassName(String(response.field.(fieldTypeKey == "asset_state").stringValue)));
trace("5: "+response.field.(fieldTypeKey == "asset_state").toXMLString());
var state:String = String(response.field.(fieldTypeKey == "asset_state").stringValue);
trace("6: state: "+state);


Output:



1:
2: Namespace
3: IDLE
4: String
5: <field xmlns:xsi="http://ift.tt/ra1lAU" xmlns:xsd="http://ift.tt/tphNwY">
<fieldType>1</fieldType>
<fieldTypeKey>asset_state</fieldTypeKey>
<stringValue>IDLE</stringValue>
</field>
TypeError: Error #1010: A term is undefined and has no properties.


TypeError occurs when setting state. I have tried many, many variations of trying to get stringValue as a String, and in every case, it traces out fine, but when I try to set the value to a var, I get the TypeError.


I can see that the namespaces are being added to the field node. My question is, how do I deal with there being no default namespace set? I have tried all manner of setting the default namespace to a new Namespace, getting the response namespace and using that, etc.


I know I could probably get around it by stripping the namespaces out, but that's such a hack, I don't want to do that. Any suggestions?


No comments:

Post a Comment