Introduction to XML Elements |
|
Elements Fundamentals |
Introduction |
Consider the following file named videos.xml: <?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 Minutes</length> <format>DVD</format> <rating>PG-13</rating> </video> <video> <title>Chalte Chalte</title> <director>Aziz Mirza</director> <length>145 Minutes</length> <format>DVD</format> <rating>N/R</rating> </video> <video> <title>Sneakers</title> <director>Phil Alden Robinson</director> <length>2 Hours 6 Minutes</length> <format>DVD</format> <rating>N/R</rating> </video> </videos> |
An element in an XML document is an object that begins with a start-tag, may contain a value, and may terminate with an end-tag. Based on this, the combination of a start-tag, the value, and the end-tag is called an element. An element can be more than that but for now, we will consider that an element is primarily characterized by a name and possibly a value. You use XML elements through the IXMLNode class. To access an XML element, you can declare a variable of type IXMLNode.
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. To support the name of a node, the IXMLNode class is equipped with a property named NodeName that can be used to identify an existing element. Here is an example of accessing it: //--------------------------------------------------------------------------- void __fastcall TfrmMain::btnDocumentClick(TObject *Sender) { TXMLDocument *docVideos = new TXMLDocument(frmMain); docVideos->LoadFromFile(L"C:\\Programs\\videos.xml"); IXMLNode *nodVideo = docVideos->DocumentElement; ShowMessage(nodVideo->NodeName); } //--------------------------------------------------------------------------- This would produce:
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. To support the text or value of an element, the IXMLNode interface is equipped with a property named Text: __property DOMString Text; 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> <rating>R</rating> <price>14.95</price> </video> <video> <title>Her Alibi</title> <director>Bruce Beresford</director> <lengthinminutes>94</lengthinminutes> <rating>PG-13</rating> <price>8.75</price> </video> <video> <title>Chalte Chalte</title> <director>Aziz Mirza</director> <lengthinminutes>145</lengthinminutes> <rating>N/R</rating> <price>29.95</price> </video> <video> <title>Sneakers</title> <director>Phil Alden Robinson</director> <lengthinminutes>126</lengthinminutes> <rating>N/R</rating> <price>14.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. To get the value of an element, you can access its Text property. Obviously not all elements have a value. For example, in the above example, the videos element does not have a value. If you try accessing the value of an element when that element does not have one, the compiler would throw an EXMLDocError exception: //--------------------------------------------------------------------------- void __fastcall TfrmMain::btnDocumentClick(TObject *Sender) { TXMLDocument *docVideos = new TXMLDocument(frmMain); docVideos->LoadFromFile(L"C:\\Programs\\videos.xml"); // Locate the root node IXMLNode *nodRoot = docVideos->DocumentElement; ShowMessage(nodRoot->Text); } //--------------------------------------------------------------------------- To assist you with checking whether an element has a value, the IXMLNode interface is equipped with a Boolean property named IsTextElement: __property Boolean IsTextElement; If the element that accesses this property has a value, it returns true. Otherwise it produces false. Here is an example of checking whether an element (the root in this case) has a value //--------------------------------------------------------------------------- void __fastcall TfrmMain::btnDocumentClick(TObject *Sender) { TXMLDocument *docVideos = new TXMLDocument(frmMain); docVideos->LoadFromFile(L"C:\\Programs\\videos.xml"); // Locate the root node IXMLNode *nodRoot = docVideos->DocumentElement; if( nodRoot->IsTextElement == True ) ShowMessage(nodRoot->Text); else ShowMessage("That element ain't got no a value"); } //--------------------------------------------------------------------------- This would produce:
An element may not have a value but only a name. Consider the following example: <?xml version="1.0" encoding="utf-8"?> <videos> <video> <title>The Distinguished Gentleman</title> <director>Jonathan Lynn</director> </video> </videos> In this case, the video element does not have a value. It is called an empty element. When a tag is empty, the Text property of its IXMLNode object would return an empty value. Now consider the following example: <?xml version="1.0" encoding="utf-8"?> <videos> <video> <title>The Distinguished Gentleman</title> <director>Jonathan Lynn</director> </video> <video>The Day After Tomorrow <director>Jonathan Lynn</director> <length>123 Minutes</length> </video> </videos> This time, the first video node has no. The second video has a value, which is The Day After Tomorrow. That means the Text property of the first video node has an empty value and the Text value of the second video node is The Day After Tomorrow.
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 so far, a typical XML node has a start tag, may have a value and may have child nodes. To give you access to the tags and values of the children of a node, the IXMLNode interface is equipped with a string property named XML: __property DOMString XML; Here is an example of accessing it: //--------------------------------------------------------------------------- void __fastcall TfrmMain::btnDocumentClick(TObject *Sender) { TXMLDocument *docVideos = new TXMLDocument(frmMain); docVideos->LoadFromFile(L"C:\\Programs\\videos.xml"); IXMLNode *nodVideo = docVideos->DocumentElement; ShowMessage(nodVideo->XML); } //---------------------------------------------------------------------------
You can also access it from its read-implement, a method named GetXML and whose syntax is: DOMString GetXML(); This method returns a string that includes the symbols, the name(s), and the value(s) if any, of the node that called it.
Consider the following file named videos.xml: <?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 Minutes</length> <format>DVD</format> <rating>PG-13</rating> </video> </videos> 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. In the above XML code example, 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.
To support the child nodes of a particular node, the IXMLNode interface is equipped with a property named ChildNodes. To identify the collection of child nodes of a node, the VCL provides the TXMLNodeList class. The TXMLNodeList class starts as follows: class TXMLNodeList : public TInterfacedObject, public IXMLNodeList; As you can see, the TXMLNodeList class implements an interface named IXMLNodeList. The ChildNodes property of an IXMLNode collection is of type IXMLNodeList. This property is declared as follows: __property IXMLNodeList ChildNodes; When this property is used, it produces an IXMLNodeList list, which is a collection of all nodes that share the same parent. Each item in the collection is of type IXMLNode.
Consider the following document: <?xml version="1.0" encoding="utf-8"?> <videos> <video> <title>The Distinguished Gentleman</title> <director>Jonathan Lynn</director> </video> <video>The Day After Tomorrow <director>Jonathan Lynn</director> <length>123 Minutes</length> </video> <video></video> <video>The General's Daughter <rating>PG-13</rating> <color></color> <yearreleased>1999</yearreleased> </video> </videos> As seen previously, the video node has children, and as mentioned already, one node can have a value while another would not. In the same, when having nodes of the same level, such as the video nodes in this example, the second and third video nodes have a value. Although these parent nodes do not have the same children, the children, if any, of each are allowed to have or not have values. These are pieces of information you should know before starting to perform operations on nodes as we will study in the next lesson.
To give you the number of child nodes that an element contains, the TXMLNodeList class is equipped with a property named Count: __property int Count; Here is an example of using it: //--------------------------------------------------------------------------- void __fastcall TfrmMain::btnDocumentClick(TObject *Sender) { TXMLDocument *docVideos = new TXMLDocument(frmMain); docVideos->LoadFromFile(L"C:\\Programs\\videos.xml"); IXMLNode *nodVideo = docVideos->DocumentElement; IXMLNodeList *lstVideos = nodVideo->ChildNodes; ShowMessage("The root element contains " + AnsiString(lstVideos->Count) + " nodes"); } //--------------------------------------------------------------------------- This would produce: You can also use the Count property in a for loop to visit the members of the collection.
The children of a node, that is, the members of a ChildNodes property, or the members of an IXMLNodeList collection, can be located each by an index. The first node has an index of 0, the second has an index of 1, an so on. To give you access to a node of the collection, the IXMLNodeList interface is equipped with the overloaded [] property. For example, if a node has four children, to access the third, you can apply an index of 2 to the [] property. Here is an example: //--------------------------------------------------------------------------- void __fastcall TfrmMain::btnDocumentClick(TObject *Sender) { TXMLDocument *docVideos = new TXMLDocument(frmMain); docVideos->LoadFromFile(L"C:\\Programs\\videos.xml"); IXMLNode *nodVideo = docVideos->DocumentElement; IXMLNodeList *lstVideos = nodVideo->ChildNodes; lstVideos[2]; } //--------------------------------------------------------------------------- When applying the square brackets operator to access a node based on its index, the operation produces an IXMLNode object that has all the characteristics of a node as we have see so far. This means that an IXMLNode[index] item is an XML element that has a name, may have a value, and may have children. To access a child node, you can apply the [] operator to it. Here is an example: //--------------------------------------------------------------------------- void __fastcall TfrmMain::btnDocumentClick(TObject *Sender) { TXMLDocument *docVideos = new TXMLDocument(frmMain); docVideos->LoadFromFile(L"C:\\Programs\\videos.xml"); IXMLNode *nodVideo = docVideos->DocumentElement; IXMLNodeList *lstVideos = nodVideo->ChildNodes; ShowMessage((lstVideos[0])[0]->XML); } //--------------------------------------------------------------------------- Fortunately, the compiler can access the collection of elements of a node so you do not need the parentheses. Here is an example: //--------------------------------------------------------------------------- void __fastcall TfrmMain::btnDocumentClick(TObject *Sender) { TXMLDocument *docVideos = new TXMLDocument(frmMain); docVideos->LoadFromFile(L"C:\\Programs\\videos.xml"); IXMLNode *nodVideo = docVideos->DocumentElement; IXMLNodeList *lstVideos = nodVideo->ChildNodes; ShowMessage((lstVideos[0])[0]->XML); ShowMessage(lstVideos[0][1]->XML); } //--------------------------------------------------------------------------- As an alternative, to give you access to a child of a node, the IXMLNode interface is equipped with a method named Get. Its syntax is: IXMLNode Get(int Index); This method takes an integer as argument. That integer is the index of the element you want to access. Here is an example: //--------------------------------------------------------------------------- void __fastcall TfrmMain::btnDocumentClick(TObject *Sender) { TXMLDocument *docVideos = new TXMLDocument(frmMain); docVideos->LoadFromFile(L"C:\\Programs\\videos.xml"); IXMLNode *nodVideo = docVideos->DocumentElement; IXMLNodeList *lstVideos = nodVideo->ChildNodes; ShowMessage((lstVideos[0])[0]->XML); ShowMessage(lstVideos[0][1]->XML); ShowMessage(lstVideos[0].Get(2)->XML); } //--------------------------------------------------------------------------- Notice that you have various options to access a child of a node. You can use any of these techniques to access the children of a node, the grandchildren of a node, and so on. Here are examples: //--------------------------------------------------------------------------- void __fastcall TfrmMain::btnDocumentClick(TObject *Sender) { TXMLDocument *docVideos = new TXMLDocument(frmMain); docVideos->LoadFromFile(L"C:\\Programs\\videos.xml"); // Locate the root node IXMLNode *nodRoot = docVideos->DocumentElement; IXMLNodeList *lstVideos = nodRoot->ChildNodes; ShowMessage(lstVideos[0].Get(0)->XML); ShowMessage(lstVideos[0].Get(1)->XML); ShowMessage(lstVideos[0].Get(2)->XML); ShowMessage(lstVideos[0].Get(3)->XML); ShowMessage(lstVideos[0].Get(3)->ChildNodes->Get(0)->Text); ShowMessage(lstVideos[0].Get(3)->ChildNodes->Get(1)->Text); ShowMessage(lstVideos[0].Get(3)->ChildNodes->Get(2)->Text); ShowMessage(lstVideos[0].Get(3)->ChildNodes->Get(3)->Text); ShowMessage(lstVideos[0].Get(3)->ChildNodes->Get(4)->Text); } //---------------------------------------------------------------------------
Not all nodes have children, obviously. To find out whether a node has children, check its HasChildNodes Boolean property that is declared as follows: __property Boolean HasChildNodes; If a node is a child, to get its parent, you can access its ParentNode property. It is declared as: __property IXMLNode ParentNode; Notice that the ParentNode property is an element of type IXMLNode. Here is an example of accessing it: //--------------------------------------------------------------------------- void __fastcall TfrmMain::btnDocumentClick(TObject *Sender) { TXMLDocument *docVideos = new TXMLDocument(frmMain); docVideos->LoadFromFile(L"C:\\Programs\\videos.xml"); IXMLNode *nodVideo = docVideos->DocumentElement; IXMLNodeList *lstVideos = nodVideo->ChildNodes; ShowMessage((lstVideos[0])[0]->XML); ShowMessage(lstVideos[0][1]->XML); ShowMessage(lstVideos[0].Get(2)->XML); ShowMessage(lstVideos[0].Get(3)->ParentNode->XML); } //--------------------------------------------------------------------------- You can also access the parent of a node by calling the IXMLNode::GetParentNode() method. Its syntax is: IXMLNode GetParentNode();
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>. To give you access to the first child of a node, the TXMLNodeList class is equipped with a method named First whose syntax is: IXMLNode First(); Here is an example: //--------------------------------------------------------------------------- void __fastcall TfrmMain::btnDocumentClick(TObject *Sender) { TXMLDocument *docVideos = new TXMLDocument(frmMain); docVideos->LoadFromFile(L"C:\\Programs\\videos.xml"); IXMLNode *nodVideo = docVideos->DocumentElement; IXMLNodeList *lstVideos = nodVideo->ChildNodes; ShowMessage(lstVideos[0].First()->XML); } //--------------------------------------------------------------------------- This would produce:
In the same way, you can scan the document so that, when you get to a node, you can access its first child, if it has any. Here are examples: //--------------------------------------------------------------------------- void __fastcall TfrmMain::btnDocumentClick(TObject *Sender) { TXMLDocument *docVideos = new TXMLDocument(frmMain); docVideos->LoadFromFile(L"C:\\Programs\\videos.xml"); IXMLNode *nodVideo = docVideos->DocumentElement; IXMLNodeList *lstVideos = nodVideo->ChildNodes; for(int i = 0; i < lstVideos->Count; i++) lbxTitles->Items->Add(lstVideos[0].Get(i)->ChildNodes->First()->Text); } //--------------------------------------------------------------------------- This would produce:
As opposed to the first child, the child node that immediately precedes the end-tag of the parent node is called the last child. To give you access to the last child of a node, the IXMLNodeList interface is equipped with a method named Last and whose syntax is: IXMLNode Last(); Here is an example of calling it: //--------------------------------------------------------------------------- void __fastcall TfrmMain::btnDocumentClick(TObject *Sender) { TXMLDocument *docVideos = new TXMLDocument(frmMain); docVideos->LoadFromFile(L"C:\\Programs\\videos.xml"); IXMLNode *nodVideo = docVideos->DocumentElement; IXMLNodeList *lstVideos = nodVideo->ChildNodes; ShowMessage(lstVideos[0].Last()->XML); } //---------------------------------------------------------------------------
In the same way, you can first access a child node, then access the last one of its children. When calling the IXMLNodeList::Last() method, if the node that calls it does not have any children, the compiler would throw an EAccessViolation exception.
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> <video> <title>Sneakers</title> <director>Phil Alden Robinson</director> <length>2 Hrs 6 Mins</length> <format>DVD</format> <rating>N/R</rating> </video> </videos> The child nodes that are nested in a parent node and share the same level are referred to as siblings. In our videos.xml 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. Consider the following example: //--------------------------------------------------------------------------- void __fastcall TfrmMain::btnDocumentClick(TObject *Sender) { TXMLDocument *docVideos = new TXMLDocument(frmMain); docVideos->LoadFromFile(L"C:\\Programs\\videos.xml"); // Locate the root node IXMLNode *nodRoot = docVideos->DocumentElement; IXMLNodeList *lstVideos = nodRoot->ChildNodes; ShowMessage(lstVideos[0].Get(0)[0].ChildNodes->First()->XML ); } //--------------------------------------------------------------------------- This code accesses the first child node of the first element:
To give you access to the next sibling of a node, the IXMLNode interface is equipped with a method named NextSibling. Its syntax is: IXMLNode NextSibling(); Therefore, to call the IXMLNode::NextSibling() method, first get to, or access, a node. Then call this method to get to the most next sibling under it. Here is an example: //--------------------------------------------------------------------------- void __fastcall TfrmMain::btnDocumentClick(TObject *Sender) { TXMLDocument *docVideos = new TXMLDocument(frmMain); docVideos->LoadFromFile(L"C:\\Programs\\videos.xml"); // Locate the root node IXMLNode *nodRoot = docVideos->DocumentElement; IXMLNodeList *lstVideos = nodRoot->ChildNodes; ShowMessage(lstVideos[0].Get(0)[0].ChildNodes->First()->NextSibling()->XML); } //--------------------------------------------------------------------------- This would produce:
In the same way, you can access an element, then access its next sibling, then access the next sibling of that other one, and so on. Here is an example: |
//--------------------------------------------------------------------------- void __fastcall TfrmMain::btnDocumentClick(TObject *Sender) { TXMLDocument *docVideos = new TXMLDocument(frmMain); docVideos->LoadFromFile(L"C:\\Programs\\videos.xml"); // Locate the root node IXMLNode *nodRoot = docVideos->DocumentElement; IXMLNodeList *lstVideos = nodRoot->ChildNodes; ShowMessage(lstVideos[0].Get(0)[0].ChildNodes->First()->NextSibling()->NextSibling()->XML ); } //---------------------------------------------------------------------------
|
||
Previous | Copyright © 2008-2009 FunctionX, Inc. | Next |
|