|
We have learned to create an element by specifying a
start tag and an optional end tag. We also know that, to specify the value
of an element, we can type it outside the start tag of the element. If we
want to add a child node, we can nest a complete element with a start tag,
an optional name and an optional value.
|
An XML attribute is a technique to create a start tag
that includes the child node(s) of the tag in the start tag. This means
that the child nodes are not created with their own tag: they are embedded
in the start tag of the element.
To create an XML attribute, inside the start tag,
after the name of the element, type the name of each attribute and assign
the desired value to it, using the assignment operator "=". The name of
the attribute follows the same rules of the names of elements. The value
of the attribute must be in double-quotes. Here are two examples of
elements each with an attribute:
<Employee FirstName="Frank"></Employee>
<Employee FirstName="Justine" />
Of course, an element can still have its own value, in
which case the value is typed outside the start tag and the element must
have an end tag. Here is an example:
<Employee FirstName="Frank">Is about to lead a new division.</Employee>
An Element With Many Attributes
|
|
The start tag of an element can contain many
attributes. Each attribute is created with a value assigned to its name.
Here are examples:
<?xml version="1.0" encoding=”UTF-8”?>"
<Employees>
<Employee FirstName="Frank" LastName="Misma" Title="General Manager" DateHired="10/04/2008"></Employee>
<Employee DateHired="05/30/2012" LastName="Grand" FirstName="Justine" Title="Sales Associates" />
<Contractor FullName="Arnold Alley" Job="Webmaster">
<HourlySalary>25.50</HourlySalary>
</Contractor>
</Employees>
To make the attributes easy to read, you can write
each on its own line and indent them on the same level. Here are examples:
<?xml version="1.0" encoding=”UTF-8”?>"
<Employees>
<Employee FirstName="Frank"
LastName="Misma"
Title="General Manager"
DateHired="10/04/2008">
</Employee>
<Employee DateHired="05/30/2012"
LastName="Grand"
FirstName="Justine"
Title="Sales Associates" />
<Contractor FullName="Arnold Alley"
Job="Webmaster">
<HourlySalary>25.50</HourlySalary>
</Contractor>
</Employees>
Of course, you can create attributes using VBA code.
Here are examples:
Private Sub cmdXMLElement_Click()
On Error GoTo cmdElement_Error
Open "Employees5.xml" For Output As #1
Print #1, "<?xml version="1.0" encoding=”UTF-8”?>"
Print #1, "<Employees>"
Print #1, " <Employee FirstName='Frank' LastName='Misma' Title='General Manager'>"
Print #1, " <DateHired>10/04/2008</DateHired>"
Print #1, " </Employee>"
Print #1, " <Employee DateHired='05/30/2012' LastName='Grand' FirstName='Justine' Title='Sales Associates' />"
Print #1, " <Contractor FullName='Arnold Alley'>"
Print #1, " <Job>Webmaster</Job>"
Print #1, " <HourlySalary>25.50</HourlySalary>"
Print #1, " </Contractor>"
Print #1, "</Employees>"
Close #1
Exit Sub
cmdElement_Error:
MsgBox "There was a problem when trying to create the XML file.", _
vbOKOnly Or vbInformation, _
"Extensible Markup Language"
Resume Next
End Sub
Notice that the first and the second
elements share some attributes but also notice how "messy" they are. For
example, in the first element, the FirstName attribute comes
before the LastName, which is reversed on the second element.
Also notice that the first element contains a child element
named DateHired, but DateHired is created as an attribute in the second
element.
On one hand, you are free to create your child
elements and attributes any way you want. On the other hand, you should be
better organized. For example, on elements that are on the same level, if
they have the same child element, either create it as a child element or
as an attribute. This is not a rule but a suggestion. The other suggestion
you should apply to attributes is their order. Again, this is not a rule
but an attribute shared by elements on the same level should be in the
same positions. For example, if the first and the second elements have
many attributes and one of those attributes is named FirstName, if you
decide to use that attribute as the first, use it like that on all
elements that have that attribute. The reason is that if you must right
code that looks for an attribute and you try to locate the attribute by
position, your code would process better if the attributes on the same
positions have the same name.
So far, we have learned how to create elements, that
is, how to create:
- An element that contains a start tag, a value and an end tag
- An empty element that consists of only a start tag
- An element that contains one or more child elements
- An element that contains one or more attributes
That's all you need to know to create a
fully-functional XML database. The features we will review now are
additional options that can be helpful, but are not required.
Introduction to Features of XML
|
|
A comment is a section that the parser must ignore in
the XML document. To create a commented line, type <!--, followed by the
comment you want, and end it with --> Here are examples:
Private Sub cmdComment_Click()
On Error GoTo cmdElement_Error
Open "Employees5.xml" For Output As #1
Print #1, "<?xml version=""1.0"" encoding=""UTF-8""?>"
Print #1, "<Employees>"
Print #1, " <Employee>"
Print #1, " <!-- This is the employee who started the company. -->"
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, " <!-- This is an approximate date. -->"
Print #1, " <!-- No one remembers when this employee started. -->"
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
cmdElement_Error:
MsgBox "There was a problem when trying to create the XML file.", _
vbOKOnly Or vbInformation, _
"Extensible Markup Language"
Resume Next
End Sub
Characters in Data: CDATA
|
|
CDATA is XML data included in an XML document but the
parser must ignore its section when processing the content of the
document. To create a CDATA section, start it with <![CDATA[ and end with
]]>. Any XML code between those two sections will not be processed by the
parser, but that XML code should still be well-formed. Here is an example:
Private Sub cmdCDATASection_Click()
On Error GoTo cmdCDATASection_Error
Open "Employees6.xml" For Output As #1
Print #1, "<?xml version=""1.0"" encoding=""UTF-8""?>"
Print #1, "<Employees>"
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
This would produce:
<?xml version="1.0" encoding="UTF-8" ?>
<Employees>
<Employee>
<FirstName>Frank</FirstName>
<LastName>Misma</LastName>
<![CDATA[ <FullName>LastName, FirstName</FullName>]]>
<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</FullName>
<Job>Webmaster</Job>
</Contractor>
</Employees>
While the above CDATA section was created on only one
line, you can create a CDATA section that covers as many lines as you
want. There are two rules to follow:
- The CDATA section must be included somewhere within the root's
start tag and end tag
- Make sure you start the section with <![CDATA[
and end it with ]]>
Here is an example:
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
In a computer program, a file (such as a Visual Basic
module) that holds code can contain declared variables, functions (or
procedures), class, etc.
Private IsReady As Boolean
Public Sub CreateListing()
End Sub
Public Function Sell() As Currency
End Function
Class House
Public Price As Double
Public Sub Build()
End Sub
End Class
You can then use the variables, functions, and classes
in another part of the project. For example, you can access a variable
based on its name. You can call a function using its name. You can declare
a variable of the class:
Dim PropertyOnLockwoodDrive As House
Dim SaleResult As Currency
SaleResult = Sell()
The name of each item in the file must be unique (two
items in the module cannot have the same name). If two items have the same
name, there would be a name conflict that would lead to an error:
Private IsReady As Boolean
Private IsReady As Boolean ' Error: A variable named IsReady exists already
Public Sub CreateListing()
End Sub
Public Function Sell() As Currency
End Function
Class House
Public Price As Double
Public Sub Build()
End Sub
End Class
Class House ' Error: A class named House exists already
End Class
If you need to use the same names for different items,
one solution is to create different sections in a file, give a name to
each section, and create items inside the desired section and/or functions
that must be isolaled. A namespace is a group of isolated items. The items
in the group are distinguished by their belonging to their unique group.
In most computer languages like Visual Basic (the
modern version), C#, or C++, a namespace is primarily created as a section
of code in a document and the namespace is given a name. The area of the
namespace is delimited one way or another:
Namespace AuroraBuilders
Private IsReady As Boolean
Public Sub CreateListing()
End Sub
Public Function Sell() As Currency
End Function
Class House
Public Price As Double
Public Sub Build()
End Sub
End Class
End Namespace
' Blah Blah Blah
Namespace JoshuaConstruction
Private IsReady As Boolean ' No Error: The variable named IsReady is unique in this namespace
Class House ' No Error: There is no other class named House in this namespace
End Class
Class Car
End Class
End Namespace
Each computer language has its own rules as to how to
create a namespace. In C# and C++, you start with the namespace
keyword followed by a name. Then, the area of the namespace is delimited
with { and }. In Visual Basic, the area of the namespace starts with the
keyword Namespace followed by a name and ends with a line marked
End Namespace. In some other programming languages (such as Java that
uses the word Package), a namespace is created in its own file such as a
web document.
When an item belongs to a namespace, to access that
item outside its namespace, you must qualify its name. That is, you must
indicate to what namespace the item belongs. The way you do this depends
in the language you are using. In some languages, you must first "import"
the namespace. In C#, this is done by typing using, followed by the
name of the namespace, and a semi-colon. In C++, you type using
namespace, followed by the name of the namespace, and a semi-colon. In
Visual Basic, you type Imports and in Java (remember that Java
doesn't have namespaces per se, Java has packages) you use import,
followed by the name of the namespace (or package), and a semi-colon.
There are many other issues to take into consideration but these would be
enough for now. Here is an example of how names of objects can be
qualified from their namespaces:
Dim PropertyOnLockwoodDrive As AuroraBuilders.House
Dim BeltsvilleCondo As JoshuaConstruction.House
AuroraBuilders.CreateListing()
Dim SaleResult As Currency
SaleResult = AuroraBuilders.Sell()
Dim UsedVehicle As JoshuaConstruction.Car
As mentioned already, instead of the period, some
other languages use other means to access the member of a namespace. For
example, C++ uses the double-colon :: while another language may use only
one colon :. Also, in most languages, if you have already imported a
namespace, if you use a name that belongs to only one namespace, in other
words there may not be name conflict, you may not have to qualify the name
of an object:
Imports AuroraBuilders
Imports JoshuaConstruction
Dim PropertyOnLockwoodDrive As AuroraBuilders.House
CreateListing()
Dim SaleResult As Currency
SaleResult = Sell()
Dim BeltsvilleCondo As JoshuaConstruction.House
Dim UsedVehicle As Car
An XML namespace is a Web document (as a Web page)
that specifies the document where the elements and attributes (almost like
classes and functions of our example) of an XML document are defined. This
means that you must first create an XML namespace document in a Web
document, save it as a Web file (which usually has the extension .htm, or
.html, or else), upload it to a web server, and get or know its Web
address, named a URI. An example of such an address would be
http://www.functionx.com/Houses. Then, in the XML document that references
the namespace, each element that uses, or belongs to, that namespace must
be qualified. This is done by writing the name of the namespace, followed
by a colon ":" (in C# and Visual Basic, the period is used; in C++, the
double-colon :: is used), and followed by the name of the element or the
name of the attibute. You must also give the address, called URI, of the
Web page. To do this, type xmlns, followed by a colon, followed by
the name of the element or attribute to which you would assign the address
as a string. The formula to follow is:
<Namespace:Element/Attribute xmlns:Namespace="URI">
After the traditional < symbol, you start with the
name of the namespace followed by a colon and the name of your local
element or attribute. This is followed by a space, xmlns:, and the
name of the namespace assigned the address the document where the
namespace is designed.
In most cases, you will hardly, if ever, create your
own namespaces. Instead, you will use the namespaces already defined by
W3C or namespaces created by companies such as Microsoft, etc.