Click here to Skip to main content
15,886,578 members
Articles / Programming Languages / Objective C

XML File/Stream Processing

Rate me:
Please Sign up or sign in to vote.
4.50/5 (14 votes)
5 Jan 20072 min read 154K   8K   46   24
Process an XML file or stream; read group and attribute values; write and delete groups, attribs, values and comments

Sample Image

Introduction

This DLL provides routines to manipulate XML files. The set provided is not all-singing-and-dancing, but a useful, small collection. Several co-operating executables living off a common XML file may find their operating parameters and set parameters for others.

Background

Initially, the read functions were implemented to save incorporating the large overhead of using a proprietary interface. From this grew a certain understanding of the mechanism. Then were added write and delete routines; stream routines that allowed the user program to supply and recover the XML data (without using disk files); some super (i.e., over-arching) routines to shrink the user's code.

Using the Code

VC 6.0 projects: Place the XMJDLL.dll in a directory on your path variable. Add the library XMJDLL.lib to the project resources. Add the module XMJcalls.h to the project. Use the routines therein.

VB 6.0 projects: Register the XMJDLL.dll with regsvr32. Add the module XMJDLL.bas to the project. Use the public routines therein.

C#
//
// Sample source to produce the above file
//
  XMJ_newFile("Order");

  XMJ_getFrstGroup("Order",0);
  XMJ_newAttPutVal("number","1234");

  XMJ_pokeNewGrpPutVal("Date","2000/1/1");
  XMJ_newGrpPutVal("Customer","Acme Ltd");
  XMJ_newAttPutVal("ID","1234A");

  XMJ_getFrstGroup("Order",0);
  XMJ_newGroup("ITEM");
  XMJ_getFrstGroup("ITEM",1);
  XMJ_newAttPutVal("ID","01");
  XMJ_newGrpPutVal("Part-number","E16-25A");
  XMJ_newAttPutVal("warehouse","Warehouse11");
  XMJ_getFrstGroup("ITEM",1);
  XMJ_pokeNewGrpPutVal("Description","Production-Class Widget A");
  XMJ_newGrpPutVal("Quantity","16");

  XMJ_getFrstGroup("Order",0);
  XMJ_newGroup("ITEM");
  XMJ_getLastGroup("ITEM",1);
  XMJ_newAttPutVal("ID","02");
  XMJ_newGrpPutVal("Part-number","E23-45B");
  XMJ_newAttPutVal("warehouse","Warehouse11");
  XMJ_getLastGroup("ITEM",1);
  XMJ_pokeNewGrpPutVal("Description","Production-Class Widget B");
  XMJ_newGrpPutVal("Quantity","12");

  XMJ_writeFile(fileName);

Points of Interest

  • For C/C++ only users, a static library can be built using workspace and project files provided.
  • The private routines in the XMJDLL.bas module are to get around C/C++ <-> VB differences.
  • The implementation of 'false' (C/C++ 0, VB -1).
  • VB string addresses to C/C++ routines.
  • VB return-string-parameter is handled in the DLL.

History

  • 3.5 Corrections to XMJ_deProfundis
  • 3.4 Encryption and catch up XM8
    • New XMJ_crypt_vb.zip - demonstration of TEA encryption applied to XML files
    • Four new encryption routines to implement Tiny Encryption Algorithm (TEA)
    • XMJ_sNew.cpp - new loop routine XMJ_deProfundis
    • handles <,&,>," and ' within values; both read and write
  • Group to attribute, and attribute to attribute, white space handled
  • What took 440 mS now takes 160 mS

License

This article has no explicit license attached to it, but may contain usage terms in the article text or the download files themselves. If in doubt, please contact the author via the discussion board below.

A list of licenses authors might use can be found here.


Written By
Web Developer
United Kingdom United Kingdom
BSc (St.Andrews(1963-67))
MSCE
Systems Programmer 39+yrs
Married to first wife 35yrs & counting, four grown-up children
Religious opinions similar to MelG's
It is not the gnosis, but the praxis must be the fruit. (Aristotle)

Comments and Discussions

 
QuestionBug with using different encoding Pin
Artem Moroz22-Apr-08 6:03
Artem Moroz22-Apr-08 6:03 
GeneralDocumentation Anomaly Pin
Garth J Lancaster5-Jan-07 12:26
professionalGarth J Lancaster5-Jan-07 12:26 
GeneralRe: Documentation Anomaly Pin
Lymington7-Jan-07 8:09
Lymington7-Jan-07 8:09 
QuestionXMJ_deProfundis() returning 2, not zero? Pin
CJayNC27-Dec-06 7:25
CJayNC27-Dec-06 7:25 
AnswerRe: XMJ_deProfundis() returning 2, not zero? Pin
Lymington3-Jan-07 3:47
Lymington3-Jan-07 3:47 
QuestionEnumerating through a depth? Pin
TonyCounts19-Apr-06 10:54
TonyCounts19-Apr-06 10:54 
Generalwhat's the meaning of depth Pin
xiyuanzuren12-Apr-06 17:19
xiyuanzuren12-Apr-06 17:19 
GeneralRe: what's the meaning of depth Pin
Lymington17-Apr-06 21:50
Lymington17-Apr-06 21:50 
GeneralRe: what's the meaning of depth Pin
xiyuanzuren19-Apr-06 22:17
xiyuanzuren19-Apr-06 22:17 
GeneralBatch Command Pin
Anonymous1-Aug-05 0:13
Anonymous1-Aug-05 0:13 
Generalreplace attribute Pin
An Phung Nguyen2-Jun-05 15:50
An Phung Nguyen2-Jun-05 15:50 
GeneralRe: replace attribute Pin
Lymington6-Jun-05 6:40
Lymington6-Jun-05 6:40 
QuestionHow Can I read tag's Data Field ? Pin
nico9999999995-May-05 0:35
nico9999999995-May-05 0:35 
AnswerRe: How Can I read tag's Data Field ? Pin
Lymington5-May-05 4:48
Lymington5-May-05 4:48 
Generallooking for a way to read excel attachments Pin
DawnYoshimura21-Jun-04 23:04
DawnYoshimura21-Jun-04 23:04 
GeneralRe: looking for a way to read excel attachments Pin
Lymington22-Jun-04 13:15
Lymington22-Jun-04 13:15 
Something like...?
VERSION 5.00
Begin VB.Form XM8fromXLS 
   Caption         =   "Write/Read XM8fromXLS.xml"
   ClientHeight    =   5055
   ClientLeft      =   60
   ClientTop       =   345
   ClientWidth     =   7290
   LinkTopic       =   "XM8fromXLS"
   ScaleHeight     =   5055
   ScaleWidth      =   7290
   StartUpPosition =   3  'Windows Default
   Begin VB.TextBox Text1 
      CausesValidation=   0   'False
      BeginProperty Font 
         Name            =   "Courier New"
         Size            =   8.25
         Charset         =   0
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   3615
      Left            =   240
      MultiLine       =   -1  'True
      ScrollBars      =   2  'Vertical
      TabIndex        =   1
      Text            =   "XM8fromXLS.frx":0000
      Top             =   1200
      Width           =   6855
   End
   Begin VB.CommandButton Command2 
      Caption         =   "Read XM8fromXLS.xls Write XM8fromCSV.xml"
      Height          =   735
      Left            =   2640
      TabIndex        =   0
      Top             =   240
      Width           =   1935
   End
End
Attribute VB_Name = "XM8fromXLS"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
'
Const rootName = "XLSROOT"
Const rowGroupName = "ROW"
Const filename = "XM8fromXLS"

Dim Lines() As String, ColHeads() As String, RowValues() As String

'
'/ ================================================================================ //
'/  I n i t i a l i s e                                                             //
'/ ================================================================================ //
Private Sub Form_Initialize()
    Text1.Text = vbNewLine + "Made by XM8DLL version " + XM8_getVersion + ", dated " + XM8_getDate
End Sub
'
'/ ================================================================================ //
'/  The ' R e a d / W r i t e ' command                                             //
'/ ================================================================================ //
Private Sub Command2_Click()
  
  ExportXLSfileToCSV
  
  ReadCSVfileIntoLines
    
  ColHeads = Split(Lines(0), ",")
  
  WriteLinesToXMLfile
  
End Sub
'
'/ ================================================================================ //
'/ Export XLS file to CSV file                                                      //
'/ ================================================================================ //
Private Sub ExportXLSfileToCSV()
Dim xlAppl As Excel.Application
Dim xlBook As Excel.Workbook
Dim pat As String: pat = App.Path + "\"
    
    Set xlAppl = New Excel.Application
    Set xlBook = xlAppl.Workbooks.Open(pat + filename + ".xls", , ReadOnly)
    xlBook.SaveAs pat + filename + ".csv", xlCSV
    xlBook.Close False
    Set xlBook = Nothing
    Set xlAppl = Nothing
    
    Text1.Text = "Exported XLS to CSV" + vbNewLine + Text1.Text
End Sub
'
'/ ================================================================================ //
'/ Read CSV file into Lines array                                                   //
'/ ================================================================================ //
Private Sub ReadCSVfileIntoLines()

    Dim stm As ADODB.stream
    Set stm = New ADODB.stream
    stm.Open

    stm.Charset = "UTF-8"
    stm.Position = 0
    stm.Type = adTypeText
    stm.LoadFromFile filename + ".csv"
    stm.Position = 0
    strData = stm.ReadText()
        
    Lines = Split(strData, vbNewLine, -1)
    
    stm.Close
    Set stm = Nothing
    
    Text1.Text = "   Read CSV file" + vbNewLine + Text1.Text
End Sub
'
'/ ================================================================================ //
'/ ================================================================================ //
Private Sub displayError()
    Text1.Text = XM8_getLastError() + vbNewLine + Text1.Text
End Sub
'
'/ ================================================================================ //
'/ Write Lines array to xml file                                                    //
'/ ================================================================================ //
Private Sub WriteLinesToXMLfile()
Dim i As Long, j As Long, ju As Long
    
  If XM8_newFile(rootName) Then
  
    For i = 1 To UBound(Lines) - 1: XM8_newGroup rowGroupName: Next
        
    XM8_getFrstGroup rootName, 0
        
    For i = 1 To UBound(Lines) - 1
      If XM8_getNextGroup(rowGroupName, 1) Then
        If XM8_putGroup(rowGroupName, CStr(i + 1)) Then
          RowValues = Split(Lines(i), ",")
                        
          ju = UBound(ColHeads) ' too many values, limit to number of columns
          If ju > UBound(RowValues) Then ju = UBound(RowValues) ' too many columns, limit to number of values
            
          For j = 0 To ju
            If XM8_newAttribute(ColHeads(j)) Then
              If Not XM8_putAttribute(ColHeads(j), RowValues(j)) Then displayError
            Else
              displayError
            End If
          Next
        Else
          displayError
        End If
      Else
        displayError
        Exit For
      End If
    Next
        
    If Not XM8_writeFile(filename + ".xml") Then displayError
        
    XM8_reset
    
    Text1.Text = "Written XML file" + vbNewLine + Text1.Text
  Else
    displayError
  End If

End Sub
'

This takes an xls file, your supply, 'fileName'.xls
Makes a csv file 'fileName'.csv
Then converts this to an xml file 'fileName'.xml
with a group for every row containg an attribute for every titled column
GeneralRe: looking for a way to read excel attachments Pin
XiAoYanG14-Dec-04 4:36
XiAoYanG14-Dec-04 4:36 
Generalparse xml utf-8 &amp; utf-16 Pin
Andrew Tyapuhin12-May-04 1:52
Andrew Tyapuhin12-May-04 1:52 
GeneralRe: parse xml utf-8 &amp; utf-16 Pin
Lymington13-May-04 0:20
Lymington13-May-04 0:20 
GeneralRe: parse xml utf-8 &amp; utf-16 Pin
Lymington3-Jun-04 21:52
Lymington3-Jun-04 21:52 
GeneralRe: parse xml utf-8 &amp; utf-16 Pin
martyconnelly12-Jun-04 7:23
martyconnelly12-Jun-04 7:23 
GeneralRe: parse xml utf-8 &amp; utf-16 Pin
Lymington13-Jun-04 6:49
Lymington13-Jun-04 6:49 
GeneralRe: parse xml utf-8 &amp; utf-16 Pin
Lymington13-Jun-04 6:56
Lymington13-Jun-04 6:56 
GeneralShort, but fine Pin
StHubi18-Mar-04 20:35
StHubi18-Mar-04 20:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.