Solved

reading XLM file in Delphi (IXMLDocument) not working

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

Open in new window

My attempt to read the file looks like this:
procedure TChronEmail.GetFromTempFile;
  var
    i:integer;
    sFile : string;
    Doc: IXMLDocument;
    Email: IXMLNode;
begin
  sFile := [filename--I've tested that it is valid and that it is there];
  if fileExists(sFile) then begin
    Doc.LoadFromFile(sFile);
    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.
0
Comment
Question by:EricTaylor
  • 3
  • 2
6 Comments
 
LVL 100

Expert Comment

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

Is the childnode array dimensioned from 0 or 1?

mlmcc
0
 
LVL 45

Accepted Solution

by:
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"?>
<Emails>
<Email>
<Subject>3 events scheduled starting 3/13 1:30 PM: Job 1034</Subject>
<DeptID>111</DeptID>
<sPersonIDs>10240; 175</sPersonIDs>
<MessageText>3/11, 3/12, 3/13 1:30 - 5:00 PM Water Damage Assigned to: Bilbo Baggins</MessageText>
</Email>
</Emails>

Open in new window

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

Open in new window

0
 
LVL 45

Expert Comment

by:aikimark
ID: 39921427
Note: some XPathing might also get you the first Email item.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Closing Comment

by:EricTaylor
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.
0
 
LVL 45

Expert Comment

by:aikimark
ID: 39921856
@EricTaylor

Maybe it would be better if I point you at some instructional material on XPath to get you up to speed.
http://www.w3schools.com/XPath/ (tutorial)
http://www.w3schools.com/xpath/xpath_syntax.asp (syntax)

Since we're in a Delphi development environment, you would probably benefit from seeing some Delphi code examples that use XPath.
http://delphi.about.com/od/vclusing/qt/delphi-select-xml-nodes-ixmlnodelist-selectnodes-xpath-xmldom.htm  (Delphi example)
http://keith-wood.name/DelphiXML/BookCode/  (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.
0
 

Author Comment

by:EricTaylor
ID: 39921985
thanks  :)
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Today companies are subjected to more-and-more data, and it won't stop any time soon.  But there are obvious opportunities for reducing data, particularly data duplicated among companies.
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
The viewer will learn how to successfully download and install the SARDU utility on Windows 8, without downloading adware.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

743 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now