?
Solved

Load XML to ADODataset

Posted on 2006-07-05
10
Medium Priority
?
798 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
[X]
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
  • 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
Independent Software Vendors: 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!

 

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 200 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

Industry Leaders: 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

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 The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

762 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