Logo

XML Attributes

 
 

Introduction

One of XML strengths is its ability to describe data with various options, using simple or complex elements. Although an element can have a value as large as a long paragraph of text, an element can have only one value. There are cases where you would like the same element to be equipped with, or be able to provide, various values that can also be easily distinguished or separate. To provide various types of information to an XML element, you can use one or more attributes

 

Creating an Attribute

In C++, we are used to creating classes. Imagine that you want to create one for employees. Such a class would appear as follows:

// This is the main project file for VC++ application project 
// generated using an Application Wizard.

#include "stdafx.h"

#using <mscorlib.dll>

using namespace std;
using namespace System;

__value struct CEmployeeRecord
{
	String   *Username;
	String   *Password;
	Double    Salary;
	__wchar_t MaritalStatus;
};

int _tmain()
{
    	// TODO: Please replace the sample code below with your own.
	CEmployeeRecord emplRecord;

	emplRecord.Username = S"kallar";
	emplRecord.Password = S"7hd47D89";
	emplRecord.Salary   = 20.12;
	emplRecord.MaritalStatus = L'D';

	Console::WriteLine(S"Username:       {0}", emplRecord.Username);
	Console::WriteLine(S"Password:       {0}", emplRecord.Password);
	Console::WriteLine(S"Marital Status: {0}", __box(emplRecord.MaritalStatus));
	Console::WriteLine(S"Hourly  Salary: {0}", __box(emplRecord.Salary));

    	Console::WriteLine();
	return 0;
}

This would produce:

Username:       kallar
Password:       7hd47D89
Marital Status: D
Hourly  Salary: 20.12

Press any key to continue

The members of such a class are said to describe the class. When you instantiate the class (when you declare a variable of that class), you can provide value for one or more member variables. Another instance of the class can have different values.

In XML, a tag is like a variable of a C++ class, except that you don't have to create the class but you must create the tag. Inside of the start tag, you can provide one or more attributes that mimic the member variables of a class.

An attribute is created in the start tag using the formula:

<tag Attribute_Name="Value">Element_Value</tag>

Like the tag, the name of an attribute is up to you. On the right side of the attribute, type its value in double-quotes. The end tag doesn't need any information about any attribute. It is only used to close the tag. Here is an example of a tag that uses an attribute:

<salary status="Full Time">22.05</salary >

In this example, status="Full Time" is called an attribute of the salary element.

One of the good features of an attribute is that it can carry the same type of value as that of an XML tag. Therefore, using an attribute, you can omit giving a value to a tag. For example, instead of creating the following tag with its value:

<movie>Coming to America</movie>

You can use an attribute to carry the value of the tag. Here is an example:

<movie title="Coming to America"></movie>

In this case, you can still provide another or new value for the tag.

You can create more than one attribute in a tag. To do this, separate them with an empty space. Here is an example:

<movie title="Coming to America" director="John Landis" length="116 min">Nile Rodgers</movie>

If you create a tag that uniquely contains attributes without a formal value, you can omit the end tag. In this case, you can close the start tag as you would do for an empty tag. Here is an example:

<movie title="Coming to America" />

Practical Learning: Creating XML Attributes

  1. If you want to apply an example, start Microsoft Visual C++ .NET and create a new Windows Forms Application named DepartmentStore1
  2. To create a new XML file, on the main menu, click Project -> Add New Item...
  3. In the Templates section, click XML File (.xml)
  4. In the Name text box, type Inventory and press Enter
  5. Complete the file as follows:
     
    <?xml version="1.0" encoding="utf-8"?>
    <StoreItems>
      <Item ItemNumber="947783" ItemDescription="Double-faced wool coat" UnitPrice="275.25" />
      <Item ItemNumber="934687" ItemDescription="Floral Silk Tank Blouse" UnitPrice="180.00" />
      <Item ItemNumber="973947" ItemDescription="Push Up Bra" UnitPrice="50.00" />
      <Item ItemNumber="987598" ItemDescription="Chiffon Blouse" UnitPrice="265.00" />
      <Item ItemNumber="974937" ItemDescription="Bow Belt Skirtsuit" UnitPrice="245.55" />
      <Item ItemNumber="743765" ItemDescription="Cable-knit Sweater" UnitPrice="45.55" />
      <Item ItemNumber="747635" ItemDescription="Jeans with Heart Belt" UnitPrice="25.65" />
      <Item ItemNumber="765473" ItemDescription="Fashionable mini skirt" UnitPrice="34.55" />
      <Item ItemNumber="754026" ItemDescription="Double Dry Pants" UnitPrice="28.55" />
      <Item ItemNumber="730302" ItemDescription="Romantic Flower Dress" UnitPrice="24.95" />
      <Item ItemNumber="209579" ItemDescription="Cotton Polo Shirt" UnitPrice="45.75" />
      <Item ItemNumber="267583" ItemDescription="Pure Wool Cap" UnitPrice="25.00" />
      <Item ItemNumber="248937" ItemDescription="Striped Cotton Shirt" UnitPrice="65.55" />
      <Item ItemNumber="276057" ItemDescription="Two-Toned Ribbed Crewneck" UnitPrice="9.75" />
      <Item ItemNumber="267945" ItemDescription="Chestnut Italian Shoes" UnitPrice="165.75" />
      <Item ItemNumber="409579" ItemDescription="Under Collar and Placket Jacket" UnitPrice="265.15" />
      <Item ItemNumber="467583" ItemDescription="Country Coat Rugged Wear" UnitPrice="35.55" />
      <Item ItemNumber="448937" ItemDescription="Carpenter Jeans" UnitPrice="24.95" />
      <Item ItemNumber="476057" ItemDescription="Double-Cushion Tennis Shoes" UnitPrice="48.75" />
      <Item ItemNumber="467945" ItemDescription="Stitched Center-Bar Belt" UnitPrice="32.50" />					 
    </StoreItems>
  6. On the main menu, click Project -> Add New Item...
  7. To add another form, in the Templates list, click Window Form (.NET)
  8. Set the Name to Inventory
     
    Add New Item
  9. Press Enter
  10. Design the form as follows:
     
    Sandrine Hoaks Department Store - Inventory
    Control Name Text Additional Properties
    DataGrid     AutoFormat: Colorful 3
    Anchor: Top, Bottom, Left, Right
    BorderStyle: Fixed3D
    CaptionText: Store Inventory
    Button btnLoad &Load Anchor: Bottom, Left
    Button btnClose &Close Anchor: Bottom, Right
  11. From the Data section of the Toolbox, click a DataSet and click the form
  12. In the Add Dataset dialog box, click the Untyped Dataset radio button
     
  13. Click OK
  14. Double-click the Load button and implement its event as follows:
     
    private: System::Void btnLoad_Click(System::Object *  sender, System::EventArgs *  e)
    {
    	 this->dataSet1->ReadXml(S"Inventory.xml");
    
    	 this->dataGrid1->DataSource = this->dataSet1;
    	 this->dataGrid1->DataMember = S"Item";
    }
  15. Return to the Inventory form. Double-click the Close button and implement its event as follows:
     
    private: System::Void btnClose_Click(System::Object *  sender, System::EventArgs *  e)
    {
    	 Close();
    }
  16. Display the first form by clicking the Form1.h [Design] tab
  17. Add a button to it and set its properties as follows:
    Name: btnInventory
    Text:  &View Inventory
  18. Double-click the new button and implement its Click event as follows:
     
    #pragma once
    
    #include "Inventory.h"
    
    namespace DepartmentStore1
    {
    	using namespace System;
    	using namespace System::ComponentModel;
    	using namespace System::Collections;
    	using namespace System::Windows::Forms;
    	using namespace System::Data;
    	using namespace System::Drawing;
    
    	. . . No Change
    			
    private: System::Void btnInventory_Click(System::Object *  sender, System::EventArgs *  e)
    		{
    			 Inventory *frm = new Inventory;
    			 frm->ShowDialog();
    		}
    
    	};
    }
  19. Test the application
     
  20. Close the form
  21. On the main menu, click Project -> Add New Item...
  22. To add another form, in the Templates list, click Window Form (.NET)
  23. Set the Name to OrderProcessing and press Enter
  24. Design the form as follows:
     
    Control Name Text Additional Properties
    Label   Item #  
    Label   Description  
    Label   Unit Price  
    Label   Qty  
    Label   Sub Total  
    TextBox txtPartID1    
    TextBox txtDescription1    
    TextBox txtUnitPrice1 0.00 TextAlign: Right
    TextBox txtQuantity1 0 TextAlign: Right
    TextBox txtSubTotal1 0.00 TextAlign: Right
    TextBox txtPartID2    
    TextBox txtDescription2    
    TextBox txtUnitPrice2 0.00 TextAlign: Right
    TextBox txtQuantity2 0 TextAlign: Right
    TextBox txtSubTotal2 0.00 TextAlign: Right
    TextBox txtPartID3    
    TextBox txtDescription3    
    TextBox txtUnitPrice3 0.00 TextAlign: Right
    TextBox txtQuantity3 0 TextAlign: Right
    TextBox txtSubTotal3 0.00 TextAlign: Right
    TextBox txtPartID4    
    TextBox txtDescription4    
    TextBox txtUnitPrice4 0.00 TextAlign: Right
    TextBox txtQuantity4 0 TextAlign: Right
    TextBox txtSubTotal4 0.00 TextAlign: Right
    TextBox txtPartID5    
    TextBox txtDescription5    
    TextBox txtUnitPrice5 0.00 TextAlign: Right
    TextBox txtQuantity5 0 TextAlign: Right
    TextBox txtSubTotal5 0.00 TextAlign: Right
    TextBox txtPartID6    
    TextBox txtDescription6    
    TextBox txtUnitPrice6 0.00 TextAlign: Right
    TextBox txtQuantity6 0 TextAlign: Right
    TextBox txtSubTotal6 0.00 TextAlign: Right
    TextBox txtPartID7    
    TextBox txtDescription7    
    TextBox txtUnitPrice7 0.00 TextAlign: Right
    TextBox txtQuantity7 0 TextAlign: Right
    TextBox txtSubTotal7 0.00 TextAlign: Right
    TextBox txtPartID8    
    TextBox txtDescription8    
    TextBox txtUnitPrice8 0.00 TextAlign: Right
    TextBox txtQuantity8 0 TextAlign: Right
    TextBox txtSubTotal8 0.00 TextAlign: Right
    Button btnClose &Close  
    Label   &Order Total:  
    TextBox txtTotalOrder    
  25. On the form, click the first text box under Item #
  26. In the Properties window, click the Events button Events and double-click the Leave field
  27. Return to the form and generate the Leave event of each text box under Item #
  28. Implement the Leave events as follows:
     
    private: System::Void txtPartID1_Leave(System::Object *  sender, System::EventArgs *  e)
    {
    	 String *partNo = this->txtPartID1->Text;
    	 String *partID = 0;
    
    	 XmlTextReader *xtr = new XmlTextReader(S"Inventory.xml");
    
    	 while(xtr->Read())
    	 {
    		 switch(xtr->NodeType)
    		 {
    		 case XmlNodeType::Element:
    			 partID = xtr->GetAttribute(S"ItemNumber");
    
    			 if( partNo->Equals(partID) )
    			 {
    				 this->txtDecription1->Text = xtr->GetAttribute(S"ItemDescription");
    				 this->txtUnitPrice1->Text = xtr->GetAttribute(S"UnitPrice");
    				 this->txtQuantity1->Text = S"1";
    				 this->txtSubTotal1->Text = xtr->GetAttribute(S"UnitPrice");
    				 this->txtQuantity1->Focus();
    			 }
    			 break;
    		 }
    	 }
    }
    
    private: System::Void txtPartID2_Leave(System::Object *  sender, System::EventArgs *  e)
    {
    	 String *partNo = this->txtPartID2->Text;
    	 String *partID = 0;
    
    	 XmlTextReader *xtr = new XmlTextReader(S"Inventory.xml");
    
    	 while(xtr->Read())
    	 {
    		 switch(xtr->NodeType)
    		 {
    		 case XmlNodeType::Element:
    			 partID = xtr->GetAttribute(S"ItemNumber");
    			 if( partNo->Equals(partID) )
    			 {
    				 this->txtDescription2->Text = xtr->GetAttribute(S"ItemDescription");
    				 this->txtUnitPrice2->Text = xtr->GetAttribute(S"UnitPrice");
    				 this->txtQuantity2->Text = S"1";
    				 this->txtSubTotal2->Text = xtr->GetAttribute(S"UnitPrice");
    				 this->txtQuantity2->Focus();
    			 }
    			break;
    		}
    	}
    }
    
    private: System::Void txtPartID3_Leave(System::Object *  sender, System::EventArgs *  e)
    {
    	 String *partNo = this->txtPartID3->Text;
    	 String *partID = 0;
    
    	 XmlTextReader *xtr = new XmlTextReader(S"Inventory.xml");
    
    	 while(xtr->Read())
    	 {
    		 switch(xtr->NodeType)
    		 {
    		 case XmlNodeType::Element:
    			 partID = xtr->GetAttribute(S"ItemNumber");
    			 if( partNo->Equals(partID) )
    			 {
    				 this->txtDescription3->Text = xtr->GetAttribute(S"ItemDescription");
    				 this->txtUnitPrice3->Text = xtr->GetAttribute(S"UnitPrice");
    				 this->txtQuantity3->Text = S"1";
    				 this->txtSubTotal3->Text = xtr->GetAttribute(S"UnitPrice");
    				 this->txtQuantity3->Focus();
    			 }
    			break;
    		}
    	}
    }
    
    private: System::Void txtPartID4_Leave(System::Object *  sender, System::EventArgs *  e)
    {
    	 String *partNo = this->txtPartID4->Text;
    	 String *partID = 0;
    
    	 XmlTextReader *xtr = new XmlTextReader(S"Inventory.xml");
    
    	 while(xtr->Read())
    	 {
    		 switch(xtr->NodeType)
    		 {
    		 case XmlNodeType::Element:
    			 partID = xtr->GetAttribute(S"ItemNumber");
    			 if( partNo->Equals(partID) )
    			 {
    				 this->txtDescription4->Text = xtr->GetAttribute(S"ItemDescription");
    				 this->txtUnitPrice4->Text = xtr->GetAttribute(S"UnitPrice");
    				 this->txtQuantity4->Text = S"1";
    				 this->txtSubTotal4->Text = xtr->GetAttribute(S"UnitPrice");
    				 this->txtQuantity4->Focus();
    			 }
    			break;
    		}
    	}
    }
    
    private: System::Void txtPartID5_Leave(System::Object *  sender, System::EventArgs *  e)
    {
    	 String *partNo = this->txtPartID5->Text;
    	 String *partID = 0;
    
    	 XmlTextReader *xtr = new XmlTextReader(S"Inventory.xml");
    
    	 while(xtr->Read())
    	 {
    		 switch(xtr->NodeType)
    		 {
    		 case XmlNodeType::Element:
    			 partID = xtr->GetAttribute(S"ItemNumber");
    			 if( partNo->Equals(partID) )
    			 {
    				 this->txtDescription5->Text = xtr->GetAttribute(S"ItemDescription");
    				 this->txtUnitPrice5->Text = xtr->GetAttribute(S"UnitPrice");
    				 this->txtQuantity5->Text = S"1";
    				 this->txtSubTotal5->Text = xtr->GetAttribute(S"UnitPrice");
    				 this->txtQuantity5->Focus();
    			 }
    			break;
    		}
    	}
    }
    
    private: System::Void txtPartID6_Leave(System::Object *  sender, System::EventArgs *  e)
    {
    	 String *partNo = this->txtPartID6->Text;
    	 String *partID = 0;
    
    	 XmlTextReader *xtr = new XmlTextReader(S"Inventory.xml");
    
    	 while(xtr->Read())
    	 {
    		 switch(xtr->NodeType)
    		 {
    		 case XmlNodeType::Element:
    			 partID = xtr->GetAttribute(S"ItemNumber");
    			 if( partNo->Equals(partID) )
    			 {
    				 this->txtDescription6->Text = xtr->GetAttribute(S"ItemDescription");
    				 this->txtUnitPrice6->Text = xtr->GetAttribute(S"UnitPrice");
    				 this->txtQuantity6->Text = S"1";
    				 this->txtSubTotal6->Text = xtr->GetAttribute(S"UnitPrice");
    				 this->txtQuantity6->Focus();
    			 }
    			break;
    		}
    	}
    }
    
    private: System::Void txtPartID7_Leave(System::Object *  sender, System::EventArgs *  e)
    {
    	 String *partNo = this->txtPartID7->Text;
    	 String *partID = 0;
    
    	 XmlTextReader *xtr = new XmlTextReader(S"Inventory.xml");
    
    	 while(xtr->Read())
    	 {
    		 switch(xtr->NodeType)
    		 {
    		 case XmlNodeType::Element:
    			 partID = xtr->GetAttribute(S"ItemNumber");
    			 if( partNo->Equals(partID) )
    			 {
    				 this->txtDescription7->Text = xtr->GetAttribute(S"ItemDescription");
    				 this->txtUnitPrice7->Text = xtr->GetAttribute(S"UnitPrice");
    				 this->txtQuantity7->Text = S"1";
    				 this->txtSubTotal7->Text = xtr->GetAttribute(S"UnitPrice");
    				 this->txtQuantity7->Focus();
    			 }
    			break;
    		}
    	}
    }
    
    private: System::Void txtPartID8_Leave(System::Object *  sender, System::EventArgs *  e)
    {
    	 String *partNo = this->txtPartID8->Text;
    	 String *partID = 0;
    
    	 XmlTextReader *xtr = new XmlTextReader(S"Inventory.xml");
    
    	 while(xtr->Read())
    	 {
    		 switch(xtr->NodeType)
    		 {
    		 case XmlNodeType::Element:
    			 partID = xtr->GetAttribute(S"ItemNumber");
    			 if( partNo->Equals(partID) )
    			 {
    				 this->txtDescription8->Text = xtr->GetAttribute(S"ItemDescription");
    				 this->txtUnitPrice8->Text = xtr->GetAttribute(S"UnitPrice");
    				 this->txtQuantity8->Text = S"1";
    				 this->txtSubTotal8->Text = xtr->GetAttribute(S"UnitPrice");
    				 this->txtQuantity8->Focus();
    			 }
    			break;
    		}
    	}
    }
  29. In the top section of the Code Editor, under the other using namespace line add using namespace System::Xml
  30. In the Code Editor, define a method as follows:
     
    void CalculateTotal(void)
    {
    	double subTotal1 = this->txtSubTotal1->Text->ToDouble(0);
    	double subTotal2 = this->txtSubTotal2->Text->ToDouble(0);
    	double subTotal3 = this->txtSubTotal3->Text->ToDouble(0);
    	double subTotal4 = this->txtSubTotal4->Text->ToDouble(0);
    	double subTotal5 = this->txtSubTotal5->Text->ToDouble(0);
    	double subTotal6 = this->txtSubTotal6->Text->ToDouble(0);
    	double subTotal7 = this->txtSubTotal7->Text->ToDouble(0);
    	double subTotal8 = this->txtSubTotal8->Text->ToDouble(0);
    	double totalOrder = subTotal1 + subTotal2 + subTotal3 + subTotal4 +
    			subTotal5 + subTotal6 + subTotal7 + subTotal8;
    
    	this->txtTotalOrder->Text = totalOrder.ToString(S"F");
    }
  31. On the form, click the first text box under Qty
  32. In the Properties window, double-click the Leave field
  33. Return to the form and generate the Leave event of each text box under Qty
  34. Implement the Leave events as follows:
     
    private: System::Void txtQuantity1_Leave(System::Object *  sender, System::EventArgs *  e)
    		 {
    			 double unitPrice = this->txtUnitPrice1->Text->ToDouble(0);
    			 int qty = this->txtQuantity1->Text->ToInt16(0);
    			 double subTotal = unitPrice * qty;
    
    			 this->txtSubTotal1->Text = subTotal.ToString(S"F");
    			 CalculateTotal();
    		 }
    
    private: System::Void txtQuantity2_Leave(System::Object *  sender, System::EventArgs *  e)
    		 {
    			 double unitPrice = this->txtUnitPrice2->Text->ToDouble(0);
    			 int qty = this->txtQuantity2->Text->ToInt16(0);
    			 double subTotal = unitPrice * qty;
    
    			 this->txtSubTotal2->Text = subTotal.ToString(S"F");
    			 CalculateTotal();
    		 }
    
    private: System::Void txtQuantity3_Leave(System::Object *  sender, System::EventArgs *  e)
    		 {
    			 double unitPrice = this->txtUnitPrice3->Text->ToDouble(0);
    			 int qty = this->txtQuantity3->Text->ToInt16(0);
    			 double subTotal = unitPrice * qty;
    
    			 this->txtSubTotal3->Text = subTotal.ToString(S"F");
    			 CalculateTotal();
    		 }
    
    private: System::Void txtQuantity4_Leave(System::Object *  sender, System::EventArgs *  e)
    		 {
    			 double unitPrice = this->txtUnitPrice4->Text->ToDouble(0);
    			 int qty = this->txtQuantity4->Text->ToInt16(0);
    			 double subTotal = unitPrice * qty;
    
    			 this->txtSubTotal4->Text = subTotal.ToString(S"F");
    			 CalculateTotal();
    		 }
    
    private: System::Void txtQuantity5_Leave(System::Object *  sender, System::EventArgs *  e)
    		 {
    			 double unitPrice = this->txtUnitPrice5->Text->ToDouble(0);
    			 int qty = this->txtQuantity5->Text->ToInt16(0);
    			 double subTotal = unitPrice * qty;
    
    			 this->txtSubTotal5->Text = subTotal.ToString(S"F");
    			 CalculateTotal();
    		 }
    
    private: System::Void txtQuantity6_Leave(System::Object *  sender, System::EventArgs *  e)
    		 {
    			 double unitPrice = this->txtUnitPrice6->Text->ToDouble(0);
    			 int qty = this->txtQuantity6->Text->ToInt16(0);
    			 double subTotal = unitPrice * qty;
    
    			 this->txtSubTotal6->Text = subTotal.ToString(S"F");
    			 CalculateTotal();
    		 }
    
    private: System::Void txtQuantity7_Leave(System::Object *  sender, System::EventArgs *  e)
    		 {
    			 double unitPrice = this->txtUnitPrice7->Text->ToDouble(0);
    			 int qty = this->txtQuantity7->Text->ToInt16(0);
    			 double subTotal = unitPrice * qty;
    
    			 this->txtSubTotal7->Text = subTotal.ToString(S"F");
    			 CalculateTotal();
    		 }
    
    private: System::Void txtQuantity8_Leave(System::Object *  sender, System::EventArgs *  e)
    		 {
    			 double unitPrice = this->txtUnitPrice8->Text->ToDouble(0);
    			 int qty = this->txtQuantity8->Text->ToInt16(0);
    			 double subTotal = unitPrice * qty;
    
    			 this->txtSubTotal8->Text = subTotal.ToString(S"F");
    			 CalculateTotal();
    		 }
    
  35. On the form, double-click the Close button and implement its Click event as follows:
     
    private: System::Void button2_Click(System::Object *  sender, System::EventArgs *  e)
    {
    	 Close();
    }
  36. Display the first form by clicking the Form1.h [Design] tab
  37. Add a button to it and set its properties as follows:
    Name: btnOrders
    Text:  &Order Processing
  38. Double-click the new button and implement its Click event as follows:
     
    #pragma once
    
    #include "Inventory.h"
    #include "OrderProcessing.h"
    
    namespace DepartmentStore1
    {
    	. . . No Change
    
    
    	private: System::Void btnOrders_Click(System::Object *  sender, System::EventArgs *  e)
    			 {
    				 OrderProcessing *frm = new OrderProcessing;
    				 frm->ShowDialog();
    			 }
    
    	};
    }
  39. Return to the form and add a button to it
  40. Set its properties as follows:
    Name: btnClose
    Text:  &Close
     
    Department Store
  41. Double-click the new button and implement its Click event as follows:
     
    private: System::Void btnClose_Click(System::Object *  sender, System::EventArgs *  e)
    {
    	 Close();
    }
  42. Execute the application
     
  43. After using it, close the form
 

Home Copyright © 2004-2010 FunctionX, Inc.