• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 699
  • Last Modified:

Flash Builder 4 URLLoader How to read a single XML element using RESTFUL

I have a function that injects an XML order using PUT and the URLRequest method.

I receive an XML response and I simply want to read an XML tag in the response so that I may save the data for future changes to the order.  Is there a way to do this?

Here is the response.  I only want to read the OrderUrl tag so that I may save it in a database for later use.

<NewOrderResponse xmlns="http://schemas.datacontract.org/2004/07/supermansystems" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
  <ConsumerTrackingUrl>http://superman.com/Customer/ViewOrderLocation/41a63fc5-6895-4016-bfa6-56dcca4d8b2e</ConsumerTrackingUrl>
  <OrderUrl>http://superman.com/Orders.svc/Orders/41a63fc5-6895-4016-bfa6-56dcca4d8b2e</OrderUrl>
</NewOrderResponse>
public var loader:URLLoader = new URLLoader();
			public function sendData(){
				
				loader.addEventListener(Event.COMPLETE, onComplete); 
				loader.addEventListener(Event.CHANGE, onChange);
				loader.addEventListener(HTTPStatusEvent.HTTP_STATUS,onStatus);
				
				
				
				var request:URLRequest = new
					URLRequest("http://superman.com/Stores.svc/Stores/b1d0cfaf-966a-4525-b643-f4abf0436f96/Orders");
				
				request.method = URLRequestMethod.POST;
				
				request.contentType = "application/xml"; // Indicates payload is json
				//format
				
				
				request.data = "<Order xmlns=\"http://schemas.datacontract.org/2004/07/BlinkTrac360TypeSystem\"><CustomerName>"+scfname.text+" "+sclname.text+"</CustomerName><EmailAddress>"+emailer.text+"</EmailAddress><EstimatedDeliveryTime>"+scdate.text+"T"+scstime.text+"</EstimatedDeliveryTime><LineItems><OrderLineItem><Description>notes</Description><Options><OrderLineItemOption><Description>String content</Description><Price>1234.99</Price></OrderLineItemOption><OrderLineItemOption><Description>String content</Description><Price>1234.99</Price></OrderLineItemOption></Options><Price>1234.99</Price><Quantity>999</Quantity></OrderLineItem><OrderLineItem><Description>String content</Description><Options><OrderLineItemOption><Description>String content</Description> <Price>1234.99</Price></OrderLineItemOption><OrderLineItemOption><Description>String content</Description><Price>1234.99</Price></OrderLineItemOption></Options><Price>1223.99</Price><Quantity>999</Quantity></OrderLineItem></LineItems><Note>String content</Note><PostalAddress><City>Louisville</City><GeoLocation><Latitiude>55.543233</Latitiude><Longitiude>-100.543233</Longitiude> </GeoLocation><PostalCode>"+sczip.text+"</PostalCode><StateOrProvince>"+sstate.text+"</StateOrProvince><StreetAddress1>"+scstreet.text+"</StreetAddress1><StreetAddress2></StreetAddress2></PostalAddress><Price>1234.99</Price><RemoteSystemOrderTag>XXXXXX</RemoteSystemOrderTag><TextPhoneNumber>"+phoneFormatter.format(smphone.text)+"</TextPhoneNumber><VoicePhoneNumber>"+phoneFormatter.format(smphone.text)+"</VoicePhoneNumber></Order>";
				
				var authhead:String =                                 "bloomerplumbing:BD99D50AA1D0448AA0E8A197B947D663290FF59BB89942C4A33B55117CDFB4EE";
				
				var headers:Array = [
					new URLRequestHeader("X-HTTP-Method-Override","PUT"),
					new URLRequestHeader("Authorization", authhead)];
				
				request.requestHeaders = headers;
				
				trace(scfname.text+" "+sclname.text);
				trace(smphone.text);
				loader.load(request);
				
				
			}
			
public function onComplete(evt:Event){
				trace(evt);
				//trace(loader.data);
				
				var xmlLoader:URLLoader  = new  URLLoader();
				var xmlData:XML = new XML();
				xmlData = new XML(evt.target.data);
				trace(xmlData);

			}
The response:
<NewOrderResponse xmlns="http://schemas.datacontract.org/2004/07/supermansystems" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
  <ConsumerTrackingUrl>http://superman.com/Customer/ViewOrderLocation/41a63fc5-6895-4016-bfa6-56dcca4d8b2e</ConsumerTrackingUrl>
  <OrderUrl>http://superman.com/Orders.svc/Orders/41a63fc5-6895-4016-bfa6-56dcca4d8b2e</OrderUrl>
</NewOrderResponse>

Open in new window

0
aduvall
Asked:
aduvall
  • 3
  • 2
1 Solution
 
dgofmanCommented:
What the problem you need to use xmlData variable

trace(xmlData.ConsumerTrackingUrl);
trace(xmlData.OrderUrl);
0
 
aduvallAuthor Commented:
Hello Yes I have tried that and I get a blank trace.

I suspect it may have something to do with the xml response.  

0
 
dgofmanCommented:
You can trace but when you are creating a XML object its failing?

Can we do one experiment? Are you using Flex, so replace Loader by HTTPObject

<mx:HTTPService id="userRequest" url=" http://superman.com/Stores.svc/Stores/b1d0cfaf-966a-4525-b643-f4abf0436f96/Orders" useProxy="false" method="POST" resultFormat="e4x" result=”onComplete”>

http://livedocs.adobe.com/flex/3/html/help.html?content=data_access_2.html
0
 
aduvallAuthor Commented:
It will work without xmlns="http://schemas.datacontract.org/2004/07/BlinkTrac360TypeSystem" xmlns:i="http://www.w3.org/2001/XMLSchema-instance" in the xml tag but that is how it is returned.

Also if I must use HTTPService can I use "X-HTTP-Method-Override","PUT".
0
 
dgofmanCommented:
should be possible

<fx:Script>
			<![CDATA[
			import mx.rpc.events.ResultEvent; 
			import mx.rpc.events.FaultEvent; 
			
			override protected function createChildren():void
			{
				super.createChildren();
				var authhead:String = "bloomerplumbing:BD99D50AA1D0448AA0E8A197B947D663290FF59BB89942C4A33B55117CDFB4EE";
				httpServoce.headers = {'Authorization':authhead,
								   'X-HTTP-Method-Override':'PUT'};
				//httpServoce.send(); 
			} 
			
			private function resultHandler(event:ResultEvent):void 
			{ 
				trace(event.result); 
			} 
			
			private function resultFailed(event:FaultEvent):void { 
				trace(event.fault.toString()); 
			} 

		]]>
	</fx:Script>

	<fx:Declarations>
	<mx:HTTPService id="httpServoce" 
					url="http://superman.com/Stores.svc/Stores/b1d0cfaf-966a-4525-b643-f4abf0436f96/Orders"
					useProxy="false" method="POST" resultFormat="e4x" 
					result="resultHandler(event)" 
					fault="resultFailed(event)"/>
	</fx:Declarations>

Open in new window

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now