File processing consists of:
In the same way, you can create XML to be stored in a file, or you can open an XML document to read its content and use it as you see fit.
An XML document is made of sections called elements. An XML file can have as many elements as possible. This means that they can range in the dozens, hundreds, or thousands. The operating system has the responsibility of being able to handle the file if it gets exceedingly large. To rightly play its role, an element must follow some rules. An XML element is classically made of three parts, two of which are called tags: a start tag and an end tag. A start tag is made of three parts: <, followed by a name, and ending with >. An example is <DEPARTMENT> The name must follow some rules:
As started above, an element must have a start tag and
an end tag. The end tag appears like the start tag but must include a
forward slash after the <. This also means that the end tag must have the
same name as the start tag. An example is: </DEPARTMENT> Between the start tag and the end tag, an element can have a value. An example is: <DEPARTMENT>Environmental Protection Agency</DEPARTMENT> This can be created in file processing as follows: Private Sub cmdXMLElement_Click()
On Error GoTo cmdElement_Error
Open "Departments1.xml" For Output As #1
Print #1, "<DEPARTMENT>Environmental Protection Agency</DEPARTMENT>"
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
We stated that an element could have a value. This means that some elements do not have a value. An example is: Private Sub cmdXMLElement_Click()
On Error GoTo cmdElement_Error
Open "Departments2.xml" For Output As #1
Print #1, "<DEPARTMENT></DEPARTMENT>"
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
This is called an empty element. When an element does not have a value, you can use one tag that would act as the start and end tags. In this case, omit the end tag but, between the start tag's name and >, add a forward slash. An example is: Private Sub cmdXMLElement_Click()
On Error GoTo cmdElement_Error
Open "Departments3.xml" For Output As #1
Print #1, "<DEPARTMENT/>"
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
It is usually a good idea to add an empty space before the forward slash to make it easy to read. Here is an example: <DEPARTMENT /> To resume, an XML element can be made of:
An XML file is made of elements created using regular text characters. When an application or an environment wants to use that file, it must be able to recognize the various elements. The program that must read and analyze the content of the XML document is called a parser. The values of some of the elements in an XML document may not be in regular US English, which means that they may use International characters or symbols, referred to as Unicode. Some other values may include complex or special characters. One way to assist the parser is to indicate whether the document uses only regular (US English) words or regular and Unicode characters. To give such an instruction, you must add a line in the top section of the document. Such a line is called an XML declaration. An XML declaration is not required in an XML document, especially if you know that the parsers that will use your XML file can read it without a problem. In most cases, although the XML declaration is not required, you don’t lose anything by adding it. An XML declaration:
You probably know already that, in the Visual Basic language, to include a double-quote in your code, you can double it. This is another way to apply the encoding. Other types of encodings can be used. UTF-8 and UTF-16 are just two examples.
As mentioned already, an XML document contains one or a series of elements. The elements follow one another like a bulleted list of a text document. The document is organized like a tree (an upside-down tree). For this reason, each part (or element) is referred to as a node.
An XML document can have just one element. Another XML document can have as many elements as necessary. Here is an example: <Employee>Frank Misma 10/04/2008 General Manager</Employee> <Employee>Justine Grand 05/30/2012 Sales Associates</Employee> <Employee>Arnold Alley 5/2/2010 Webmaster</Employee> If an XML document contains more than one element and those elements are on the same level (in later sections, or little by little, we will find out what “level” means), the elements must be included in another element. This is done as follows. Create a start tag. Add the necessary elements, and close with an end tag that corresponds to the earlier start tag. Here is an example: <Identification> <Employee>Frank Misma 10/04/2008 General Manager</Employee> <Employee>Justine Grand 05/30/2012 Sales Associates</Employee> <Employee>Arnold Alley 5/2/2010 Webmaster</Employee> </Identification> If an XML document contains many elements, there must be a special element that starts the document. The start tag of that element is in the beginning of the document. That element is referred to as the root of the document. In the above example, <Identification> is the root of the document. If the document contains an XML declaration, the root is created just after the declaration. Here is an example: <?xml version="1.0" encoding=”UTF-8”?> <Identification> <Employee>Frank Misma 10/04/2008 General Manager</Employee> <Employee>Justine Grand 05/30/2012 Sales Associates</Employee> <Employee>Arnold Alley 5/2/2010 Webmaster</Employee> </Identification> This can be created in a file as follows: Private Sub cmdXMLElement_Click()
On Error GoTo cmdElement_Error
Open "Departments5.xml" For Output As #1
Print #1, "<?xml version=""1.0"" encoding=""UTF-8""?>"
Print #1, "<Identification>"
Print #1, "<Employee>Frank Misma 10/04/2008 General Manager</Employee>"
Print #1, "<Employee>Justine Grand 05/30/2012 Sales Associates</Employee>"
Print #1, "<Employee>Arnold Alley 5/2/2010 Webmaster</Employee>"
Print #1, "</Identification>"
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
An element can be created inside of another element. Here is an example: <Identification>
<Employee>Frank Misma</Employee>
</Identification>
In this case, the <Employee> element is created inside the <Identification> Element. We also say that the <Employee> element is nested in the <Identification> element. As a result, the <Employee> element is considered, or referred to as, the parent of the <Identification> Element. In the same way, a parent of a node can be a parent of another node. An element can be a parent of a node that itself is a parent.
Indentation consists of aligning the elements of a document to make it easy to read. To make it possible, the elements are considered by levels. The root node is the first level. Any node created inside of it is considered in the second or next level. Subsequently, other nodes can be created inside of other nodes. Indentation consists of leaving empty spaces on the left side of an element with regards to the parent node. The typical number of empty spaces is 2. Normally, the XML declaration and the root node should be on the same level. Here is an example: <?xml version="1.0" encoding=”UTF-8”?> <Identification> <Employee>Frank Misma 10/04/2008 General Manager</Employee> <Employee>Justine Grand 05/30/2012 Sales Associates</Employee> <Employee>Arnold Alley 5/2/2010 Webmaster</Employee> </Identification> If you decide to indent by 4 characters, this is equivalent to pressing Tab. Here is an example: <?xml version="1.0" encoding=”UTF-8”?>
<Identification>
<Employee>Frank Misma 10/04/2008 General Manager</Employee>
<Employee>Justine Grand 05/30/2012 Sales Associates</Employee>
<Employee>Arnold Alley 5/2/2010 Webmaster</Employee>
</Identification>
You can use indentation in your text editor. Indentation
is not necessary if you are creating your XML file in Microsoft Access.
As mentioned already, an element can be created inside another element. This is also referred to as nesting an element. This means that the element created inside is said to be nested. The element that encloses another is referred to as the parent. The element created inside is referred to as a child element or a child node. Consider the following example: <Identification> <Employee>Frank Misma</Employee> </Identification> In this case, the <Employee> node is the child of the <Identification> node. In the same way, you can create as many child nodes to a parent as you want. Here is an example we saw already: <?xml version="1.0" encoding=”UTF-8”?> <Identification> <Employee>Frank Misma 10/04/2008 General Manager</Employee> <Employee>Justine Grand 05/30/2012 Sales Associates</Employee> <Employee>Arnold Alley 5/2/2010 Webmaster</Employee> </Identification> In this case, the parent <Identification> node has three children that are all named Employee. The child nodes of a parent can have different names even if they are on the same level. Of course, a child node can have only one parent, but a parent can have as many children as necessary. An element can be created as a child of an element that itself is a child of another element. In fact, this is a good feature to organize the elements of an XML document by illustrating their dependencies. This is also where indentation is important. Here are examples: <?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>
</Employee>
<Employee>
<FirstName>Arnold</FirstName>
<LastName>Alley</LastName>
<DateHired>5/2/2010</DateHired>
<Title>Webmaster</Title>
</Employee>
</Employees>
The document can be created in file processing as follows (indentation is used simply to make the document easy to read; indentation is not required): Private Sub cmdXMLElement_Click()
On Error GoTo cmdElement_Error
Open "Employees1.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, " <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, " </Employee>"
Print #1, " <Employee>"
Print #1, " <FirstName>Arnold</FirstName>"
Print #1, " <LastName>Alley</LastName>"
Print #1, " <DateHired>5/2/2010</DateHired>"
Print #1, " <Title>Webmaster</Title>"
Print #1, " </Employee>"
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
A child node is just another XML element. It must have a start tag. It can be empty, which means it may not have a value, in which case it may not have an end tag. Here are examples: Private Sub cmdXMLElement_Click()
On Error GoTo cmdElement_Error
Open "Employees2.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, " <DateHired>10/04/2008</DateHired>"
Print #1, " <Title>General Manager</Title>"
Print #1, " </Employee>"
Print #1, " <Employee></Employee>"
Print #1, " <Employee>"
Print #1, " <FirstName>Justine</FirstName>"
Print #1, " <MiddleName></MiddleName>"
Print #1, " <LastName>Grand</LastName>"
Print #1, " <DateHired>05/30/2012</DateHired>"
Print #1, " <Title>Sales Associates</Title>"
Print #1, " </Employee>"
Print #1, " <Employee/>"
Print #1, " <Employee>"
Print #1, " <FirstName>Arnold</FirstName>"
Print #1, " <MiddleName />"
Print #1, " <LastName>Alley</LastName>"
Print #1, " <DateHired>5/2/2010</DateHired>"
Print #1, " <Title>Webmaster</Title>"
Print #1, " <Salary></Salary>"
Print #1, " </Employee>"
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
A parent node can have its own value. To specify its value, type it just after the start tag and before its child node(s). Here are two examples: Private Sub cmdXMLElement_Click()
On Error GoTo cmdElement_Error
Open "Employees3.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, " <DateHired>10/04/2008</DateHired>"
Print #1, " <Title>General Manager</Title>"
Print #1, " </Employee>"
Print #1, " <Employee>First Sales Associate to be hired"
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, " </Employee>"
Print #1, " <Employee>For the time being, the webmaster also acts as the application developer."
Print #1, " <FirstName>Arnold</FirstName>"
Print #1, " <LastName>Alley</LastName>"
Print #1, " <DateHired>5/2/2010</DateHired>"
Print #1, " <Title>Webmaster</Title>"
Print #1, " </Employee>"
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
The elements on the same level are referred to as
siblings. Consider the following elements: Private Sub cmdXMLElement_Click()
On Error GoTo cmdElement_Error
Open "Employees4.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, " <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</LastName>"
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
In this case, the <Employee> and the <Contractor> elements are siblings. The <FirstName>, the <LastName, the <DateHired>, and the <Title> elements of the first <Employee> node are siblings. In the same way, the <FullName> and the <Job> nodes are sibling elements.
|
|
|||||||||||||||||||||||||||||
|
|