Serving the XML data to a form
XML is an information interchange format that can be used to provide the data that a form
will display. This recipe shows how to create a contact form that feeds off the XML data that is
sent from the server. This is how the contact form looks like after receiving the XML data:

How to do it…
- Build the name, company, picture box, internet, phone, business address,
and home address panels as explained in the first six steps of the recipe Loading
form data from the server. - Create the contact form and assign it an XmlReader as its data reader:
var contactForm = new Ext.FormPanel({
frame: true,
title: ‘TODO: Load title dynamically’,
bodyStyle: ‘padding:5px’,
width: 650,
url: ‘contact-xml.php’,
reader: new Ext.data.XmlReader({
record: ‘contact’,
success: ‘@success’
},
[‘id’, ‘firstName’, ‘lastName’, ‘company’, ‘title’,
‘pic’,’email’, ‘webPage’, ‘imAddress’, ‘homePhone’,
‘busPhone’,’mobPhone’, ‘fax’, ‘bAddress’, ‘hAddress’,
‘mailingAddress’] ),
items: [{
bodyStyle: {
margin: ‘0px 0px 15px 0px’
},
items: [{
layout: ‘column’,
items: [nameAndCompany, picBox] }] }, {
items: [{
layout: ‘column’,
items: [phones, internet] }] }, {
xtype: ‘fieldset’,
title: ‘Addresses’,
autoHeight: true,
hideBorders: true,
layout: ‘column’,
items: [busAddress, homeAddress] }],
buttons: [{
text: ‘Save’
},
{
text: ‘Cancel’
}] });
- Handle the form’s actioncomplete event:
contactForm.on({
actioncomplete: function(form, action){
if(action.type == ‘load’){
var contact = action.result.data;
Ext.getCmp(contact.mailingAddress).setValue(true);
contactForm.setTitle(contact.firstName + ‘ ‘ +
contact.lastName);
Ext.getDom(‘pic’).src = contact.pic;
}
}
});
- Render the form:
contactForm.render(document.body); - Finally, load the form:
contactForm.getForm().load({ method:’get’,
params: { id: ‘contact1’ }, waitMsg: ‘Loading’ });
How it works…
The contact form’s building sequence is similar to the one described in the recipe
Loading form data from the server. This is the resulting form, with the placement of each of
the panels pinpointed:

Although a real-world application will most likely do some processing to generate the XML
information, for this example the server page will generate sample data as simply as this:
<?php
header(“content-type: text/xml”);
echo “<?xml version=\”1.0\” encoding=\”utf-8\” ?>”;
echo “<message success=\”true\”>”;
echo ” <contact>”;
echo ” <id>contact1</id>”;
echo ” <firstName>Jorge</firstName>”;
echo ” <lastName>Ramon</lastName>”;
echo ” <company>MiamiCoder</company>”;
echo ” <title>Mr</title>”;
echo ” <pic>img/jorger.jpg</pic>”;
echo ” <email>ramonj@miamicoder.com</email>”;
echo ” <webPage>http://www.miamicoder.com</webPage>”;
echo ” <imAddress></imAddress>”;
echo ” <homePhone></homePhone>”;
echo ” <busPhone>555 555-5555</busPhone>”;
echo ” <mobPhone></mobPhone>”;
echo ” <fax></fax>”;
echo ” <bAddress>123 Acme Rd #001 Miami, FL 33133</bAddress>”;
echo ” <hAddress></hAddress>”;
echo ” <mailingAddress>mailToBAddress</mailingAddress>”;
echo ” </contact>”;
echo “</message>”;
?>
The data consists of a message entity with a success attribute, which signals whether the
request succeeded, and a contact entity for the contact information.
This data can be read by the form, thanks to the use of an XmlReader instance:
reader: new Ext.data.XmlReader({
record: ‘contact’,
success: ‘@success’
}, [‘id’, ‘firstName’, ‘lastName’, ‘company’, ‘title’, ‘pic’,’email’,
‘webPage’, ‘imAddress’, ‘homePhone’, ‘busPhone’,’mobPhone’,
‘fax’, ‘bAddress’, ‘hAddress’, ‘mailingAddress’]
)
The handler for the actioncomplete event is used to process the results of the load or
submit actions:
contactForm.on({
actioncomplete: function(form, action){
if(action.type == ‘load’){
.
.
.
}
if(action.type == ‘submit’){
.
.
.
}
}
});
When the handler processes the load action, it sets the default mailing address, the form’s
title, and the contact’s picture.
As mentioned, the contact’s information arrives in the data property of the action’s result:
var contact = action.result.data;
The default mailing address comes in the contact’s mailingAddress property, and the radio
button for the default mailing address is set as shown in the following code:
Ext.getCmp(contact.mailingAddress).setValue(true);
The source for the contact’s photo is the value of contact.pic:
Ext.getDom(‘pic’).src = contact.pic;
And finally, the title of the form:
contactForm.setTitle(contact.firstName + ‘ ‘ + contact.lastName);
There’s more…
XML is powerful and popular, and it is likely that you will encounter situations where
it’s appropriate to create forms that are able to read XML data. As you saw, this can be
accomplished by using an XmlReader through the reader configuration option, instead of
using the form’s built-in support for processing JSON.
See also…
- The previous recipe, Loading form data from the server, explains how to populate a
form with data sent from the server