The name of an element is the string that represents the tag. For
example, in <Director>, the word Director is the
name of the element. An element must have at least a start-tag. All of the tags we have seen so far
were created as elements. When creating your elements, remember to follow
the
rules we defined for names.
The value of an element is the item displayed on the right side of the
start-tag. It is also called the text of the element. In the case of
<director>Jonathan Lynn</director>, the
"Jonathan Lynn" string is the value of the director element.
While the value of
one element can be a number, the value of another element can be a date. Yet
another element can use a regular string as its value. Consider the following
example:
<?xml version="1.0" encoding="utf-8"?> <videos> <video> <title>The Distinguished Gentleman</title> <director>Jonathan Lynn</director> <LengthInMinutes>112</LengthInMinutes> <format>DVD</format> <rating>R</rating> <price>14.95</price> </video> <video> <title>Her Alibi</title> <director>Bruce Beresford</director> <LengthInMinutes>94</LengthInMinutes> <format>VHS</format> <rating>PG-13</rating> <price>9.95</price> </video> </videos>
Notice that the price elements contain numbers that look
like currency values and the LengthInMinutes elements use an integer as value.
An element may not have a value but
only a name. Consider the following example:
<?xml version="1.0" encoding="utf-8"?> <videos> <video> <vitle>The Distinguished Gentleman</title> <director>Jonathan Lynn</director> </video> </videos>
In this case, the video element doesn't have a value. It is
called an empty element.
Besides the obvious types of values, you may want to display special
characters as values of elements. Consider the following example:
<?xml version="1.0" encoding="utf-8" ?>
<Employees>
<Employee>
<FullName>Sylvie <Bellie> Aronson</FullName>
<Salary>25.64</Salary>
<DepartmentID>1</DepartmentID>
</Employee>
<Employee>
<FullName>Bertrand Yamaguchi</FullName>
<Salary>16.38</Salary>
<DepartmentID>4</DepartmentID>
</Employee>
</Employees>
If you try using this XML document, for example, if you try
displaying it in a browser, you would receive an error:
The reason is that when the parser reaches the
<FullName>Sylvie <Bellie> Aronson</FullName> line, it thinks
that <Bellie> is a tag but then <Bellie> is not closed. The parser
concludes that the document is not well-formed, that there is an error. For this reason, to display a special symbol
as part of a value, you can use its character code. For example, the < (less
than) character is represented with < and the > (greater than) symbol
can be used with >. Therefore, the above code can be corrected as
follows:
<?xml version="1.0" encoding="utf-8" ?> <Employees> <Employee> <FullName>Sylvie <Bellie> Aronson</FullName> <Salary>25.64</Salary> <DepartmentID>1</DepartmentID> </Employee> <Employee> <FullName>Bertrand Yamaguchi</FullName> <Salary>16.38</Salary> <DepartmentID>4</DepartmentID> </Employee> </Employees>
This would produce:
Here is a list of other codes you can use for special
characters:
There are still other codes to include special characters
in an XML file.
As mentioned already, one node can be nested inside of
another. A nested node is called a child of the nesting node. This also implies
that a node can have as many children as necessary, making them child nodes of
the parent node. Once again, consider our videos.xml example:
<?xml version="1.0" encoding="utf-8"?> <videos> <video> <title>The Distinguished Gentleman</title> <director>Jonathan Lynn</director> <length>112 Minutes</length> <format>DVD</format> <rating>R</rating> </video> <video> <title>Her Alibi</title> <director>Bruce Beresford</director> <length>94 Mins</length> <format>DVD</format> <rating>PG-13</rating> </video> <video> <title>Chalte Chalte</title> <director>Aziz Mirza</director> <length>145 Mins</length> <format>DVD</format> <rating>N/R</rating> </video> </videos>
The title and the director nodes are
children of the video node. The video node is the parent of both the title and
the director nodes.
Not all nodes have children, obviously. For example, the
title node of our videos.xml file does not have children.
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
The children of a nesting node are also
recognized by their sequence. For our videos.xml file, the first line is called
the first child of the DOM. This would be:
<?xml version="1.0" encoding="utf-8"?>
After identifying or locating a node, the first node that immediately
follows it is referred to as its first child. In our videos.xml file, the first
child of the first video node is the <title>The Distinguished
Gentleman</title> element. The first child of the second <video> node is
<title>Her Alibi</title>.
In this example, we started our parsing on the root node of
the document. At times, you will need to consider only a particular node, such
as the first child of a node. For example, you may want to use only the first
child of the root.
Consider the following modification of the Videos.xml file:
<?xml version="1.0" encoding="utf-8" ?>
<Videos>
<Video>
<Title>The Distinguished Gentleman</Title>
<Director>Jonathan Lynn</Director>
<CastMembers>
<Actor>Eddie Murphy</Actor>
<Actor>Lane Smith</Actor>
<Actor>Sheryl Lee Ralph</Actor>
<Actor>Joe Don Baker</Actor>
<Actor>Victoria Rowell</Actor>
</CastMembers>
<Length>112 Minutes</Length>
<Format>DVD</Format>
<Rating>R</Rating>
</Video>
<Video>
<Title>Her Alibi</Title>
<Director>Bruce Beresford</Director>
<Length>94 Mins</Length>
<Format>DVD</Format>
<Rating>PG-13</Rating>
</Video>
<Video>
<Title>Chalte Chalte</Title>
<Director>Aziz Mirza</Director>
<Length>145 Mins</Length>
<Format>DVD</Format>
<Rating>N/R</Rating>
</Video>
</Videos>
As we have learned that a node or a group of nodes can be
nested inside of another node. When you get to a node, you may know or find out
that it has children. You may then want to consider only the first child.
As opposed to the first child, the child node that immediately precedes
the end-tag of the parent
node is called
the last child.
The child nodes that are nested in a parent node and
share the same level are referred to as siblings. Consider the above file:
Director, CastMembers, and Length are child
nodes of the Video node but the Actor node is not a child of the Video node.
Consequently, Director, CastMembers, and Length are siblings. Obviously, to get a sibling, you must first have a node.
|
|
|||||||
|
No comments:
Post a Comment