Page 1 of 1

fast and simple xml parsing in XP (+IE7) and VISTA

Posted: Thu Oct 25, 2007 1:20 pm
by bingo
use xmllite.dll :lol:
http://msdn2.microsoft.com/en-us/library/ms752872.aspx

copy xmllite.lib in pb first !
http://kyf.net/tmp/xmllite.zip

here a sample for xp-users .
it read the c:\windows\system32\wmpscheme.xml , make a comment to all "validchildid" nodes in depth 2 and write a new file to c:\wmpscheme_new.xml .
if vista , edit the xml-files and the node ...

Code: Select all

Import "xmllite.lib"
CreateXmlReader(riid.l,ppvObject.l,pMalloc.l)
CreateXmlWriter(riid.l,ppvObject.l,pMalloc.l)
EndImport

Interface IXmlReader Extends IUnknown
SetInput(a)
GetProperty(a,b)
SetProperty(a,b)
Read(a)
GetNodeType(a)
MoveToFirstAttribute()
MoveToNextAttribute()
MoveToAttributeByName(a,b)
MoveToElement()
GetQualifiedName(a,b)
GetNamespaceUri(a,b)
GetLocalName(a,b)
GetPrefix(a,b)
GetValue(a,b)
ReadValueChunk(a,b,c)
GetBaseUri(a,b)
IsDefault()
IsEmptyElement()
GetLineNumber(a)
GetLinePosition(a)
GetAttributeCount(a)
GetDepth(a)
IsEOF()
EndInterface

Interface IXmlWriter Extends IUnknown
SetOutput(a)
GetProperty(a,b)
SetProperty(a,b)
WriteAttributes(a,b)
WriteAttributeString(a,b,c,d)
WriteCData(a)
WriteCharEntity(a)
WriteChars(a,b)
WriteComment(a)
WriteDocType(a,b,c,d)
WriteElementString(a,b,c,d)
WriteEndDocument()
WriteEndElement()
WriteEntityRef(a)
WriteFullEndElement()
WriteName(a)
WriteNmToken(a)
WriteNode(a,b)
WriteNodeShallow(a,b)
WriteProcessingInstruction(a,b)
WriteQualifiedName(a,b)
WriteRaw(a)
WriteRawChars(a,b)
WriteStartDocument(a)
WriteStartElement(a,b,c)
WriteString(a)
WriteSurrogateCharEntity(a,b)
WriteWhitespace(a)
Flush()
EndInterface

Structure STATSTG  
pwcsName.l 
type.l 
cbSize.q 
mtime.FILETIME 
ctime.FILETIME 
atime.FILETIME 
grfMode.f 
grfLocksSupported.l 
clsid.GUID 
grfStateBits.l 
reserved.l 
EndStructure

#STGM_READ = 0
#STGM_WRITE = 1
#STGM_CREATE = $1000

#STATFLAG_NONAME=0

prFileStream.IStream
pwFileStream.IStream
rreader.IXmlReader
wreader.IXmlWriter

Procedure.s Uni2Ansi(unicode.l) 
ProcedureReturn PeekS(UnicodeToMultiByte_(unicode,#CP_ACP))
EndProcedure

Procedure.l Ansi2Uni(ansi.s) 
SHStrDup_(@ansi,@mempoint.l)
ProcedureReturn mempoint 
EndProcedure

If SHCreateStreamOnFile_("c:\windows\system32\wmpscheme.xml",#STGM_READ,@prFileStream) = 0
Else
Debug "error"
End
EndIf

If SHCreateStreamOnFile_("c:\wmpscheme_new.xml",#STGM_WRITE|#STGM_CREATE,@pwFileStream) = 0
Else
Debug "error"
End
EndIf

prFileStream\Stat(st.STATSTG,#STATFLAG_NONAME)

Debug "size of xml: " + Str(st\cbSize) + " bytes"

CreateXmlReader(?IID_IXmlReader,@rreader,0)
CreateXmlWriter(?IID_IXmlWriter,@wreader,0)

rreader\setinput(prFileStream)
wreader\setoutput(pwFileStream)

nodeType.l
linenumber.l
size.l
tmp.l
node.l

While rreader\read(@node) = 0

wreader\WriteNodeShallow(rreader,#True)

Select node

Case 1;XmlNodeType_Element
rreader\GetDepth(@tmp)
If tmp = 2
rreader\GetQualifiedName(@tmp,@size)
If uni2ansi(tmp) = "validchildid"
wreader\WriteComment(ansi2uni("test - - comment - - test"))
EndIf
EndIf

EndSelect

Wend

wreader\WriteEndDocument()
wreader\flush()

DataSection 
IID_IXmlReader: 
Data.l $7279FC81
Data.w $709D,$4095
Data.b $B6,$3D,$69,$FE,$4B,$0D,$90,$30
IID_IXmlWriter:
Data.l $7279FC88
Data.w $709D,$4095
Data.b $B6,$3D,$69,$FE,$4B,$0D,$90,$30
EndDataSection 

End
should be fast ... and simple , understandably 8)

* your exe is small (no pb4 xml lib needed in xp + vista)
* easier as pbosl xml wrapper

use it ! xmllite is present in all vista os and in xp with ie7 ! 8)

Posted: Thu Oct 25, 2007 2:49 pm
by Fred
You forgot the drawbacks: Windows only (and even, XP and Vista), non PB like functions and a some MSDN docs to consult before using it properly ;)

Nice example of interfaces use tough.