?
Solved

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

Posted on 2011-09-06
5
Medium Priority
?
683 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 1500 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

Industry Leaders: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses

764 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