Solved

Load XML to ADODataset

Posted on 2006-07-05
10
775 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:ciuly
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
 

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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

744 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

11 Experts available now in Live!

Get 1:1 Help Now