In this article we will look at the code that we used to create a program that enabled us to load an XML document into DOM. This article is the fourth part of a four-part series that covers Delphi, XML, and the DOM.
Loading an XML Document into the DOM - Checking the file (Page 4 of 4 )
Then we check to see if the user has selected a file. If a file name has been selected, then we need to make sure that it is not the root. The root is located at level zero(0), so we check to make sure that the selected level is not zero:
if (tv1.Selected.Level <> 0) then begin
Once we've determined that the selected level is not zero, we then store the name of the selected file in a global variable called fn:
Now we combine the previously extracted path and the stored filename and feed that to the xmldocument:
Next we need to add the information contained in the XML document to the treeview. This job will be done by the DOMShow procedure:
Once a document is loaded into the XMLDocument component, you can access specific bits of it by using the various methods that are available to the component. For example, to fetch the value of any attribute, write the following:
In fact, let's add a procedure to check to see if we can get a value by checking for a node number. Add a button; call it "element check" and add the following code:
To access the nodes you can use something similar and take advantage of the child values array. This is a Delphi extension to the DOM, which allows you to pass as a parameter either the name of the element or its numeric position. Let's add another procedure to demonstrate this:
In the above code, I will get the title of the child node on the root. This is dependent on what document you have loaded.
DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.