Microsoft Access Database Development With VBA

Viewing an XML File

   

Using an Editor to Open an XML File

An XML file is primarily a text-based document. This means that you can open it using any text editor such as Notepad in Microsoft Windows or vi on UNIX/Linux. Most programming environments and HTML editors also make it possible to open an XML file.

In fact, such applications make it possible to create and edit XML documents. While you as a database developer may open an XML document to explore its tags, a regular user would want to see a better display of the values in the document. In fact, most users are not interested in the structures of XML elements (their tags, etc). XML provides many options to aesthetically present XML to a regular user.

Previewing an XML File

Probably the easiest ways to see what an XML looks like is to use a Web browser. In most cases, if you double-click an XML file in a file utility such as Windows Explorer, this may launch the default browser and present the document. Consider the following file we saw earlier:

Private Sub cmdCDATASection_Click()
On Error GoTo cmdCDATASection_Error

    Open "Employees7.xml" For Output As #1

    Print #1, "<?xml version=""1.0"" encoding=""UTF-8""?>"
    Print #1, "<Employees>"
    Print #1, "<!-- This is the primary structure of our XML document. -->"
    Print #1, "<![CDATA["
    Print #1, "<Employees>"
    Print #1, "  <Employee>"
    Print #1, "    <FirstName></FirstName>"
    Print #1, "    <LastName></LastName>"
    Print #1, "    <DateHired></DateHired>"
    Print #1, "    <Title></Title>"
    Print #1, "  </Employee>"
    Print #1, "</Employees>"
    Print #1, "]]>"
    Print #1, "  <Employee>"
    Print #1, "    <FirstName>Frank</FirstName>"
    Print #1, "    <LastName>Misma</LastName>"
    Print #1, "    <![CDATA[<FullName>LastName, FirstName</FullName>]]>"
    Print #1, "    <DateHired>10/04/2008</DateHired>"
    Print #1, "    <Title>General Manager</Title>"
    Print #1, "  </Employee>"
    Print #1, "  <Employee>"
    Print #1, "    <FirstName>Justine</FirstName>"
    Print #1, "    <LastName>Grand</LastName>"
    Print #1, "    <DateHired>05/30/2012</DateHired>"
    Print #1, "    <Title>Sales Associates</Title>"
    Print #1, "    <HourlySalary>22.15</HourlySalary>"
    Print #1, "  </Employee>"
    Print #1, "  <Contractor>"
    Print #1, "    <FullName>Arnold Alley</FullName>"
    Print #1, "    <Job>Webmaster</Job>"
    Print #1, "  </Contractor>"
    Print #1, "</Employees>"

    Close #1
    
    Exit Sub

cmdCDATASection_Error:
    MsgBox "There was a problem when trying to create the XML file.", _
         vbOKOnly Or vbInformation, _
         "Extensible Markup Language"
    Resume Next
End Sub

After the file has been created and saved, it can be previewed in a a Web browser as follows:

CDATA Section

Using a Cascading Style Sheet

To inform a parser as to how it should present an XML file in a Web browser, you can create a style sheet. In other words, a style sheet is a document that instructs the parser about how to display the values in the XML document. In order to give those instructions, you must create the style sheet as a document and save it. Then, to specify the accompanying style sheet of an XML file, after the declarative line, type a line as follows:

<?xml-stylesheet href="File Name or Path" type=""?>

This can also be written as follows:

<?xml-stylesheet type="" href="File Name or Path"?>

A style sheet is a formatting scheme that indicates to a Web browser how it should display the values of the elements in an XML document. The style sheet is created in a separate file that has the .css extension. Inside the document, you create sections that each specifies the necessary formatting based on its name.

 
 
 

You should first be familiar with the elements in the XML document. This means that you should know the tag names and the relationships among the elements. This is because, in your style sheet, you will create a style for each desired tag and you will use the name of that style. Consider the following XML document that we saw earlier:

<?xml version="1.0" encoding="UTF-8"?>
<Employees>
  <Employee>
    <FirstName>Frank</FirstName>
    <LastName>Misma</LastName>
    <DateHired>10/04/2008</DateHired>
    <Title>General Manager</Title>
  </Employee>
  <Employee>
    <FirstName>Justine</FirstName>
    <LastName>Grand</LastName>
    <DateHired>05/30/2012</DateHired>
    <Title>Sales Associates</Title>
    <HourlySalary>22.15</HourlySalary>
  </Employee>
  <Contractor>
    <FullName>Arnold Alley</LastName>
    <Job>Webmaster</Job>
  </Contractor>
</Employees>

Based on the names of its elements (FirstName, LastName, DateHired, etc), you can create a style sheet for it as follows:

FirstName, LastName {
    font-family: Verdana, Geneva, Tahoma, sans-serif;
    font-size:   12pt;
    color:       red;
}

FullName {
    font-family: Verdana, Geneva, Tahoma, sans-serif;
    font-size:   14pt;
    color:       blue;
    background-color: orange;
}

DateHired {
    font-family: Verdana, Geneva, Tahoma, sans-serif;
    font-size:   12pt;
    color:       green;
}

Title, Job {
    font-family: Verdana, Geneva, Tahoma, sans-serif;
    font-size:   12pt;
    color:       maroon;
}

HourlySalary {
    font-family: Verdana, Geneva, Tahoma, sans-serif;
    font-size:   12pt;
    color:       orange;
}

Save the document as a file with the .css extension. To specify the style sheet, in the XML file, set the type as text/css:

<?xml-stylesheet href="File Name or Path.css" type="text/css"?>

Replace File Name or Path with either the name of the file or the complete path and file name. Here is an extension:

Private Sub cmdCascadingStyleSheet_Click()
On Error GoTo cmdCascadingStyleSheet_Error

    Open "Employees8.css" For Output As #1

    Print #1, "FirstName, LastName {"
    Print #1, "font-family: Verdana, Geneva, Tahoma, sans-serif;"
    Print #1, "font-size:   12pt;"
    Print #1, "color:       red;"
    Print #1, "}"

    Print #1, "FullName {"
    Print #1, "font-family: Verdana, Geneva, Tahoma, sans-serif;"
    Print #1, "font-size:   14pt;"
    Print #1, "color:       blue;"
    Print #1, "background-color: orange;"
    Print #1, "}"

    Print #1, "DateHired {"
    Print #1, "font-family: Verdana, Geneva, Tahoma, sans-serif;"
    Print #1, "font-size:   12pt;"
    Print #1, "color:       green;"
    Print #1, "}"

    Print #1, "Title, Job {"
    Print #1, "font-family: Verdana, Geneva, Tahoma, sans-serif;"
    Print #1, "font-size:   12pt;"
    Print #1, "color:       maroon;"
    Print #1, "}"

    Print #1, "HourlySalary {"
    Print #1, "font-family: Verdana, Geneva, Tahoma, sans-serif;"
    Print #1, "font-size:   12pt;"
    Print #1, "color:       orange;"
    Print #1, "}"

    Close #1
    
    Exit Sub

cmdCascadingStyleSheet_Error:
    MsgBox "There was a problem when trying to create the CSS file.", _
         vbOKOnly Or vbInformation, _
         "Extensible Markup Language"
    Resume Next
End Sub

Private Sub cmdUsingCascadingStyleSheet_Click()
On Error GoTo cmdUsingCascadingStyleSheet_Error

    Open "Employees8.xml" For Output As #1

    Print #1, "<?xml version=""1.0"" encoding=""UTF-8""?>"
    Print #1, "<?xml-stylesheet href=""Employees7.css"" type=""text/css""?>"
    Print #1, "<Employees>"
    Print #1, "  <Employee>"
    Print #1, "    <FirstName>Frank</FirstName>"
    Print #1, "    <LastName>Misma</LastName>"
    Print #1, "    <DateHired>10/04/2008</DateHired>"
    Print #1, "    <Title>General Manager</Title>"
    Print #1, "  </Employee>"
    Print #1, "  <Employee>"
    Print #1, "    <FirstName>Justine</FirstName>"
    Print #1, "    <LastName>Grand</LastName>"
    Print #1, "    <DateHired>05/30/2012</DateHired>"
    Print #1, "    <Title>Sales Associates</Title>"
    Print #1, "    <HourlySalary>22.15</HourlySalary>"
    Print #1, "  </Employee>"
    Print #1, "  <Contractor>"
    Print #1, "    <FullName>Arnold Alley</FullName>"
    Print #1, "    <Job>Webmaster</Job>"
    Print #1, "  </Contractor>"
    Print #1, "</Employees>"

    Close #1
    
    Exit Sub

cmdUsingCascadingStyleSheet_Error:
    MsgBox "There was a problem when trying to create the XML file.", _
         vbOKOnly Or vbInformation, _
         "Extensible Markup Language"
    Resume Next
End Sub

This would produce

Cascading Style Sheet
 
 
   
 

Previous Copyright © 2013 FunctionX, Inc. Next