Online Hibernate Training just in Rs. 1000

One week online training in Hibernate ORM framework. Learn Hibernate Framework and enhance your skills. Best Online Hibernate training at 90% discount. Join Now!! Offer is valid Only for limited Period!!! Online Training

XML Attributes

XML elements can have attributes in the start tag, just like HTML. Attributes are used to provide additional information about elements.

Ads

Tutorials   
JAXB Create XML File And Get Data From XML XML,XML Tutorials,Online XML Tutorial,XML Help Tutorials The Simple API for XML (SAX) APIs Part I XML,XML Tutorials,Online XML Tutorial,XML Help Tutorials XML Tutorial Ignoring Comments While Parsing an XML File Accessing XML file from Java Preventing Expansion of Entity References Getting next Tag in the XML File Replacing a Node with a New One Reading XML Data from a Stream Reading an XML document using JDOM Query XML with an XPath expression Normalize All of the Text in a Document Modifying Text by Replacement Insert a Processing Instruction and a Comment Node Getting text values from a NodeList Finding an ID Value and Print the Element Text Edit Text by Insertion and Replacement Duplicating a Subtree Deleting Two Attributes Delete the First Child of the Root Node Creating XML Tree Creating a DocumentFragment Subtree and Appending it to the Document Creates element node, attribute node, comment node, processing instruction and a CDATA section Creates a New DOM Parse Tree Replacing a Text Node with a New CDATA Section Node Create an Empty DOM Document Adding an Attribute in DOM Document Transforming XML with XSLT Transforming an XML File with XSL Transforming XML with SAXFilters Storing properties in XML file Testing EntityReferences in Xml Splitting One Text Node into Three Reading XML from a File Modifying Text by Cutting and Pasting Locating a Node by Using Siblings Locate a Node and Change Its Content Loading properties from a XML file Listing nodes used in a document Inserting a New Entry in a List Creating an InlineStreamFilter Ignoring Comments While Parsing an XML File Handling Errors While Parsing an XML File Emitting DOCTYPE Declaration while writing XML File Converting CDATA Nodes into Text Nodes Java DOM Tutorial Creating Blank DOM Document
Ads

XML Attributes

     

XML elements can have attributes in the start tag, just like HTML. Attributes are used to provide additional information about elements. Attributes often provide information that is not a part of the data. In the example below, the file type is irrelevant to the data, but important to the software that wants to manipulate the element:

<file type="gif">roseindia.gif</file>

Use the quote styles: "red" or 'red'

Attribute values must always be enclosed in quotes. Use either single or double quotes  eg..

<color="red">

or like this:

<color='red'>

Note: If the attribute value itself contains double quotes it is necessary to use single quotes, like in this example: 

<name='Rose "India" Net'>

Note: If the attribute value itself contains single quotes it is necessary to use double quotes, like in this example: 

<name="Rose 'India' Net">

Use of Elements vs. Attributes

If you start using attributes as containers for XML data, you might end up with the documents that are both difficult to maintain and  manipulate. So the user should use elements to describe the data. Use attributes only to provide data that is not relevant to the reader. Only metadata (data about data) should be stored as attributes, and that data itself should be stored as elements.

This is not the way to use attributes eg..

<?xml version="1.0" encoding="ISO-8859-1"?>
<E-mail To="Rohan" From="Amit" Subject="Surprise...."
<Body>Be ready for a cruise...i will catch u  tonight</Body>
</E-mail>

Try to avoid using attributes in few of the situations.

Lot of  problems occur with using attributes values. They are not easily expandable and cannot contain multiple values .They are not easy to test against a Document Type Definition and are also unable to describe their structure. Becomes more  irritating ,because of its difficultly to get manipulated by program code.

Here is an example, demonstrating how elements can be used instead of attributes. The following three XML documents contain exactly the same information. A date attribute is used in the first, a date element is used in the second, and an expanded date element is used in the third:

<?xml version="1.0" encoding="ISO-8859-1"?>
<E-mail date="15/05/07">
<To>Rohan</To>
<From>Amit</From>
<Subject>Surprise....</Subject>
<Body>Be ready for a cruise...i will catch u  tonight</Body>
</E-mail>

First  xml document contains date as a attribute which can not  be further extended. But date used a element in second document makes it more flexible.

<?xml version="1.0" encoding="ISO-8859-1"?>
<E-mail >
<date="15/05/07">
<To>Rohan</To>
<From>Amit</From>
<Subject>Surprise....</Subject>
<Body>Be ready for a cruise...i will catch u tonight</Body>
</E-mail>

Second xml document can be further extended as.. 

<?xml version="1.0" encoding="ISO-8859-1"?>
<E-mail >
<date> 
  <day>12</day> 
  <month>11</month> 
  <year>99</year> 
</date>

<To>Rohan</To>
<From>Amit</From>
<Subject>Surprise....</Subject>
<Body>Be ready for a cruise...i will catch u tonight</Body>
</E-mail>

     

Advertisements

Share on Google+Share on Google+

XML Attributes

Posted on: May 24, 2007 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

Ads
 

Discuss: XML Attributes   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:1
Swati
April 15, 2013
DTD formation of date?

Well Explained. Thanks. Also, can you plz describe how to form DTD for the above mentioned example including Date in it. Re-writing the example: <E-mail > <date> <day>12</day> <month>11</month> <year>99</year> </date> <To>Rohan</To> <From>Amit</From> <Subject>Surprise....</Subject> <Body>Be ready for a cruise...i will catch u tonight</Body> </E-mail>

Ads

 

Ads