Adobe
Products
Acrobat
Creative Cloud
Creative Suite
Digital Marketing Suite
Digital Publishing Suite
Elements
Photoshop
Touch Apps
Student and Teacher Editions
More products
Solutions
Creative tools for business
Digital marketing
Digital media
Education
Financial services
Government
Web Experience Management
More solutions
Learning Help Downloads Company
Buy
Home use for personal and home office
Education for students, educators, and staff
Business for small and medium businesses
Licensing programs for businesses, schools, and government
Special offers
Search
 
Info Sign in
Welcome,
My cart
My orders My Adobe
My Adobe
My orders
My information
My preferences
My products and services
Sign out
Why sign in? Sign in to manage your account and access trial downloads, product extensions, community areas, and more.
Adobe
Products Sections Buy   Search  
Solutions Company
Help Learning
Sign in Sign out My orders My Adobe
Preorder Estimated Availability Date. Your credit card will not be charged until the product is shipped. Estimated availability date is subject to change. Preorder Estimated Availability Date. Your credit card will not be charged until the product is ready to download. Estimated availability date is subject to change.
Qty:
Purchase requires verification of academic eligibility
Subtotal
Review and Checkout
Adobe Developer Connection / Adobe AIR Developer Center / AIR Quick Starts for HTML/JavaScript developers /

Reading from and writing to an XML preferences file

by Jeff Swartz

Jeff Swartz  Adobe

Modified

23 February 2009

Page tools

Share on Facebook
Share on Twitter
Share on LinkedIn
Bookmark
Print
Adobe AIR

Requirements

Prerequisite knowledge

General experience building HTML-based applications is suggested. For more details on getting started with this Quick Start, refer to Building the Quick Start sample applications with HTML.

 

Additional Requirements

  • Adobe AIR SDK

User level

Intermediate

Required products

  • Adobe AIR

Sample files

  • PrefsXMLDemoHTML.zip
  • PrefsXMLDemoHTML.air

The sample application reads and writes data related to the position and size of the application, as well as the date saved. It also positions the window according to that data when the application opens. More specifically, this sample application demonstrates the following Adobe AIR features:

  • Reading and writing to a text file
  • Specifying a file in the Adobe AIR application storage directory
  • Modifying the position, size, and visibility of an AIR application window
  • Responding to the closing event dispatched by a Window object
XML Preferences application
Figure 1. This sample application enables you to specify XML preferences.

Note: This is a sample application provided, as is, for instructional purposes.

This sample application includes the following files:

  • index.html: The main application source code.
  • AIRAliases.js: The AIR JavaScript aliases file
  • application.xml: The AIR application descriptor file
  • Sample AIR icon files

Testing the application

The application reads and writes data related to the position and size of the application, as well as the date saved. It also positions the window according to that data when the application opens. The preferences are stored in a text file contains XML data. The application converts that data to an XML object, upon reading, and converts the XML data to text upon writing. To test this application, follow these steps:

  1. Open the application.
  2. Resize and reposition the window.
  3. Quit the application.
  4. Restart the application to see your preferences take effect.

Understanding the code

This section does not describe all of the HTML mark-up for the file—only the AIR specific JavaScript code.

Reading data from the XML file

The init() method initializes the prefsFile File object to point to a predefined path and then call the readXML() method, which reads the data:

prefsFile = air.File.applicationStorageDirectory; prefsFile = prefsFile.resolvePath("preferences.xml"); readXML();

File.applicationStorageDirectory points to the AIR application store directory, which is uniquely defined for each AIR application.

The init() method also sets up an event handler to respond to the window closing (which saves the preferences data to the file):

window.nativeWindow.addEventListener(Event.CLOSING, windowClosingHandler);

The readXML() method sets up a File object and a FileStream object. The fileMode parameter of the call to the open() method is set to FileMode.READ, so that the FileStream object can read data from the file (if it exists).

stream = new air.FileStream(); if (prefsFile.exists) { stream.open(prefsFile, air.FileMode.READ); processXMLData(); }

The open() method of the stream object opens the file synchronously and begins reading data into the read buffer.

The processXMLData() method processes the XML data and closes the file. The bytesAvailable property of the FileStream object is the number of bytes in the read buffer, which is all of the bytes from the file (since the file is read synchronously):

prefsXML = stream.readUTFBytes(stream.bytesAvailable); stream.close();

Repositioning and resizing the application window

The processXMLData() function uses JavaScript DOM API to processes the XML data and closes the file. The bytesAvailable property of the FileStream object is the number of bytes in the read buffer, which is all of the bytes from the file (since the file is read synchronously):

var domParser = new DOMParser(); prefsXML = domParser.parseFromString(prefsXML, "text/xml"); var windowState = prefsXML.getElementsByTagName("windowState")[0]; window.moveTo(windowState.getAttribute("x"), windowState.getAttribute("y")); document.getElementById("xPositionField").value = windowState.getAttribute("x"); document.getElementById("yPositionField").value = windowState.getAttribute("y"); window.resizeTo(windowState.getAttribute("width"), windowState.getAttribute("height")); document.getElementById("widthField").value = windowState.getAttribute("width"); document.getElementById("heightField").value = windowState.getAttribute("height"); document.getElementById("dateField").value = prefsXML.getElementsByTagName("saveDate")[0].firstChild.nodeValue;

In the application.xml file, which defines properties of the application, the visible property of the initialWindow property is set to false. The window is resized and repositioned before the window is made visible.

The readXML() method makes the window visible after the processXMLData() method returns:

window.nativeWindow.visible = true;

Writing XML data to the file

The createXMLData() function converts the XML data to a string, adds the XML declaration to the beginning of the string, and replaces line ending characters with the platform-specific line ending character (represented by the File.lineEnding constant):

var cr = air.File.lineEnding; prefsXML = "<?xml version='1.0' encoding='utf-8'?>" + cr + "<preferences>" + cr + " <windowState" + cr + " width = '" + window.outerWidth.toString() + "'" + cr + " height = '" + window.outerHeight.toString() + "'" + cr + " x = '" + window.screenLeft.toString() + "'" + cr + " y = '" + window.screenTop.toString() + "'" + "/>" + cr + " <saveDate>" + new Date().toString() + "</saveDate>" + cr + "</preferences>"; document.getElementById("widthField").value = window.outerWidth.toString(); document.getElementById("heightField").value = window.outerWidth.toString(); document.getElementById("xPositionField").value = window.screenLeft.toString(); document.getElementById("yPositionField").value = window.screenTop.toString();

The writeXMLData() function sets up and uses a FileStream object for writing the data. Note that FileMode.WRITE is specified as the fileMode parameter in the open() method. This specifies that the FileStream object is able to write to the file (and truncates any existing data before writing):

stream = new air.FileStream(); stream.open(prefsFile, air.FileMode.WRITE);

Next, the writeUTFBytes() method is called, which writes the string version of the XML data to the file (as UTF-8 data):

stream.writeUTFBytes(prefsXML); stream.close();

When the user closes the window, the NativeWindow object dispatches a closing event. The event handler for this event cancels the default behavior of the event (which is to close the application, and calls the saveData() method, which saves the XML data. After the data is saved, the code closes the application by calling the NativeApplication.nativeApplication.exit() method:

function windowClosingHandler(event) { event.preventDefault(); nativeWindow.removeEventListener("closing", windowClosingHandler) saveData(); air.NativeApplication.nativeApplication.exit(); }

Products

  • Acrobat
  • Creative Cloud
  • Creative Suite
  • Digital Marketing Suite
  • Digital Publishing Suite
  • Elements
  • Mobile Apps
  • Photoshop
  • Touch Apps
  • Student and Teacher Editions

Solutions

  • Digital marketing
  • Digital media
  • Web Experience Management

Industries

  • Education
  • Financial services
  • Government

Help

  • Product help centers
  • Orders and returns
  • Downloading and installing
  • My Adobe

Learning

  • Adobe Developer Connection
  • Adobe TV
  • Training and certification
  • Forums
  • Design Center

Ways to buy

  • For personal and home office
  • For students, educators, and staff
  • For small and medium businesses
  • For businesses, schools, and government
  • Special offers

Downloads

  • Adobe Reader
  • Adobe Flash Player
  • Adobe AIR
  • Adobe Shockwave Player

Company

  • News room
  • Partner programs
  • Corporate social responsibility
  • Career opportunities
  • Investor Relations
  • Events
  • Legal
  • Security
  • Contact Adobe
Choose your region United States (Change)
Choose your region Close

North America

Europe, Middle East and Africa

Asia Pacific

  • Canada - English
  • Canada - Français
  • Latinoamérica
  • México
  • United States

South America

  • Brasil
  • Africa - English
  • Österreich - Deutsch
  • Belgium - English
  • Belgique - Français
  • België - Nederlands
  • България
  • Hrvatska
  • Česká republika
  • Danmark
  • Eastern Europe - English
  • Eesti
  • Suomi
  • France
  • Deutschland
  • Magyarország
  • Ireland
  • Israel - English
  • ישראל - עברית
  • Italia
  • Latvija
  • Lietuva
  • Luxembourg - Deutsch
  • Luxembourg - English
  • Luxembourg - Français
  • الشرق الأوسط وشمال أفريقيا - اللغة العربية
  • Middle East and North Africa - English
  • Moyen-Orient et Afrique du Nord - Français
  • Nederland
  • Norge
  • Polska
  • Portugal
  • România
  • Россия
  • Srbija
  • Slovensko
  • Slovenija
  • España
  • Sverige
  • Schweiz - Deutsch
  • Suisse - Français
  • Svizzera - Italiano
  • Türkiye
  • Україна
  • United Kingdom
  • Australia
  • 中国
  • 中國香港特別行政區
  • Hong Kong S.A.R. of China
  • India - English
  • 日本
  • 한국
  • New Zealand
  • 台灣

Southeast Asia

  • Includes Indonesia, Malaysia, Philippines, Singapore, Thailand, and Vietnam - English

Copyright © 2012 Adobe Systems Incorporated. All rights reserved.

Terms of Use | Privacy Policy and Cookies (Updated)

Ad Choices

Reviewed by TRUSTe: site privacy statement