FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour Problems traversing XML tree when :next() is not :next()
Posts: 107
Joined: Tue Sep 15, 2009 07:52 AM
Problems traversing XML tree when :next() is not :next()
Posted: Wed Apr 28, 2010 01:31 PM
Hi there!

Could someone with experinece in xHarbour and XML help me out? I'v been battling with this short code for days now.

The category comes out nicely but the leaves of the XML tree refuses to show:

with my simple XML structure (forget the rest which is a valid XML structure)
Code (fw): Select all Collapse
<images>
    <category name="Bowling History">
        <image>
            <description>pinsetting cirvca 1932</description>
            <imgsrc>images/album1/1.jpg</imgsrc>
        </image>
        <image>
            <description>Hand score bowler</description>
            <imgsrc>images/album1/10.jpg</imgsrc>
        </image>
    </category>
</images>

Program to (try) to extract structure:

Code (fw): Select all Collapse
#include "fivewin.ch"
#include "hbxml.ch"

function main()
local cFile:="data.xml", cString 
LOCAL oXmlDoc := TXmlDocument():new()
LOCAL oCurrent, oScan, oCatego, oIterator, oImages, oItems

cString:=memoread( cFile )
IF !( len(cString) > 0 )
  MSGWAIT ("XML file unavailable")
  RETURN .F.
ENDIF

oXmlDoc:=TXmlDocument( cString, HBXML_STYLE_NOESCAPE )
if !( oXmlDoc:nError==HBXML_ERROR_NONE )
  Msgwait ("XML file parsing error " + HB_XmlErrorDesc(oXmlDoc:nError))
  RETURN .F.
endif

oScan:=TXmlIteratorScan():New( oXmlDoc:oRoot )
oCatego:=oScan:find( "category" )
if oCatego == NIL
   msgwait ("no categories found")
   return NIL
endif

do while oCatego<>NIL

  if HHasKey( oCatego:aAttributes, "name" )
     ? "category : " + oCatego:aAttributes["name"]
  else
     ? "no category description"  &&this is OK and working for all categories
  endif
  
  oIterator:=TXmlIterator():New( oCatego )
  oImages:=oIterator:find("image")

  do while oImages<>NIL

     oItems:=TXmlIterator():New( oImages )
     oCurrent:=oItems:Next()
     do while oCurrent<>nil
        IF oCurrent:cName<>NIL
           IF alltrim(oCurrent:cName) == "description"
             ? oCurrent:cName  &&passes almost 12 times her but I only have 5 images
             ? oCurrent:cData  &&empty data, but I have a valid description
           ENDIF
           IF alltrim(oCurrent:cName) == "imgscr"
             ? oCurrent:cName  &&no data, but I have one
             ? oCurrent:cData  &&no data, but I have one
           ENDIF
        ENDIF
       
        oCurrent:=oItems:Next()
     enddo

     oImages:=oIterator:next()
  enddo

  oCatego:=oScan:next()
  
enddo

return NIL


The category comes out OK, is in the description and image (leaf) where the problem is. The loop goes though it at least the double+1 image with no description displayed. If I put a counter in the <image> loop I get the number of data rows that there are between each category instead of the <image> </image> tags as I thought (bug? instruction misunderstanding? what's up with the :next()?)

Also tried:

Code (fw): Select all Collapse
    oItems  := TXmlIteratorScan():new( oItems )
    oCurrent:= oItems:find( "description" )
             ? oCurrent:cName
             ? oCurrent:cData
    oCurrent:= oItems:find( "imgscr" )
             ? oCurrent:cName
             ? oCurrent:cData


instead of all the inner loop with almos the same result, and since the loop is traversed at least once for every line between <category> and <category> you have a screen-full of empty lines.

Thanks in advance for any help or enlightment

Emiliano Llano Díaz
Posts: 107
Joined: Tue Sep 15, 2009 07:52 AM
Re: Problems traversing XML tree when :next() is not :next()
Posted: Thu Apr 29, 2010 01:23 PM

Got a little better by changing TXmlIterator with TXmlIteratorScan on the inner-most loops. At least it will loop the correct number of images, and descriptions. Still does not show any data.

Continue the discussion