Solved

Load XML to ADODataset

Posted on 2006-07-05
10
792 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
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!

 

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

691 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