Solved

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

Posted on 2011-09-06
5
652 Views
Last Modified: 2012-05-12
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
Comment
Question by:aduvall
  • 3
  • 2
5 Comments
 
LVL 29

Expert Comment

by:dgofman
ID: 36492465
What the problem you need to use xmlData variable

trace(xmlData.ConsumerTrackingUrl);
trace(xmlData.OrderUrl);
0
 

Author Comment

by:aduvall
ID: 36493190
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
 
LVL 29

Expert Comment

by:dgofman
ID: 36493233
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
 

Author Comment

by:aduvall
ID: 36493322
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
 
LVL 29

Accepted Solution

by:
dgofman earned 500 total points
ID: 36493375
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now