XML Reader/Writer

Samples

 

MSXML3 SDK help files contain all the information you need to work with these SAX2 components.

XML reader/writer is an all-in-one and easy-to-use component that implements XMLReader, XMLWriter as well as all the available handlers.

Drop XMLParser-component on a form and parse away:

 

procedure TForm1.Button1Click(Sender: TObject);
begin
  XmlParser1.ParseUrl('c:\test.xml');
end;

 

Attaching external handlers.

To make the work with XMLParser more flexible you might want to connect external handlers, which you can change during parsing, depending on the data:

procedure TForm1.Button1Click(Sender: TObject);
begin
XMLparser1.contentHandler:=SaxContenthandler1;
XmlParser1.ParseUrl('c:\test.xml');
end;

procedure TForm1.SAXContentHandler1StartDocument;
begin
Showmessage('Here I am!');
end;

procedure TForm1.XMLParser1StartDocument;
begin
Showmessage('Here I am, too! Think it is time to change contenthandler');
XMLParser1.Contenthandler:=SaxContenthandler2;
end;

 

You don't even need the XMLParser-component. Drop the handlers you want to use on the form and do something like:

 

procedure TForm1.Button2Click(Sender: TObject);
var
  Reader: IVBSAXXMLReader;
begin
  Reader := CoSaxXMLReader30.Create;
  Reader.ContentHandler:=SaxContenthandler1;
  Reader.parseUrl('c:\test.xml');
end;

procedure TForm1.SAXContentHandler1StartDocument;
begin
  Showmessage('Here I am!');
end;

 

Locator.

You can call XMLParser.Locator to get location  information.

XMLParser1.Locator.LineNumber
XMLParser1.Locator.ColumnNumber
XMLParser1.Locator.SystemID
XMLParser1.Locator.PublicID

procedure TForm1.Button1Click(Sender: TObject);
begin
  XMLParser1.ParseURL('c:\test.xml');
end;

procedure TForm1.XMLParser1StartElement(var strNamespaceURI, strLocalName,strQName: WideString; const oAttributes: IVBSAXAttributes);
begin
  Showmessage('Column: '+    InttoStr(XmlParser1.locator.columnNumber)+#13#10+
'Line: '+ InttoStr(XmlParser1.locator.LineNumber));
end;


Output and pipelines.

Setting property "writing" will make sure the output is written to XMLParser.Output:

procedure TForm1.Button1Click(Sender: TObject);
begin
 
XMLParser1.Writing:=TRUE;
  XMLParser1.ParseUrl('c:\test.xml');
  Memo1.lines.text:=XMLparser1.Output;
end;

You can use this to connect a number of XMLParsers to each other and let each of them do a particular job and pass on data to next parser:

procedure TForm1.Button1Click(Sender: TObject);
begin
 
XMLParser1.Writing := TRUE;
  XMLParser2.Writing := TRUE;
  XMLParser1.ParseUrl('c:\test.xml');
  XMLParser2.Parse(XMLparser1.Output);
  Memo1.Lines.Text := XMLParser2.Output;
end;

You can use the implemented ISAXXMLFilter.Parent property to do a similar kind of filtering. See MSXML3 SDK help files for more info.


XML-writing.

You can just as easy work in the other direction and use the component to create XML-files based on e.g. database information. Here is a delphi-translation of the xml-write-sample in Aron Skonnards SAX XMLWriter demo:

procedure TForm1.Button1Click(Sender: TObject);
var
atrs: TXMLAttributes;
begin
 atrs := TXMLAttributes.Create;
 with XMLParser1 do begin
   startDocument;
   writestartDTD('MyDTD', '', 'http://eureka.sample/mydtd.dtd');
   writeelementDecl('book', 'title | descr');
   writeattributeDecl('book', 'author', 'CDATA', '#IMPLIED', '');
   writeattributeDecl('book', 'ISBN', 'CDATA', '#REQUIRED', '000000000');
   writeattributeDecl('book', 'cover', '(hard|soft)', '', 'soft');
   writeelementDecl('title', '(#PCDATA)');
   writeelementDecl('descr', '(#PCDATA)');
   endDTD;
   atrs.addAttribute('', '', 'cover', '', 'hard');
   writestartElement('', '', 'book', atrs);
   atrs.clear;
   writestartelement('', '', 'title', atrs);
   writecharacters('On the Circular Problem of Quadratic Equations');
   writeendelement('', '', 'title');
   writeendelement('', '', 'book');
   enddocument;
   Listbox1.items.text := output;
  end;
end;

And here is the output in Listbox1:

<?xml version="1.0" encoding="UTF-16" standalone="no"?>
<!DOCTYPE MyDTD SYSTEM "http://eureka.sample/mydtd.dtd" [
<!ELEMENT book title | descr>
<!ATTLIST book author CDATA #IMPLIED>
<!ATTLIST book ISBN CDATA #REQUIRED "000000000">
<!ATTLIST book cover (hard|soft) "soft">
<!ELEMENT title (#PCDATA)>
<!ELEMENT descr (#PCDATA)>
]>
<book cover="hard"><title>On the Circular Problem of Quadratic Equations</title></book>

 

Useful Links:

MSDN Online XML Developer Center

MSXML 3.0 MergeModule Redistribution Package

Solving the SAX Puzzle- Off-the-Shelf XML Processing

Saving XML Data Using the Internet Explorer XML Parser

SAX ContentHandler Implementations

The XML Files- MSXML 3.0 Supports XPath 1.0, XSLT 1.0, XDR, and SAX2 -- MSDN Magazine, September 2000