Solved

Load XML to ADODataset

Posted on 2006-07-05
10
783 Views
Last Modified: 2010-07-27
Hi.

Is there a way to load a XML string into an ADODataset without using midas or a transform file?

Sample XML:
<?xml version="1.0" encoding="ISO-8859-2" ?>
<Ticket>
<DATA type="D" value="DC_HEADER" align="C" face="Arial" size="8" style="N" voffset="0" mroffset="-5"/>
<DATA type="HL" voffset="-2"/>
<DATA type="D" value="DC_FOOTER" voffset="0"/>
</Ticket>

Thanks.
0
Comment
Question by:BlueAlien
  • 7
  • 2
10 Comments
 
LVL 28

Expert Comment

by:2266180
ID: 17044750
take a look at this PAQ:
http://www.experts-exchange.com/Programming/Programming_Languages/Delphi/Q_21586387.html
you might need to save the xml file from an adodataset first to see exactly the formatting before loading (just to make sure you follow the same formatting rules)
0
 
LVL 26

Expert Comment

by:EddieShipman
ID: 17053619
This data cannot be transformed as is. More information is needed

My XMLTable control will not save back to the table, only create the
XML file from the dataset results and it uses a standard format. The
default format is the same as Oracle 8 used for importing.
0
 
LVL 26

Expert Comment

by:EddieShipman
ID: 17053624
If you have a schema for your ADODataset that you can show and how the XML data corresponds,
I'm sure someone may write a parser for you.
0
Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 

Author Comment

by:BlueAlien
ID: 17056558
It looks like this (column name, data type, max length):
type, string, 2
value, string, 50
align, string, 1
face, string, 30
size, string, 2
style, string, 1
voffset, string, 5
mroffset, string, 5
break, string, 1

The problem is, that I need to keep the XML file as easiest to edit as possible. Or if you could suggest some other XML format (I don't know very much about XML). I could write my own parser for this specific format, but than I'll have to modify it every time a change occurs.
0
 
LVL 26

Expert Comment

by:EddieShipman
ID: 17058456
Ok, give me a little while and I'll write a parsing routine for you.
0
 
LVL 26

Accepted Solution

by:
EddieShipman earned 50 total points
ID: 17058788
Here is a basic XML Parsing routine for you. You can modify to suit your needs.

unit uxmltoado;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, MSXML2_TLB, DB, ADODB, Grids, DBGrids, StdCtrls, ActiveX, COMObj;

type
  TForm1 = class(TForm)
    Memo1: TMemo;
    Button1: TButton;
    DataSource1: TDataSource;
    DBGrid1: TDBGrid;
    ADOTable1: TADOTable;
    ADOConnection1: TADOConnection;
    ADOTable1type: TWideStringField;
    ADOTable1value: TWideStringField;
    ADOTable1align: TWideStringField;
    ADOTable1face: TWideStringField;
    ADOTable1size: TWideStringField;
    ADOTable1style: TWideStringField;
    ADOTable1voffset: TWideStringField;
    ADOTable1mroffset: TWideStringField;
    ADOTable1break: TWideStringField;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
  oXMLDoc:                   IXMLDOMDocument2;
  s:                         String;
  oTicketNodeList:           IXMLDOMNodeList;
  oDataNodeList:             IXMLDOMNodeList;
  oDataNode:                 IXMLDOMNode;
  oDataAttribs:              IXMLDOMNamedNodeMap;
  oDataAttrib:               IXMLDOMNode;
  i, j, x:                   Integer;
  sType:                     String;
  sValue:                    String;
  sAlign:                    String;
  sFace:                     String;
  sSize:                     String;
  sStyle:                    String;
  sVoffset:                  String;
  sMroffset:                 String;
  sBreak:                    String;
begin
  oXMLDoc  := CreateOleObject('MSXML2.DOMDocument.3.0') as IXMLDOMDocument2;
  try
    s := StringReplace(StringReplace(Memo1.Lines.Text, #13, '', [rfReplaceAll]), #10, '', [rfReplaceAll]);
    oXMLDoc.loadXML(s);
    // GET the ticket nodes
    oTicketNodeList := oXMLDoc.selectNodes('//Ticket');
    for i := 0 to oTicketNodeList.length-1 do
    begin
      // get the DATA nodes
      oDataNodeList := oTicketNodeList.item[i].childNodes;
      for j := 0 to oDataNodeList.length-1 do
      begin
        oDataNode := oDataNodeList.item[j];
        // Get the Attributes of the DATA node
        // Clear out old values from last DATA node.
        sType     := '';
        sValue    := '';
        sAlign    := '';
        sFace     := '';
        sSize     := '';
        sStyle    := '';
        sVoffset  := '';
        sMroffset := '';
        sBreak    := '';
        oDataAttribs := oDataNode.attributes;
        for x := 0 to oDataAttribs.length-1 do
        begin
          oDataAttrib := oDataAttribs.item[x];
          if oDataAttrib.nodeName = 'type' then
            sType := oDataAttrib.text;
          if oDataAttrib.nodeName = 'value' then
            sValue := oDataAttrib.text;
          if oDataAttrib.nodeName = 'align' then
            sAlign := oDataAttrib.text;
          if oDataAttrib.nodeName = 'face' then
            sFace := oDataAttrib.text;
          if oDataAttrib.nodeName = 'size' then
            sSize := oDataAttrib.text;
          if oDataAttrib.nodeName = 'style' then
            sStyle := oDataAttrib.text;
          if oDataAttrib.nodeName = 'voffset' then
            sVoffset := oDataAttrib.text;
          if oDataAttrib.nodeName = 'mroffset' then
            sMroffset := oDataAttrib.text;
          if oDataAttrib.nodeName = 'break' then
            sBreak := oDataAttrib.text;
        end;
        ADOTable1.InsertRecord([sType, sValue, sAlign, sFace, sSize, sStyle, sVoffset, sMroffset, sBreak]);
        Application.ProcessMessages;
      end;
    end;
  finally
    oXMLDoc  := nil;
  end;
end;

end.
0
 
LVL 26

Expert Comment

by:EddieShipman
ID: 17058911
I forgot to mention that the Memo on the form has the XML text.
0
 
LVL 26

Expert Comment

by:EddieShipman
ID: 17058913
Oh, and you can remove the persistant fields from ADOTale1, too.
0
 

Author Comment

by:BlueAlien
ID: 17071174
Thanks for the parser, it works perfectly.

PS: Why do you have to remove #13 and #10?
0
 
LVL 26

Expert Comment

by:EddieShipman
ID: 17096003
You don't want the extra whitespace in the XML when parsing.
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

772 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