Home

XML-Based Applications:
College Park Auto-Parts

 
 

Using the Document Object Model (DOM)

The main purpose of this application is to take advantage of XML and the Document Object Model (DOM) as it is implemented in the .NET Framework through the XmlDocument class. As mentioned in our lessons on XML, the XmlDocument class provides the necessary methods to create an element, to locate one, and to managed. To perform some of these operations, it gets assisted by the XmlNode and its derived classes. Based on this, we will use those classes to create parts and items sold in the store.

Before creating a part, the application must have a list of cars. To start, we created a sample XML file that contained a few cars. Here, we will all the user to add new model and add them to the file. We will create new models from the New Model dialog box.

When designing the New Part form, we equipped it with a few combo boxes. When the New Part form displays, we will make sure that those combo boxes are filled with the information that is already stored in the corresponding XML files.

 

Practical Learning Practical Learning: Introducing the Document Object Model (DOM)

  1. Display the New Model form and double-click an unoccupied area of the form to access its Load event
  2. Implement the Load event as follows:
     
    System::Void NewModel_Load(System::Object *  sender, System::EventArgs *  e)
    {
    	 // We will need a reference to the XML document
    	 XmlDocument *docXML = new XmlDocument;
    
    	 // We will start with the Make combo box
    	 // Open the Cars.xml file
    	 docXML->Load(S"Cars.xml");
    	 // Get a reference to the root node
    	 XmlElement *nodRoot = docXML->DocumentElement;
    	 // Locate all nodes whose name is Make
    	 XmlNodeList *nodItems = nodRoot->GetElementsByTagName(S"Make");
    	 // Retrieve the value of each Make node and put 
    	 // that value in the Make combo box
    	 for(int i = 0; i < nodItems->Count; i++)
    this->cboMakes->Items->Add(nodItems->ItemOf[i]->Attributes->ItemOf[S"MakeName"]->InnerText);
    }
  3. Access the New Part form and change its Load event as follows:
     
    System::Void NewPart_Load(System::Object *  sender, System::EventArgs *  e)
    {
    	  // Fill the Year combo box with years from 1960 to the coming year
    	 for(int i = DateTime::Now.Year+1; i >= 1960; i--)
    		 this->cboYears->Items->Add(i.ToString());
    
    	 // We will need a reference to the XML document
    	 XmlDocument *docXML = new XmlDocument;
    
    	 // Open the Cars.xml file
    	 docXML->Load(S"Cars.xml");
    
    	 // Get a reference to the root node
    	 XmlElement *nodRoot = docXML->DocumentElement;
    	 // Locate each node whose name is Make
    	 XmlNodeList *nodItems = nodRoot->GetElementsByTagName(S"Make");
    	 // Retrieve the value of each Make node and put 
    	 // that value in the Make combo box
        for(int i = 0; i < nodItems->Count; i++)
    this->cboMakes->Items->Add(nodItems->ItemOf[i]->Attributes->ItemOf[S"MakeName"]->InnerText);
    
    	 // Open the Makes.xml file
    	 docXML->Load(S"PartCategories.xml");
    
    	 // Get a reference to the root node
    	 nodRoot = docXML->DocumentElement;
    	 // Locate each node whose name is Make
    	 nodItems = nodRoot->GetElementsByTagName(S"PartCategory");
    	 // Retrieve the value of each Make node and put 
    	 // that value in the Make combo box
        for(int i = 0; i < nodItems->Count; i++)
    		this->cboPartCategories->Items->Add(nodItems->ItemOf[i]->InnerText);
    
    	 this->cboPartCategories->Text = S"Miscellaneous";
    
    	 // We will generate a random number for the item
    	 // To start, we will use the miliseconds as a seed
    	 DateTime tmeNow = DateTime::Now;
    	 int ms = tmeNow.Millisecond;
    
    	 // Now we can generate a random number between 100000 and 999999
    	 Random *rndNumber = new Random(ms);
    
    	 // Generate three random characters
    	 Char firstCharacter  = static_cast<Char>(rndNumber->Next(65, 90));
    	 Char secondCharacter = static_cast<Char>(rndNumber->Next(65, 90));
    	 Char thirdCharacter  = static_cast<Char>(rndNumber->Next(65, 90));
    	 // Generate a random number made of 4 digits
    	 int numberPart = rndNumber->Next(1000, 9999);
    	 
    	 // Exclude the digits 1 and 0 because they create confusion
    	 if( firstCharacter == 'I' || firstCharacter == 'O' )
    		 firstCharacter = 'A';
    	 if( secondCharacter == 'I' || secondCharacter == 'O' )
    		 secondCharacter = 'A';
    	 if( thirdCharacter == 'I' || thirdCharacter == 'O' )
    		 thirdCharacter = 'A';
    	 // Generate a random number between 1 and 3
    	 int rndCombination = rndNumber->Next(1, 4);
    	 String *strPartNumber = 0;
    
    	 // Create a part number using some algorithm
    	 if( rndCombination == 1 )
    		 strPartNumber = String::Concat(firstCharacter.ToString(),
    						secondCharacter.ToString(), 
    						numberPart.ToString(),
    						thirdCharacter.ToString());
    	 else if( rndCombination == 2 )
    		 strPartNumber = String::Concat(firstCharacter.ToString(),
    						numberPart.ToString(), 
    						secondCharacter.ToString(),
    						thirdCharacter.ToString());
    	 else if( rndCombination == 3 )
    		 strPartNumber = String::Concat(numberPart.ToString(),
    						firstCharacter.ToString(),
    						secondCharacter.ToString(),
    						thirdCharacter.ToString());
    	 else
    		 strPartNumber = rndNumber->Next(100000, 999999).ToString();
    
    	 // Display the new number in the Part # text box
    	 this->txtPartNumber->Text = strPartNumber;
    
    	 // Disable the OK button to indicate that the part is not ready
    	 this->btnAddPart->Enabled = false;
    }
  4. Access the Cars.xml file. To add a comment, change it as follows:
     
    <?xml version="1.0" encoding="utf-8"?>
    <Cars>
    	<!-- Each car model is organized as follows:
    	       1. The first part identifies the make of the car.
    	           We use a MakeName attribute of the Make element
    	           to identify a make
    	       2. The second part identifies the name of the model.
    	-->
    	<Make MakeName="Acura">
    		<Model>Integra GS 1.8L L4</Model>
    		<Model>MDX 3.5L V6</Model>
    		<Model>NSX 3.0L V6</Model>
    		<Model>NSX 3.2L V6</Model>
    		<Model>TL 3.2L V6</Model>
    	</Make>
    	<Make MakeName="Audi">
    		<Model>A4 Quattro 1.8L Turbo</Model>
    		<Model>A4 Quattro 3.0L V6</Model>
    		<Model>S4 2.7L V6</Model>
    	</Make>
    	<Make MakeName="BMW">
    		<Model>325I 2.5L L6</Model>
    		<Model>325XI 2.5L L6</Model>
    		<Model>745I 4.4L V8</Model>
    		<Model>Z3 Coupe 3.0L L6</Model>
    	</Make>
    </Cars>
  5. Save the file
  6. Execute the application to test it and make sure that the top combo boxes of the New Part and the New Model forms are rightly filled
  7. Close the form and return to your programming environment
 

Previous Copyright © 2004-2007 FunctionX, Inc. Next