1

Here is the thing I want to do. My program is working fine with this:

XMLText =  '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>' +
'<book xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">' +
  '<title>Harry Potter</title>' +
  '<author>J. K. Rowling.</author>' +
  '<length>400</length>' +
'</book>';


procedure TForm1.Button1Click(Sender: TObject);
var
XMLDoc: IXMLDOMDocument;
Node, SibNode: IXMLDOMNode;
begin
  Memo1.Clear;
  XMLDoc := CoDOMDocument.Create;
  XMLDoc.loadXML(XMLText);
end;

Now I want to import XML file with 6000 books (books.xml) from the same folder where project is, instead of const XMLText. How can I do that?

Thank you! :)

3 Answers 3

6

Option 1: load directly from disk

IXMLDomDocument has a load method that accepts a filename. You can use that method instead of loadXML, which you are currently using.

Option 2: load the file into a string first

Alternatively, you can load your file into a string first. I can hardly find any reason to do so in this case, but it can never hurt to know. :)

Take a look at TStringStream, which has a LoadFromFile method to load a file from disk. You can use it to load the entire books.xml into memory. After loading the file, you can pass the stringstreams's DataString property to the loadXML method. This property returns the entire contents of the stream (containing the XML) as a string.

4

Use the Delphi XML Data Binding Wizard for this.

It will generate a unit with Delphi wrapper objects around your XML file (or if you have it an XSD file describing the XML).

Those wrappers are based on the IXMLDocument which is very similar to the IXMLDOMDocument you are using now, and add a layer around it that allows you to access your data with more support from the Delphi compiler usually making the process of handling the data inside the XML much easier than using plain IXMLDocument or IXMLDOMDocument.

The unit contains methods to load the XML from either a file or a string.

There is a good tutorial and a nice video on using this wizard.

4
  • This isn't what the question asked about doing. The question asked about using IXMLDOMDocument and loading from a file instead of a constant string. The XML Data Binding Wizard isn't the answer to every question about XML and Delphi.
    – Ken White
    Commented Jul 12, 2013 at 21:32
  • I know, and I still think it is a valid answer when you scale up from a small XML string to a large XML file where likely more logic is involved. I've seen plain IXMLDOMDocument solutions fail at too many clients, and usually adding an intermediate layer made the process much easier. Let me know where you think I can make my answer more clear, and I will try to do that. Note I upvoted your answer as it is the most simple solution. Commented Jul 12, 2013 at 21:50
  • I agree with the limitations of the XML Data Binding Wizard. With the current compiler technology (generics, attributes) a much better wrapper could be generated. I still haven't uploaded most of my XML Demos, which could form the base of a more elaborate answer (: Commented Jul 12, 2013 at 22:02
  • 1
    +1. Nice edit; the information makes it much more useful in case the link targets are not up for some reason. I'll delete my last comment, because you've addressed the majority of what it said (although the part about not answering the question asked still applies). :-)
    – Ken White
    Commented Jul 12, 2013 at 22:14
3

Just change the loadXML to load('YourFileName.xml'):

procedure TForm1.Button1Click(Sender: TObject);
var
  XMLDoc: IXMLDOMDocument;
begin
  XMLDoc := CoDOMDocument.Create;
  XMLDoc.load('MyBooks.xml');
  Memo1.Lines.Text := XMLDoc.xml;
end;
2
  • It doesn't work for me. Xml file needs to be in the same folder as .exe file, correct? Commented Jul 15, 2013 at 11:01
  • 5
    Replace "MyBooks.xml" with the actual path and name of your own XML file, of course. Also, "it is not working for me" is not a description of a problem; I can't read your screen or your mind from here, so I have no idea what that means.
    – Ken White
    Commented Jul 15, 2013 at 16:33

Not the answer you're looking for? Browse other questions tagged or ask your own question.