News:

PlayBASIC2DLL V0.99 Revision I Commercial Edition released! - Convert PlayBASIC programs to super fast Machine Code. 

Main Menu

Parse Properties from a tag (XML)

Started by kevin, June 19, 2008, 02:13:16 AM

Previous topic - Next topic

kevin

 Parse Properties from a tag (XML)

   This example function will pull a group of unknown properties from a XML tag declarlation.  The prop's are returned into a 2d array.  From here you can skim the array and therefore retrieve any required info, or not :)


 

PlayBASIC Code: [Select]
   // Set up a string with the XML styled property fields in it
// The head tag assumed to be the ID tag. The parser function pulls the list
// unknown list of Prop's from the string
s$=""

; have to replace the ' characters ".
s$=replace$(s$,"'",chr$(34),1,false,false)

print s$

// Grab the tags
print "Tags"

; Create the 2d array returned tags will be dropped in
Dim Tags$(2,2)

; Parse out the tags
Count=ParsePropertyFields(s$,Tags$())

; Display the tags
For lp=0 to Count-1
print Tags$(lp,1)+"="+Tags$(lp,2)
next

; display screen
Sync
waitkey
end


// Note This code assumes the parameters are in the following format

Function ParsePropertyFields(ThisString$,FieldProperties$())
t$=trim$(ThisString$," "+chr$(9))
pos=instring(t$," ",1,false)
IF pos>0

BufferSize=10
; Check if the array exists ? - if it does and is smaller, __AVOID___ some
; memory thrashing by not always creating the field properties array!
if GetArrayStatus(FieldProperties$())=true
Size=GetArrayElements(FieldProperties$(),1)
if Size<BufferSize
ReDim FieldProperties$(BufferSize,2)
else
BufferSize=Size
endif
else
ReDim FieldProperties$(BufferSize,2)
endif


LastPos=pos
EqualsPos=Lastpos+1
Repeat
EqualsPos=Instring(t$,"=",EqualsPos,false)
if EqualsPos>LastPos
NameTag$=trim$(mid$(t$,LastPos,EqualsPos-LastPos)," ")
ValueTag$=""
inc EqualsPos
Quote1Pos=Instring(t$,chr$(34),EqualsPos,false)
if Quote1Pos=>EqualsPos
inc Quote1Pos
Quote2Pos=Instring(t$,chr$(34),Quote1Pos,false)
if Quote2Pos>Quote1Pos
ValueTag$=mid$(t$,Quote1Pos,Quote2Pos-Quote1Pos)
LastPos =Quote2pos+1
EqualsPos=LastPos
endif
endif

FieldProperties$(FieldCount,1) =NameTag$
FieldProperties$(FieldCount,2) =ValueTag$
Inc FieldCount
If FieldCount>BufferSize
BufferSize=BufferSize+100
ReDim FieldProperties$(BufferSize,2)
endif

endif
until EqualsPos<1

EndIF
EndFunction FieldCount





thaaks

#1
Nice  ;)

I thought about porting BlitzXML to PB - but I guess the compo entry will already eat up all my spare time.
If anyone thinks about giving it a try:
BlitzXML (removed dead link 2022)

Looks like a simple and pretty useful XML library. And porting from BB to PB shouldn't be too difficult. Although it does use Banks and I'm not 100% sure how to deal with those in PB...

ATLUS

Yea nice many lines for settings XML>ini