1. Computing

Discuss in my forum

What are XML Elements and How Do You Create Them

Basics of Elements in XML

From

One advantage XML has over other markup languages is the development of elements. Elements are key structures in XML, but flexible. In other language formats, you must use tags from a list available. For example, when writing in HTML, you use tags such as <div> to create a style. This works well for formatting, but can be cumbersome. You have to know the syntax of the tag and where it goes within your code in order to develop valid pages. XML works differently. You create element names that fit into the shape of your data. XML gives you an open playing field that is rarely seen in website design.

What are Elements?

Simply put, in XML, elements are names that you apply to sections of information. If you are creating a catalog of your favorite music, write an element tag for jazz and another for hip hop. Under each section, divide the data further by listing artists and CD titles. The XML parser worries more about the structure than it does the actual names of the elements. In other markup languages, such as HTML, it is all in the name. You must use the correct name in order for the parser to interpret. XML simplifies the process by focusing on the structure.

What Type of Elements Do You Create?

XML cares more about the setup of the page then the element names, but you should follow some basic organizational rules when creating them. The first tag is the root element. A root element holds all the other tags inside, like a folder. Next is the parent element and under the parent tags comes child elements. XML is adaptable, so not all parent elements must have child elements, some might and some might not. It is up to you whether child elements are necessary.

How to You Decide on Element Names?

Organization is the key. You want to create elements that make sense structurally. Your root element is the king of the hill and the name should reflect that fact. Create element tags that work based on the subject matter, for instance, designing an XML list for music makes less sense if you use the root element ‹books›. Apply some common sense to creating your elements, so when you do updates and write the formatting file, the elements fall into place naturally.

When learning XML, you should consider element formation a straightforward process, but keep two things in mind:

Always close an element tag. The XML parser will give you an unfriendly reminder if you forget. The element <jazz> must have a partner somewhere in the XML code, </jazz>.

Stay organized when selecting element names. Define tags in a way that makes sense to you. You can reuse tag names as long as you use appropriate form.

Root element
Parent element
Child element

Two parent elements for a music catalog can both have child tags named ‹artist›. In fact, parent names can also be identical.

Catalog – root element
Item – parent element
Item – parent element

As long as you use closing tags, the XML processor will understand.

Flexibility in designing elements is one of the high points to using XML. You can build your XML world while making the most of your time and imagination.

©2013 About.com. All rights reserved.