1. Computing

Discuss in my forum

XML Tutorial - Adding Data Strings and Comments

Understanding the Basic Rules of XML

By

So far, Understanding the Basic Rules of XML has discussed a well-formed XML document, root elements and child elements, so you should have a pretty clear idea of what structured XML looks like. The problem is that the XML file is not displaying anything of value. It is not communicating with anyone visiting your website. Now its time to do some talking using the rules of XML to guide you.

XML Rule #4 – Data Strings Communicate with the Viewer

Even with the child element added, the document still does not actually do anything. To make it display on a page, we need to add a data string. XML is a container for data. That is the purpose of the language. A Data string is what the viewer sees when they visit the web page.

Position the data string after the opening child element tag and before closing that tag.

‹?xml version= “1.0”›
‹message›
    ‹Dick›
        Hi there Dick, this is a message. – data string
    ‹/Dick›
    ‹Jane›
        Hi Jane, here is your message. – data string
    ‹/Jane›
‹/message›

Data strings can be simple or complex depending on the processes involved. For now, creating a single message shows you how to display information on a web page with XML.

One fundamental distinction to understand is the difference between a data string and a comment. If you are an experienced programmer, you know the value of the comment. When writing code, the comment reminds you why you did something or defines a section. A comment is a note that the computer will not read. In XML, comments begin with the characters:

‹!-- beginning a comment

When the browser sees ‹!--, it knows to ignore the data. The ‘--›’ signifies the end of the comment.

‹?xml version= “1.0”›
‹message›
‹!-- Creating a note to Dick --›  - comment
    ‹Dick›  
        Hi there Dick, this is the data I am sending you.
    ‹/Dick›
‹!—Here is one for Jane too--›  - comment
    ‹Jane›
        Hi Jane, I am sending you different data.
    ‹/Jane›
‹/message›

Once you have the basics down, such as the elements and data strings, a staunch XML programmer will want to add some attributes to the code. We will do that in the next article. To review the Rules of XML covered so far, see the first two articles in this series:XML Tutorial - The Declaration and Root Element and XML Tutorial - Adding Child Elements

  1. About.com
  2. Computing
  3. Web Design / HTML
  4. XML
  5. Beginning XML
  6. Rules of XML - XML Tutorial - Adding Data Strings and Comments

©2014 About.com. All rights reserved.