Showing posts with label xml. Show all posts
Showing posts with label xml. Show all posts

Tuesday, 13 August 2013

What is an XML File?

What is an XML File?

An XML file, or document, is a file saved with ".xml" extension, in which structured data is stored. XML files store data within markup structures, similar to the HTML markup used to structure Web pages. XML is used in conjunction with many different technologies, for websites and applications running over the Internet as well as software applications running on desktop, laptop and mobile computing devices. XML is a data storage technology, with XML files acting as databases.

Uses

XML data is used in many different types of project, including websites and software applications. The nature of XML is that it can be extended to form other languages based on the same structural rules. For example, many news feeds you see on websites are structured in RSS, which is based on XML. Similarly, the XHTML variant of HTML is XML-structured. For this reason you will find XML files stored on Web servers for use within websites and applications.

Software programs also use XML. An XML document can act as a database, with programs querying, inserting and updating data in conjunction with a user interface. Many of the programming languages in use benefit from tools for processing XML data - these are often referred to XML parsing libraries. XML is prevalent in mobile technology too, with Android applications structuring data, design layouts and shape definitions in XML documents stored within an app structure.

Structure

XML files store data values in markup structures. The following example demonstrates a simple XML structure:

 
  Mary Smith
  John Jones
 
 
  Sarah Mitchell
 
 
  Margaret Cooper
  Mark Simpson
  Paul Hunter
 

This data models the departments and employees in a company. Each element, or node, in an XML document is preceded by an opening tag and followed by a closing tag, which is the same as the opening tag except for a leading slash "/" character. Elements can contain other elements inside them. The opening tag of an XML element can also include attributes, such as "name" in the department element above. Each attribute is listed using a name and a value, which is enclosed in quotation marks. Attributes provide additional information about an element.

XML is tree-structured, with a root node (company in the example) acting as parent to all other nodes in the tree. This is similar to a family tree, except that in XML, an element can have only one direct parent. In the example above, each employee node is a child of a department node and in some cases a sibling of other employee nodes. XML data values can include various data types, such as text strings, numbers and dates.

Tools

XML is a straightforward language to work with in most cases. You can create and edit XML files in a standard text editor program. There are also various XML development and editing software tools available, some free and some commercial. These XML editors are typically equipped with formatting and validating tools, and can sometimes prompt you with code excerpts as you write your markup. XML validators check XML files for markup errors. You can access such XML validation tools through software programs and websites, allowing you to make sure your XML documents are well-formed before you attempt to use the data in them.

Monday, 1 April 2013

Getting Started with SVG Animation

With HTML5, you can embed SVG images inline in your Web pages. Although the level of support for some of the SVG elements still has a long way to go, there are many advantages to using these graphics. Since you create the SVG code in XML in your pages, you can also implement animation and interaction on the contents of an SVG. SVGs are also scalable at any level, so are ideal for catering to different user environments.

In this tutorial we are going to look at basic animation within SVG elements in HTML5 pages. The results will not work in all modern browsers. Although inline SVG is in itself supported in all current versions of the major browsers, the animation elements are not. You will be able to see some of the effects in both Firefox and Chrome, but the effect in which we animate colour can only be reliably viewed in Chrome at the moment. As is always the case with HTML5 skills, this is more of a preview of what we will be able to do in the near future than a practical guide of techniques you can rely on already, so make sure you include alternatives in your live sites. You can see the demo now for an indicator of what we are working towards.

Create an HTML5 Page

Start by creating a new HTML5 page using the following outline:
<!DOCTYPE html>
<html>
<head>
</head>
<body>

</body>
</html>

We won't be using any scripts or CSS in the head section for this tutorial, however, you can use both to apply effects to the elements inside SVGs.

Create the SVG Element

In the body section of your page, add the SVG element as follows:




The remainder of our code will be placed between these opening and closing tags. The main attributes to pay attention to in the opening SVG tag are the dimensions. You can alternatively specify relative percentage values for the width and height if you have CSS dimensions set for the containing page element.

Add the Definitions

The SVG is going to contain shapes, definitions of gradients and animations. Let's start by defining a couple of gradients. The SVG is going to contain a simplistic representation of the sun rising against a blue sky background. In an SVG, you define re-usable items such as gradients in the dedicated defs section, so add it inside the SVG element first:




Inside this section, place a gradient to represent the sky background:

 
 


This is a linear gradient, with an ID so that we can apply it to our shapes when we create them. The x1, y1, x2, y2 attributes represent the start and end co-ordinates of the gradient. In this case we want the gradient to unfold vertically so the start point is the top left corner and the end point is the bottom left corner. The stop elements represent points in the gradient - in this case it is going to go from one shade of blue to another, will full opacity throughout.

Now add a radial gradient for the sun:

 
 


In this case we define the central points of the start and end circles of the gradient - it will spread from the center of the circle shape to its outer edges. The first colour stop starts 20% into the gradient, so there will be an area of solid colour at the centre.

Add the Shapes

The SVG is going to display a rectangle background and circular sun. First add the rectangle, after the closing defs tag but still inside the SVG element:




You can include a shape using a single self-closing tag, but we are going to include the animation properties inside the rectangle element, so we separate the opening and closing tags. The rectangle properties include the x and y co-ordinates for its top left corder and the width and height, which we set to cover the whole image. We set the gradient we defined in the defs section by referring to it in the fill attribute.

Now add the circle element for the sun:




The circle is defined using the co-ordinates for its central point and the length of the radius. Again, we specify one of the gradients already defined. The circle will be in the center of the image, occupying 80% of the total width (twice the radius).

Animate Movement

If you open your page now you will see a static image of the sun in front of the sky background. Now we can add some animation using the SVG animation elements, which come from SMIL animation. First, to animate the sun, making it move into the image as though rising from the horizon, add the following animate element inside the circle element:


This element specifies an animation that will move the circle element on the y axis, which we specify by referring to the cy attribute of the circle. The attribute type tells us what type of animation this is. The from and to attributes indicate the desired start and end positions for the circle. The begin attribute indicates that the animation should start immediately and the dur attribute that it should last 5 seconds. Setting the fill attribute to freeze stops the circle from going back to its original position when the animation has elapsed.

Animate Opacity

As well as moving the circle, let's fade it in at the same time. Add the following animation, also inside the circle element:


This time the animation is on a CSS property - the opacity. We want the circle to go from zero to full opacity, to last 5 seconds and to execute once.

Animate Colour

Let's also animate the sky to complement what is happening with the sun. This animation has less browser support at the time of writing, so you may need to view the page in Chrome to see it in action. Add it between the opening and closing rectangle tags:


This is an animateColor element, which animates a change in colour over 5 seconds. The animation uses the same colours we specified in the linear gradient for the sky - browsers without support for the colour animation will simply display the gradient instead.

Conclusion

That's our basic animation complete. Open your page in a supporting browser to see the result. You should see the sun shape rise up into the image while fading in, as the background changes colour from one shade of blue to a darker one. Check out the demo to see what it should look like. This is really just the beginning when it comes to SVG animation, as there are other elements for transformational animations such as rotations and translations. With any luck you'll be able to use them for more and more users as time passes, but for the moment these effects must necessarily be used with caution.

Thursday, 30 August 2012

XML Database Mapping Using XSD (XML Schema)

It is not uncommon for developers working on software or Web applications to change data storage and modelling approaches. An example of this would be deciding to switch from XML data to a relational database. The decision to shift data modelling technologies may be a result of changes in the aims or other technologies being used within a project.

Changing from XML to a relational database system is not typically a difficult task. The main activity involved is mapping the data structures from one system to the other. The data structure includes the data items and the relationships between them. Ultimately the database will be defined using SQL but the initial stage involves creating a design, an abstract model of the database determining the structures its data content will be held in.

If an XML data source is accompanied by an XML Schema Definition (XSD) determining its structure, this can be used as the primary source for mapping the data to a relational system. XML Schemas define the structures and elements an XML data source can contain, including the names and datatypes of elements, their attributes and the relationships between them.

Techniques For Mapping

There is no set procedure for mapping XML to a relational structure, although there are some common approaches. The process should be accompanied by an informed sense of the system for which the data is being modelled, with the overall purpose of this system in mind when mapping decisions are being made.

Element To Table

Often the most logical step will be to translate XML elements into relational database tables. This is not always the best approach, it does depend on the data in question. If an XML element is mapped into a database table, its columns may become the element attributes, optionally the element content or children.

Element To Column

In some cases XML elements may become columns in other database tables. This is most likely the case with simple elements, which simply contain a text string, easily translatable into a column in a system such as MySQL. If an element contains further elements or attributes, the possibility of mapping only into a column will be less likely.

Attribute To Column

Since attributes are single, simple pieces of data, it will generally make most sense to translate them into columns in the database tables corresponding to their given elements. An alternative may be in cases where there are only a set, finite number of possible attribute values, in which case this may be reflected in different tables for the elements having each attribute type.

For example, if an element called "thing" has an attribute "type" and this attribute can only be "red" or "green" it may be reflected by having two tables, one for "red things" and one for "green things". However, in most cases it will be more sensible to have a "thing" table with a "type" column in it.

Relationships

Representing relationships in a data set is one of the more challenging aspects of mapping from XML to relational database using an XSD. Relationships in XML are implemented by structure, for example with an element having child elements. In relational database systems such relationships are reflected using "keys" linking different tables.

For example, consider the following XML structure:


Mary
Jim
Tony


The corresponding XSD excerpt:








This could be reflected in a relational database using Foreign Keys. The relationship is "one to many" in that one family may be associated with multiple members, but each member will typically only be part of a single family. In this case, a relational database could contain a "family" table with a "name" column and a unique "ID" column representing the table's Primary Key. An additional "member" table could include a "name" column (containing "Mary", "Jim" or "Tony" in this case). The "member" table could use a Foreign Key by including a column called "familyID" in which the "ID" for the member's family is recorded, linking the two tables in a way that accurately represents their relationship.

Finally

Ultimately, there is no one correct way to translate XML indicated by an XSD into a relational database design. An understanding of both technologies, together with information about the project the data is being used for, will make a successful outcome far more likely.

See also:

Wednesday, 29 August 2012

Customising LCD Output in XBMC

XBMC Introduction

XBMC is a media player which was initially designed for use with the Xbox, when it was called Xbox Media Center. The player is open source, and can now run on lots of device types, such as television sets and PCs. Most of the main operating systems can run XBMC, including Windows, Mac and Linux.

People are watching TV in lots of different ways these days, and for many of us the PC is the main medium for all of our entertainment, including tuning into TV shows. You can use XBMC to watch the television on your computer, and the system has been extended into a host of derivative applications such as Boxee. XBMC can be used with enhancements such as interactive program guides, and can also tune into radio broadcasts.

How Can the XBMC LCD be Customised?

You can customise XBMC output in a number of ways, for example with regard to the LCD output when media playback is going on. To do this, you need to edit the lcd.xml file. Within this file you can indicate the details of how you want the display to be customised by altering the XML structures.

The lcd.xml file can be found in the "UserData" folder for your XBMC installation. Where to locate this file depends on which operating system your version of XBMC is running on. Specific details for each platform can be found in the XBMC online manual.

How Does XML Work?

If you're not already familiar with XML structuring, don't worry. Familiarising yourself with the basic principles involved in XML is not difficult, even if you don't have much technical experience.

The following is a trivial example of what the content of an XML file may look like:


Cake
Pie


XML is used to model data for many different types of application, and the lcd.xml file contains the settings for various aspects of playback output on the screen. You should be able to open the file in any basic text editor.

What Elements of LCD Output can be Tailored?

Numerous elements can be altered to dictate LCD output in XBMC. These include music, video and navigation among others, and each of these categories is contained within a dedicated area of the lcd.xml file. You will see "line" tags indicating each specific area of output, and these will be contained within the relevant categories.

Inside the "line" elements, you will see various "InfoLabels" which are the specific aspects of media playback to be displayed. These variables can be used to indicate information you want shown on the screen while a particular media item is playing. These can include, for example where music is concerned, title, time, artist and album.

What is the Easiest Way to Choose Settings?

By far the most straightforward way to pick what settings you want for the XBMC LCD is by referring directly to the online manual. The manual lists all of the possible variations, so you can easily experiment with them until you find the display options you want.

Because the XBMC player is open source, it is designed to make user customisation possible in a number of ways, giving you an excellent level of control.

See also:

Friday, 29 June 2012

Using XML Schemas (XSDs) to Create Database Tables

Introduction

Developers often need to change the type of data source used for Web projects. Deciding to change from XML to a Relational database system is a common task, and means you can base your Websites and applications on a system that is robust, reliable and easy to connect to using Server Side scripting.

Creating your database tables from XML content or XSD Schema Definitions is generally straightforward. In most cases the XML tree structure can be translated into a Relational database structure, with all of the data elements and relationships represented accurately.

Model

Create a model for your database structure by referring to the elements in your XML and XSD. There are many ways to model database structure, but the most common is to simply list your tables on paper. Coming up with a good design before you build your database is essential for efficiency. Look at your XML and XSD Schema, and list all of the elements that have child elements, as the element "person" does in this example:


John Smith
Postman


It is not necessarily the case that these elements will become tables in your database, but this is the starting point for you to figure that out.

Translate

Translate each element of data in your XML into either a table, or a column in a table, by listing each of them in turn. Starting with your list of elements that have child elements, create a draft list of tables.

Initially assume that the elements you listed are all going to become tables, and list their child elements (for example "name" in the "person" element) as columns. List the name of each column and decide on a data type for it, which may be indicated in your XSD as in the following example, where "occupation" is listed as being a text string:



Structure

Check the structure of your tables by making sure every item of data in your XML can be contained. Also check that no items of data have been repeated, as this will result in an unreliable, inefficient data structure. If your data contains any information that you have not listed among your tables and columns, extend your list to include it, considering whether it makes more sense as a new table, or a column within an existing table.

Columns in existing tables are for items that provide additional information about another item, rather than existing as a core item of data in themselves. In the "person" example, "name" and "occupation" would be columns in the "person" table.

Relationships

Look at your list of tables and identify any relationships between them, for example between tables that were child and parent elements in the original XML. If the "person" element was a child of the following example "country" element, "country" would have a table of its own in the database as well:


Canada

John Smith
Postman


Jane Doe
Teacher



The "country" and "person" tables have a "one to many" relationship, meaning that one "country" item can be associated with many "person" items. To reflect this relationship, you need to add a "Foreign Key" column to the list for your "person" table indicating which "country" the "person" is associated with, which you will do when you create your SQL.

Implement

Create your database tables using your chosen Relational Database Management System, either through a Web interface provided by your Web host, or by creating SQL statements according to the following syntax:

CREATE TABLE Country
( Country_ID int, Country_name varchar(50) )

Each table you create should have a column listing an ID as in the above example, and this ID will be included as a "Foreign Key" linking tables as follows:

CREATE TABLE Person
( Person_ID int, Name varchar(50),
Occupation varchar (20),
Country_ref int )

Each record in the "Person" table will include the "Country_ID" listed in the relevant "Country" record, in its "Country_ref" column, creating a relationship between the two. Enter data from your XML into your new database to make sure it can accommodate everything you need.

Notes

  • If you find data items in your XML content that do not have a logical home in your database, you may need to make additions to your database design, or to amend it in some way.
  • It is not simply the case that elements in XML should be represented as tables in a database, and that their attributes become columns, as this will not always result in the most effective design. Make sure you think carefully about the elements and attributes in your data, and optimize them to suit Relational database structures.

Links

Tuesday, 26 June 2012

Using XML to Control LCD Output in XBMC

About XBMC

XML can be used to control LCD display options within the open source media player XBMC (initially known as Xbox Media Center), which was originally used within Xbox gaming consoles. XBMC is now available as a general media player on many different types of device, including both TVs and computers, and can be run on most of the major operating systems.

XBMC can be used to watch TV on computer, as well as listen to the radio and view Electronic Program Guides (EPGs). Aside from the main version of XBMC, there are also many derivative programs including Boxee, iConsole, MediaPortal, Voddler and Plex.

Controlling LCD Output

Using XML to control the LCD output within an XBMC system requires editing a file named "LCD.xml" to reflect the settings you want enforced. Once you have the file located, editing it is not typically difficult, but you do need to work out what settings you want. The easiest way to find what XML LCD settings are possible is to refer to the list in the official XBMC Wiki Manual.

Find the LCD XML file

Locate the LCD XML file for your XBMC installation. The file should be located within your "UserData" folder. Where the "UserData" folder is depends on which operating system you are using.

If you're using Windows XP, look in "Documents and Settings," your username, "Application Data," "XBMC." For Windows 7 and Vista, look in "Users," your username, "AppData," "Roaming," "XBMC."

If you're using a Mac, look in "Users," your username, "Library," "Application Support," "XBMC," "userdata."

If you're using Linux, look in your home folder, ".xbmc," "userdata." If you can't find the file, try searching your entire system for "LCD.xml" or "lcd.xml."

Have a look around

Open the file and look at the contents in a text editor. If you are unfamiliar with the XML format you may need to acquaint yourself with the syntax. It helps if you have a text editor that can highlight XML code (such as Notepad++ or Gedit) or even an XML viewer, many of which are free to download.

XML files use a tree structure, with tags enclosing sections of data as in this example:


33
Jim


Don't worry if you are not accustomed to reading code, XML is an easy markup language to work with. Save a copy of your LCD XML file somewhere else on your computer before making any changes to it.

Find what you're looking for

Find the part of your LCD XML file containing the section you want to change. There are five main sections you can make changes to: Navigation, Music, Video, General and Xbelaunch (for displaying game and application labels). Locate the section relevant to the display property you want to change, for example the display during music playback, which is surrounded by "<music>" tags, and contains "<line>" elements.

Make Adjustments

Make adjustments to the appropriate function sections of your LCD XML file according to what settings you want the display to reflect. To do this, you need to include "InfoLabel" variables, examples of which you should see within your existing file. These labels will cause the LCD display to output certain types of information about whatever media is playing. For example, you can display the title of the current song using the following syntax within the "<music>" section:

$INFO[MusicPlayer.Title]

Browse through the list of available labels on the XBMC Online Manual and choose from these to suit your display preferences.

Additional options

Choose special labels to suit your preferred display options. As well as choosing from the standard list, you can choose additional settings such as turning the LCD display off when playback of music is ongoing:

music

Additionally, there are labels specific to the LCD output for various settings, such as displaying a progress bar for the media item that is currently playing:

LCD.ProgressBar

Notes

  • In some cases you may need to rename the LCD XML file to "lcd.xml" changing from upper to lower case for your changes to take effect.
  • Changing the LCD XML settings is really only for advanced users, so be careful and make sure you keep a copy of the file in its original state.

Links