Class DXMLReader

Inherits from:
Object
Declared in:
DXML.h

Class Hierarchy

    Object
      |
      +---DXMLReader

Class Description

The DXMLReader class implements methods for parsing a xml file. The class uses expat for the low level parsing. The parsing of DTDs and External Entities are not supported. The method 'characters' returns the exact contents between the start elements and end element, including linefeed and indent spacing. The method 'unparsed' returns everything that could not be parsed, including DTDs and External Entities References. Use a character that can't be part of an URI e.g. '|' for the namespace separator.

Example:
#include <stdio.h>
#include "ofc/DXML.h"
#include "ofc/DFile.h"

@interface MyHandler : Object <DXMLHandler>  // Class for handling of XML source
{
}
- (BOOL) startDocument :(const DXMLChar *) version :(const DXMLChar *) encoding :(int) standalone;
- (BOOL) endDocument;
- (BOOL) startElement :(const DXMLChar *) name;
- (BOOL) attribute :(const DXMLChar *) attribute :(const DXMLChar *) value;
- (BOOL) endElement;
- (BOOL) characters :(const DXMLChar *) text;
- (BOOL) comment :(const DXMLChar *) text;
- (BOOL) processingInstruction :(const DXMLChar *) target :(const DXMLChar *) value;
- (BOOL) startCDATA;
- (BOOL) endCDATA;
- (BOOL) startNamespace :(const DXMLChar *) prefix :(const DXMLChar *) uri;
- (BOOL) endNamespace;
- (BOOL) unparsed :(const DXMLChar *) text;
- (void) error :(int) number :(const char *) name :(int) lineNumber :(int) columnNumber;
@end

@implementation MyHandler

- (BOOL) startDocument :(const DXMLChar *) version :(const DXMLChar *) encoding :(int) standalone
{                                      // Process the start of the xml document
  printf("Start of XML document; version:%s encoding:%s standalone:%d\n", version, encoding, standalone);
  return YES;
}

- (BOOL) endDocument
{
  printf("End of XML document");       // Process the end of the xml document
  return YES;
}

- (BOOL) startElement :(const DXMLChar *) name
{
  printf("Start Element:%s\n", name);  // Process the start of an xml element
  return YES;
}

- (BOOL) attribute :(const DXMLChar *) attribute :(const DXMLChar *) value
{
  printf("Attribute: %s = %s\n", attribute, value); // Process an attribute
  return YES;
}

- (BOOL) endElement
{
  printf("End Element\n");             // Process the end of an xml element
  return YES;
}

- (BOOL) characters :(const DXMLChar *) text
{
  printf("Text:%s\n", text);           // Process normal text
  return YES;
}

- (BOOL) comment :(const DXMLChar *) text
{
  return YES;                          // Ignore comment
}

- (BOOL) processingInstruction :(const DXMLChar *) target :(const DXMLChar *) value
{
  return YES;                          // Ignore PI
}

- (BOOL) startCDATA
{
  return YES;                          // Ignore CDATA
}

- (BOOL) endCDATA
{
  return YES;                          // Ignore CDATA
}

- (BOOL) startNamespace :(const DXMLChar *) prefix :(const DXMLChar *) uri
{
  return YES;                          // Ignore start of namespace
}

- (BOOL) endNamespace
{
  return YES;                          // Ignore end of name sapce
}

- (BOOL) unparsed :(const DXMLChar *) text
{
  return YES;                          // Ignore unparsed text
}

- (void) error :(int) number :(const char *) name :(int) lineNumber :(int) columnNumber
{
  printf("Error:%s in %s at line:%d and column: %d\n", [DXMLReader errorToString :number], name, lineNumber, columnNumber);
}
@end


int main(int argc, char *argv[])
{
  MyHandler  *hdlr   = [MyHandler  new];
  DXMLReader *rdr    = [DXMLReader new];
  DFile      *file   = [DFile      new];
  char        name[] = "example.xml";

  if ([file open :name :"r"])
  {
    if ([rdr parse :file :name :hdlr :'|'])
      printf("File \"%s\" succesfully parsed.\n", name);
    else
      printf("Error during parsing of \"%s\".", name);

    [file close];
  }
  else
    printf("Could not open \"%s\".\n", name);

  [rdr  free];                         // Cleanup
  [hdlr free];
  [file free];

  return 0;
}
Last modified:
11-Aug-2008 (DXML.h)

Instance Variables

private id <DXMLHandler> _handler
the reference to the xml handler
private int _bufferSize
the size of the buffer (def. 8192)
private XML_Parser _parser
the xml parser
private DText *_name
the name of the source
private DXMLChar _separator
the namespace separator (def. \0 = no namespace)
private DXMLChar *_encoding
the encoding to be used (def. NULL = from xml file)
private DText *_characters
the characters buffer
private DText *_unparsed
the unparsed buffer

Method Index


generated 06-Sep-2008 by ObjcDoc 3.0.0