pre-populating XForm controls with XML data returned from a REST submission

Hi
I have a Restful server application running in Tomcat. This Restful
server has a config file that defines a parameterized query like:
select * from tableX where column1='joe'

On launching the following page:
http://localhost:8080/Restfulserver/lists/tableX

an XML page is returned which is a representation of the rows returned
as a result of the query defined in the config file.

Now, inside my XForm I have the following code: My goal is to get the
contents of the XML from the Server into an instance in the XForm
called DataResponse and then automatically populate text controls in
the XML with the data.
Whatever I do however does not seem to be successful in getting data
into the XForm. Any suggestions are appreciated.

<xforms:instance id="DataResponse">
	<fe:person>
		<fe:lastname/>
		<fe:firstname />
		<fe:email/>
	</fe:person>
</xforms:instance>
...
<xforms:submission 
	id="submit" 
	action="http://localhost:9001/RestServer/lists/person" 
	method="get"
	replace="instance" 
	instance="DataResponse" 
	separator="&"
/>
...
<xforms:textarea ref="instance('DataResponse')/fe:email">
	<xforms:label>Email:</xforms:label>
	<xforms:message level="ephemeral" ev:event="DOMFocusIn">
		Do you find stuff in here that the XML Server sent back!!
	</xforms:message>
	<xforms:action ev:event="DOMActivate">
		<xforms:send submission="submit"/>
	</xforms:action>
</xforms:textarea>

Any suggestions with the code or code changes are appreciated

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

posting XForm

Unfortunately I do not have the SQLLookupType java source file at this point of time. Thanks for your time.
However I have posted my XForm code here:

<?xml version="1.0" encoding="UTF-8"?>
<xhtml:html xmlns:f="http://orbeon.org/xhtml/xml/formatting" 
	xmlns:ev="http://www.w3.org/2001/xml-events" 
	xmlns:xforms="http://www.w3.org/2002/xforms" xmlns:xhtml="http://www.w3.org/1999/xhtml"
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
	xmlns:xs="http://www.w3.org/2001/XMLSchema" 
	xmlns:xi="http://www.w3.org/2001/XInclude"
	xmlns:xf="http://www.w3.org/2002/xforms">
	<xhtml:head>
		<xf:model id="ldapModel">
			<xf:instance id="DataRequest">
				<fe:person>
					<fe:lastname/>
					<fe:firstname></fe:firstname>
					<fe:email/>
				</fe:person>
			</xf:instance>
			
			<xf:instance id="DataResponse">
				<fe:person>
					<fe:lastname/>
					<fe:firstname></fe:firstname>
					<fe:email/>
				</fe:person>
			</xf:instance>
			
			<xf:action ev:event="xforms-ready" >
				<xf:action xf:if="string(instance('taskoutput')/@saved) != 'true'">
				</xf:action>
				<xf:setfocus control="id-field"/>
			</xf:action>
			
			<xf:submission id="submit" action="http://localhost:9001/SimpleXmlServer/XmlIntercept/lists/person" method="get"  
				replace="instance" instance="DataRequest" separator="&">
			</xf:submission>
		</xf:model>
		
	</xhtml:head>
	<xhtml:body>
		
		<xf:textarea ref="instance('DataResponse')/fe:firstname">
			<xf:label>First Name:</xf:label>
			<xf:message level="ephemeral" ev:event="DOMFocusIn">
				Do you find stuff in here that the XML Server sent back!!
			</xf:message>
			<xf:action ev:event="xforms-value-changed">
				
				<xf:send submission="submit"/>
			</xf:action>
		</xf:textarea>
		<xf:textarea ref="instance('DataResponse')/fe:lastname">
			<xf:label>Last Name:</xf:label>
			<xf:message level="ephemeral" ev:event="DOMFocusIn">
				Do you find stuff in here that the XML Server sent back!!
			</xf:message>
			<xf:action ev:event="DOMActivate">
				<!--<xf:send submission="submit"/>-->
				<xf:setvalue ref="instance('DataResponse')/lastname" value="instance('DataRequest')/lastname"/>
			</xf:action>
		</xf:textarea>
		<xf:textarea ref="instance('DataResponse')/fe:email">
			<xf:label>Email:</xf:label>
			<xf:message level="ephemeral" ev:event="DOMFocusIn">
				Do you find stuff in here that the XML Server sent back!!
			</xf:message>
			<xf:action ev:event="DOMActivate">
				<xf:send submission="submit"/>
			</xf:action>
		</xf:textarea>
		
		<!-- The below piece of code probably does not make sense nor is it required (??)  -->
		<xf:submit submission="submit">
			<xf:label>Pre-populate my text area with Server results</xf:label>
		</xf:submit>
		
	</xhtml:body>
</xhtml:html>

Submission port numbers

There's a difference in the submission URL port numbers in your code to the URL you post in the description of what you are trying to do. Is your submission not supposed to use the default port that Tomcat is listening on, 8080 not 9001?

If the submission URL is correct (BTW, I don't think you need the @separator attribute) and is definitely returning a well-formed XML document then your problems may lie with namespace declarations. You don't mention how you are triggering your submission either.

If you post the full source of your form/servlet, it may be easier to see what could be going wrong. Also the version of formsPlayer you are using would be good to know.

Thanks,

Alex

can't see the code I posted

Unfortunately I can't see the code I posted. At least I do not see it. Is there a different way to post code?

thanks
ilango

Formatting code

To format the code so it is visible on the forum you can enclose it in <pre> tags. Then substitute all <'s in the code with &lt;.

The XML that is returned by my Server

This is the XML that is returned by the Server:

<test2>
	<person>
		<firstname>john</firstname>
		<lastname>doe</lastname>
		<email>johndoe@yahoo.com</email>
	</person>
</test2>

So the instance to be replaced would have a root element <person> and after getting replaced it should have a root element <test2> and a child element called <person></person> under it, with the proper namespace.
**In this case, at the present point of time, since I do not have control over the namespace of the returned XML, I should have just <test2> instead of <fe:test2>, right?

thanks
ilango

Yes, you're correct in what

Yes, you're correct in what you say in both points.

If your server is returning the data above then your instance after the submission has completed will be:

<xf:instance id="DataResponse">
	<test2>
		<person>
			<firstname>john</firstname>
			<lastname>doe</lastname>
			<email>johndoe@yahoo.com</email>
		</person>
	</test2>
</xf:instance>

If you've not got control over the format of the returned data, to display it in your form you just need to adjust the @ref values of your controls to, for example, ref="instance('DataResponse')/person/firstname".

Formsplayer version

Hi
I have used 1.5.7.1013.

thanks
ilango

Working form

Here's your form cut down to work by loading the data from a local file rather than to receive the data over HTTP.

For your form to work properly with your Servlet it should declare the 'fe:' at the top of the form in the <html> element and also on the root element of your instance data that the Servlet returns. Ie. The data your Servlet returns should be the same as listed in the substitute_response_data.xml file below.

The substitute_response_data.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<fe:person xmlns:fe="http://fe.com">
	<fe:lastname>LOADED last name</fe:lastname>
	<fe:firstname>LOADED first name</fe:firstname>
	<fe:email>LOADED email</fe:email>
</fe:person>

The form that loads substitute_response_data.xml:

<?xml version="1.0" encoding="UTF-8"?>
<html 
	xmlns:ev="http://www.w3.org/2001/xml-events" 
	xmlns:xf="http://www.w3.org/2002/xforms"
	xmlns:fe="http://fe.com"
>
	<head>		
		<object id="formsPlayer" classid="CLSID:4D0ABA11-C5F0-4478-991A-375C4B648F58">
			<strong>formsPlayer has not loaded.</strong>
		</object>
		<?import namespace="xf" implementation="#formsPlayer" ?>
		
		<xf:model id="ldapModel">
			
			<xf:instance id="DataResponse">
				<fe:person xmlns:fe="http://fe.com">
					<fe:lastname>Default lastname</fe:lastname>
					<fe:firstname>Default firstname</fe:firstname>
					<fe:email>Default email</fe:email>
				</fe:person>
			</xf:instance>
			
			<!--
				Substitute what may be getting returned by Servlet with data from a file. Was:
				
				@action="http://localhost:9001/SimpleXmlServer/XmlIntercept/lists/person"
			-->
			<xf:submission
				id="sub-get-data"
				action="substitute_response_data.xml"
				method="get"
				replace="instance"
				instance="DataResponse"
			/>
			
			<xf:action ev:event="xforms-submit-done" ev:observer="sub-get-data">
				<xf:message level="modal">
					Submission completed successfully.
				</xf:message>
			</xf:action>
			
			<xf:action ev:event="xforms-submit-error" ev:observer="sub-get-data">
				<xf:message level="modal">
					Submission error!
				</xf:message>
			</xf:action>
		</xf:model>
	</head>
	<body>
		<xf:group ref="instance('DataResponse')">
			<xf:textarea ref="fe:firstname">
				<xf:label>First Name:</xf:label>
			</xf:textarea>
			<xf:textarea ref="fe:lastname">
				<xf:label>Last Name:</xf:label>
			</xf:textarea>
			<xf:textarea ref="fe:email">
				<xf:label>Email:</xf:label>
			</xf:textarea>
		</xf:group>
		<!-- 
			Execute submission by xf:trigger
		-->
		<xf:trigger>
			<xf:label>Reset form</xf:label>
			<xf:reset model="ldapModel" ev:event="DOMActivate" />
		</xf:trigger>
		<xf:trigger>
			<xf:label>Do submission</xf:label>
			<xf:send submission="sub-get-data" ev:event="DOMActivate" />
		</xf:trigger>
	</body>
</html>

The instance data file and the form should reside in the same directory.

Cheers,

Alex

Will try out your new form

Hi Alex
I will try out your new form and get back to you with my results. I appreciate your time and efforts.

thanks
ilango

Tomcat Port

I have another application server that runs on port 8080 and this runs at the same time that my Tomcat server does. So I changed my Tomcat port to 9001.

Thanks
ilango

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.