reading XLM file in Delphi (IXMLDocument) not working

Posted on 2014-03-11
Last Modified: 2014-03-11
I'm successfully writing an XML document with IXMLDocument which, slightly simplified, looks like this:
<?xml version="1.0"?>
<Subject>3 events scheduled starting 3/13 1:30 PM: Job 1034</Subject>
<sPersonIDs>10240; 175</sPersonIDs>
<MessageText>3/11, 3/12, 3/13 1:30 - 5:00 PM Water Damage Assigned to: Bilbo Baggins</MessageText>

Open in new window

My attempt to read the file looks like this:
procedure TChronEmail.GetFromTempFile;
    sFile : string;
    Doc: IXMLDocument;
    Email: IXMLNode;
  sFile := [filename--I've tested that it is valid and that it is there];
  if fileExists(sFile) then begin
    i := 0; // for this test, only need the first entry in the file
    Email := DOC.ChildNodes.Nodes['Email'].ChildNodes[i];
    Subject := Email.ChildNodes['Subject'].Text;
    if tryStrToInt(Email.ChildNodes['DeptID'].Text, i) then DeptID := i;
    sPersonIDs := Email.ChildNodes['sPersonIDs'].Text;
    MessageText := Email.ChildNodes['MessageText'].Text;

Open in new window

Apparently I'm missing something important since I'm not getting any values back out of my file. This is my first time working with IXMLDocument/IXMLNode, so happy for any guidance. Thanks.
Question by:EricTaylor
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
LVL 100

Expert Comment

ID: 39921263
Are you sure the code for the file found test is being run?

Is the childnode array dimensioned from 0 or 1?

LVL 45

Accepted Solution

aikimark earned 500 total points
ID: 39921404
Since I don't see any parent to the Email node(s), the following change should return results.
Email := DOC.ChildNodes.Nodes['Email'];

Open in new window

If the XML had been like the following...
<?xml version="1.0"?>
<Subject>3 events scheduled starting 3/13 1:30 PM: Job 1034</Subject>
<sPersonIDs>10240; 175</sPersonIDs>
<MessageText>3/11, 3/12, 3/13 1:30 - 5:00 PM Water Damage Assigned to: Bilbo Baggins</MessageText>

Open in new window

then your original statement should have been:
Email := DOC.ChildNodes.Nodes['Emails'].ChildNodes[i];

Open in new window

LVL 45

Expert Comment

ID: 39921427
Note: some XPathing might also get you the first Email item.
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.


Author Closing Comment

ID: 39921591
Thanks. That did the trick and now I understand how the pieces are relating here.

I didn't understand the XPathing comment though; would appreciate if you would clarify to give me a better understanding.
LVL 45

Expert Comment

ID: 39921856

Maybe it would be better if I point you at some instructional material on XPath to get you up to speed. (tutorial) (syntax)

Since we're in a Delphi development environment, you would probably benefit from seeing some Delphi code examples that use XPath.  (Delphi example)  (lots of Delphi examples in this ebook/article)

If you are still stuck after playing with this, you can ask a new question about XPathing and your Delphi code.

Author Comment

ID: 39921985
thanks  :)

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Skype is a P2P (Peer to Peer) instant messaging and VOIP (Voice over IP) service – as well as a whole lot more.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
This video demonstrates how to use each tool, their shortcuts, where and when to use them, and how to use the keyboard to improve workflow.
The viewer will learn how to successfully download and install the SARDU utility on Windows 7, without downloading adware.

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question