How to search for a node in an XML-document the easiest way.

  • Follow


This is a multi-part message in MIME format.

------=_NextPart_000_0010_01CACFF8.304C3460
Content-Type: text/plain;
	charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

Hi,

In an older version of xHarbour sources (0.99.60) I found xmltest.prg. =
Part of the code I've listed below.

If somebody would have a simple example of an XML-document and how to =
use the parameters cNode, cAttrib, cValue and cData
with it, I'd greatly appreciate it.

I did get it to work somewhat, when I just filled in a value for cNode, =
but that was about it.

Other or better ideas for searching an XML-document are also =
appreciated.

Thanks,

Gerard

IF cNode !=3D NIL .or. cAttrib !=3D NIL .or. cValue !=3D NIL .or. cData =
!=3D NIL
      Inkey( 0 )
      ? ""
      ? "-----------------"
      ? "Searching for node named", cNode, ",", cAttrib, "=3D", cValue,;
            " with data having", cData
      ? ""

      IF cNode !=3D NIL
         cNode :=3D HB_RegexComp( cNode )
      ENDIF
      IF cAttrib !=3D NIL
         cAttrib :=3D HB_RegexComp( cAttrib )
      ENDIF
      IF cValue !=3D NIL
         cValue :=3D HB_RegexComp( cValue )
      ENDIF
      IF cData !=3D NIL
         cData :=3D HB_RegexComp( cData )
      ENDIF
      xmlNode :=3D xmlDoc:FindFirstRegex( cNode, cAttrib, cValue, cData =
)
      WHILE xmlNode !=3D NIL
         ? "Found node ", xmlNode:Path() , ValToPrg( xmlNode:ToArray() )
         xmlNode :=3D xmlDoc:FindNext()
      ENDDO

   ENDIF

------=_NextPart_000_0010_01CACFF8.304C3460
Content-Type: text/html;
	charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META content=3D"text/html; charset=3Diso-8859-1" =
http-equiv=3DContent-Type>
<META name=3DGENERATOR content=3D"MSHTML 8.00.6001.18876">
<STYLE></STYLE>
</HEAD>
<BODY>
<DIV><FONT size=3D2 face=3DArial>Hi,</FONT></DIV>
<DIV><FONT size=3D2 face=3DArial></FONT>&nbsp;</DIV>
<DIV><FONT size=3D2 face=3DArial>In an older version of xHarbour sources =
(0.99.60) I=20
found xmltest.prg. Part of the code I've listed below.</FONT></DIV>
<DIV><FONT size=3D2 face=3DArial></FONT>&nbsp;</DIV>
<DIV><FONT size=3D2 face=3DArial>If somebody would have a simple example =
of an=20
XML-document and how to use the parameters cNode, cAttrib, cValue and=20
cData</FONT></DIV>
<DIV><FONT size=3D2 face=3DArial>with it, I'd greatly appreciate =
it.</FONT></DIV>
<DIV><FONT size=3D2 face=3DArial></FONT>&nbsp;</DIV>
<DIV><FONT size=3D2 face=3DArial>I did get it to work somewhat, when I =
just filled=20
in a value for cNode, but that was about it.</FONT></DIV>
<DIV><FONT size=3D2 face=3DArial></FONT>&nbsp;</DIV>
<DIV><FONT size=3D2 face=3DArial>Other or better ideas for searching an =
XML-document=20
are also appreciated.</FONT></DIV>
<DIV><FONT size=3D2 face=3DArial></FONT>&nbsp;</DIV>
<DIV><FONT size=3D2 face=3DArial>Thanks,</FONT></DIV>
<DIV><FONT size=3D2 face=3DArial></FONT>&nbsp;</DIV>
<DIV><FONT size=3D2 face=3DArial>Gerard</FONT></DIV>
<DIV><FONT size=3D2 face=3DArial></FONT>&nbsp;</DIV>
<DIV><FONT size=3D2 face=3DArial><EM>IF cNode !=3D NIL .or. cAttrib !=3D =
NIL .or. cValue=20
!=3D NIL .or. cData !=3D NIL<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Inkey( 0=20
)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ? =
""<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ?=20
"-----------------"<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ? "Searching for =
node=20
named", cNode, ",", cAttrib, "=3D",=20
cValue,;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&=
nbsp; "=20
with data having", cData<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ?=20
""</EM></FONT></DIV>
<DIV><FONT size=3D2 face=3DArial><EM></EM></FONT>&nbsp;</DIV>
<DIV><FONT size=3D2 face=3DArial><EM>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; IF =
cNode !=3D=20
NIL<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cNode :=3D =
HB_RegexComp(=20
cNode )<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
ENDIF<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; IF cAttrib !=3D=20
NIL<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cAttrib :=3D =
HB_RegexComp(=20
cAttrib )<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
ENDIF<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; IF cValue !=3D=20
NIL<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cValue :=3D =
HB_RegexComp(=20
cValue )<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
ENDIF<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; IF cData !=3D=20
NIL<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cData :=3D =
HB_RegexComp(=20
cData )<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
ENDIF<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; xmlNode :=3D =
xmlDoc:FindFirstRegex( cNode,=20
cAttrib, cValue, cData )<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; WHILE xmlNode =
!=3D=20
NIL<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ? "Found node ", =

xmlNode:Path() , ValToPrg( xmlNode:ToArray()=20
)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; xmlNode :=3D=20
xmlDoc:FindNext()<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
ENDDO</EM></FONT></DIV>
<DIV><FONT size=3D2 face=3DArial><EM></EM></FONT>&nbsp;</DIV>
<DIV><FONT size=3D2 face=3DArial><EM>&nbsp;&nbsp;=20
ENDIF<BR></EM></FONT></DIV></BODY></HTML>

------=_NextPart_000_0010_01CACFF8.304C3460--

0
Reply Gerard 3/30/2010 9:00:20 AM

Gerard,

On 30 mar., 12:00, "Gerard" <codg...@yahoo.com> wrote:
> Hi,
>
> In an older version of xHarbour sources (0.99.60) I found xmltest.prg. Pa=
rt of the code I've listed below.
>
> If somebody would have a simple example of an XML-document and how to use=
 the parameters cNode, cAttrib, cValue and cData
> with it, I'd greatly appreciate it.
>
> I did get it to work somewhat, when I just filled in a value for cNode, b=
ut that was about it.
>
> Other or better ideas for searching an XML-document are also appreciated.
>
> Thanks,
>
> Gerard
>
> IF cNode !=3D NIL .or. cAttrib !=3D NIL .or. cValue !=3D NIL .or. cData !=
=3D NIL
> =A0 =A0 =A0 Inkey( 0 )
> =A0 =A0 =A0 ? ""
> =A0 =A0 =A0 ? "-----------------"
> =A0 =A0 =A0 ? "Searching for node named", cNode, ",", cAttrib, "=3D", cVa=
lue,;
> =A0 =A0 =A0 =A0 =A0 =A0 " with data having", cData
> =A0 =A0 =A0 ? ""
>
> =A0 =A0 =A0 IF cNode !=3D NIL
> =A0 =A0 =A0 =A0 =A0cNode :=3D HB_RegexComp( cNode )
> =A0 =A0 =A0 ENDIF
> =A0 =A0 =A0 IF cAttrib !=3D NIL
> =A0 =A0 =A0 =A0 =A0cAttrib :=3D HB_RegexComp( cAttrib )
> =A0 =A0 =A0 ENDIF
> =A0 =A0 =A0 IF cValue !=3D NIL
> =A0 =A0 =A0 =A0 =A0cValue :=3D HB_RegexComp( cValue )
> =A0 =A0 =A0 ENDIF
> =A0 =A0 =A0 IF cData !=3D NIL
> =A0 =A0 =A0 =A0 =A0cData :=3D HB_RegexComp( cData )
> =A0 =A0 =A0 ENDIF
> =A0 =A0 =A0 xmlNode :=3D xmlDoc:FindFirstRegex( cNode, cAttrib, cValue, c=
Data )
> =A0 =A0 =A0 WHILE xmlNode !=3D NIL
> =A0 =A0 =A0 =A0 =A0? "Found node ", xmlNode:Path() , ValToPrg( xmlNode:To=
Array() )
> =A0 =A0 =A0 =A0 =A0xmlNode :=3D xmlDoc:FindNext()
> =A0 =A0 =A0 ENDDO
>
> =A0 =A0ENDIF


Below are two samples and the list of the test.xml file.
test.xml file was borrowed from Microsoft's public samples, and added
some data into it.


Ella



////////////////////////////////////////////////////
//   xml1.prg
////////////////////////////////////////////////////

#include "hbxml.ch"

function main()
   local cFile:=3D"test.xml", cString
   local cNote, cDiscount
   local oDoc, oBook, oIterator, oCurrent

   cString:=3Dmemoread( cFile )
   if !( len(cString) > 0 )
      wait "xml file unavailable"
      return NIL
   endif
   oDoc:=3DTXmlDocument( cString, HBXML_STYLE_NOESCAPE )
   if !( oDoc:nError=3D=3DHBXML_ERROR_NONE )
      wait "xml file parsing error " + str(oDoc:nError)
      return NIL
   endif

   oBook:=3DoDoc:findfirst( "book" )
   if oBook =3D=3D NIL
      wait "no books found"
      return NIL
   endif

   do while .t.

      if HHasKey( oBook:aAttributes, "id" )
         ? "book ID : " + oBook:aAttributes["id"]
      else
         ? "no attribute book ID"
      endif

      cNote:=3D""
      cDiscount:=3D""
      oIterator:=3DTXmlIterator():New( oBook )

      do while .t.
         oCurrent:=3DoIterator:Next()
         if oCurrent=3D=3DNIL
            ? "end branch"
            wait "values : "+cNote+" "+cDiscount
            exit
         else
            ? "current tag : " + oCurrent:cName
            if oCurrent:cName=3D=3D"note"
               cNote:=3DoCurrent:cData
            elseif oCurrent:cName=3D=3D"discount"
               cDiscount:=3DoCurrent:cData
            endif
         endif
      enddo

      oBook:=3DoDoc:findnext()
      if oBook =3D=3D NIL
         wait "no more books found"
         exit
      endif

   enddo

return NIL

////////////////////////////////////////////////////
//   xml2.prg
////////////////////////////////////////////////////

#include "hbxml.ch"

function main()
   local cFile:=3D"test.xml", cString
   local cNote, cDiscount
   local oDoc, oScan, oBook, oIterator, oCurrent

   cString:=3Dmemoread( cFile )
   if !( len(cString) > 0 )
      wait "xml file unavailable"
      return NIL
   endif
   oDoc:=3DTXmlDocument( cString, HBXML_STYLE_NOESCAPE )
   if !( oDoc:nError=3D=3DHBXML_ERROR_NONE )
      wait "xml file parsing error " + str(oDoc:nError)
      return NIL
   endif

   oScan:=3DTXmlIteratorScan():New( oDoc:oRoot )
   oBook:=3DoScan:find( "book" )
   if oBook =3D=3D NIL
      wait "no books found"
      return NIL
   endif

   do while .t.

      if HHasKey( oBook:aAttributes, "id" )
         ? "book ID : " + oBook:aAttributes["id"]
      else
         ? "no attribute book ID"
      endif

      cNote:=3D""
      cDiscount:=3D""
      oIterator:=3DTXmlIterator():New( oBook )

      do while .t.
         oCurrent:=3DoIterator:Next()
         if oCurrent=3D=3DNIL
            ? "end branch"
            wait "values : "+cNote+" "+cDiscount
            exit
         else
            ? "current tag : " + oCurrent:cName
            if oCurrent:cName=3D=3D"note"
               cNote:=3DoCurrent:cData
            elseif oCurrent:cName=3D=3D"discount"
               cDiscount:=3DoCurrent:cData
            endif
         endif
      enddo

      oBook:=3DoScan:next()
      if oBook =3D=3D NIL
         wait "no more books found"
         exit
      endif

   enddo

return NIL

////////////////////////////////////////////////////
//   test.xml
////////////////////////////////////////////////////
<?xml version=3D"1.0"?>
<catalog>
   <book id=3D"bk101">
      <author>Gambardella, Matthew</author>
      <title>XML Developer's Guide
         <note>111</note>
      </title>
      <genre>Computer</genre>
      <price>44.95
         <discount>11</discount>
      </price>
      <publish_date>2000-10-01</publish_date>
      <description>An in-depth look at creating applications
      with XML.</description>
   </book>
   <book id=3D"bk102">
      <author>Ralls, Kim</author>
      <title>Midnight Rain
         <note>222</note>
      </title>
      <genre>Fantasy</genre>
      <price>5.95
         <discount>12</discount>
      </price>
      <publish_date>2000-12-16</publish_date>
      <description>A former architect battles corporate zombies,
      an evil sorceress, and her own childhood to become queen
      of the world.</description>
   </book>
   <book id=3D"bk103">
      <author>Corets, Eva</author>
      <title>Maeve Ascendant
         <note>333</note>
      </title>
      <genre>Fantasy</genre>
      <price>5.95
         <discount>13</discount>
      </price>
      <publish_date>2000-11-17</publish_date>
      <description>After the collapse of a nanotechnology
      society in England, the young survivors lay the
      foundation for a new society.</description>
   </book>
   <book id=3D"bk104">
      <author>Corets, Eva</author>
      <title>Oberon's Legacy
         <note>444</note>
      </title>
      <genre>Fantasy</genre>
      <price>5.95
         <discount>14</discount>
      </price>
      <publish_date>2001-03-10</publish_date>
      <description>In post-apocalypse England, the mysterious
      agent known only as Oberon helps to create a new life
      for the inhabitants of London. Sequel to Maeve
      Ascendant.</description>
   </book>
   <book id=3D"bk105">
      <author>Corets, Eva</author>
      <title>The Sundered Grail
         <note>555</note>
      </title>
      <genre>Fantasy</genre>
      <price>5.95
         <discount>15</discount>
      </price>
      <publish_date>2001-09-10</publish_date>
      <description>The two daughters of Maeve, half-sisters,
      battle one another for control of England. Sequel to
      Oberon's Legacy.</description>
   </book>
</catalog>
////////////////////////////////////////////////////////
0
Reply Ella 3/30/2010 12:32:22 PM


Thanks Ella,

I'll give it a try. BTW, did you see my response about opening an 
XML-document through XMLDOM, instead of IE?

Thanks,

Gerard
"Ella" <ella.stern@xharbour.com> schreef in bericht 
news:7c1aaa03-2af7-430a-acbf-7d2b9818291b@g11g2000yqe.googlegroups.com...

Gerard,

On 30 mar., 12:00, "Gerard" <codg...@yahoo.com> wrote:
> Hi,
>
> In an older version of xHarbour sources (0.99.60) I found xmltest.prg. 
> Part of the code I've listed below.
>
> If somebody would have a simple example of an XML-document and how to use 
> the parameters cNode, cAttrib, cValue and cData
> with it, I'd greatly appreciate it.
>
> I did get it to work somewhat, when I just filled in a value for cNode, 
> but that was about it.
>
> Other or better ideas for searching an XML-document are also appreciated.
>
> Thanks,
>
> Gerard
>
> IF cNode != NIL .or. cAttrib != NIL .or. cValue != NIL .or. cData != NIL
> Inkey( 0 )
> ? ""
> ? "-----------------"
> ? "Searching for node named", cNode, ",", cAttrib, "=", cValue,;
> " with data having", cData
> ? ""
>
> IF cNode != NIL
> cNode := HB_RegexComp( cNode )
> ENDIF
> IF cAttrib != NIL
> cAttrib := HB_RegexComp( cAttrib )
> ENDIF
> IF cValue != NIL
> cValue := HB_RegexComp( cValue )
> ENDIF
> IF cData != NIL
> cData := HB_RegexComp( cData )
> ENDIF
> xmlNode := xmlDoc:FindFirstRegex( cNode, cAttrib, cValue, cData )
> WHILE xmlNode != NIL
> ? "Found node ", xmlNode:Path() , ValToPrg( xmlNode:ToArray() )
> xmlNode := xmlDoc:FindNext()
> ENDDO
>
> ENDIF


Below are two samples and the list of the test.xml file.
test.xml file was borrowed from Microsoft's public samples, and added
some data into it.


Ella



////////////////////////////////////////////////////
//   xml1.prg
////////////////////////////////////////////////////

#include "hbxml.ch"

function main()
   local cFile:="test.xml", cString
   local cNote, cDiscount
   local oDoc, oBook, oIterator, oCurrent

   cString:=memoread( cFile )
   if !( len(cString) > 0 )
      wait "xml file unavailable"
      return NIL
   endif
   oDoc:=TXmlDocument( cString, HBXML_STYLE_NOESCAPE )
   if !( oDoc:nError==HBXML_ERROR_NONE )
      wait "xml file parsing error " + str(oDoc:nError)
      return NIL
   endif

   oBook:=oDoc:findfirst( "book" )
   if oBook == NIL
      wait "no books found"
      return NIL
   endif

   do while .t.

      if HHasKey( oBook:aAttributes, "id" )
         ? "book ID : " + oBook:aAttributes["id"]
      else
         ? "no attribute book ID"
      endif

      cNote:=""
      cDiscount:=""
      oIterator:=TXmlIterator():New( oBook )

      do while .t.
         oCurrent:=oIterator:Next()
         if oCurrent==NIL
            ? "end branch"
            wait "values : "+cNote+" "+cDiscount
            exit
         else
            ? "current tag : " + oCurrent:cName
            if oCurrent:cName=="note"
               cNote:=oCurrent:cData
            elseif oCurrent:cName=="discount"
               cDiscount:=oCurrent:cData
            endif
         endif
      enddo

      oBook:=oDoc:findnext()
      if oBook == NIL
         wait "no more books found"
         exit
      endif

   enddo

return NIL

////////////////////////////////////////////////////
//   xml2.prg
////////////////////////////////////////////////////

#include "hbxml.ch"

function main()
   local cFile:="test.xml", cString
   local cNote, cDiscount
   local oDoc, oScan, oBook, oIterator, oCurrent

   cString:=memoread( cFile )
   if !( len(cString) > 0 )
      wait "xml file unavailable"
      return NIL
   endif
   oDoc:=TXmlDocument( cString, HBXML_STYLE_NOESCAPE )
   if !( oDoc:nError==HBXML_ERROR_NONE )
      wait "xml file parsing error " + str(oDoc:nError)
      return NIL
   endif

   oScan:=TXmlIteratorScan():New( oDoc:oRoot )
   oBook:=oScan:find( "book" )
   if oBook == NIL
      wait "no books found"
      return NIL
   endif

   do while .t.

      if HHasKey( oBook:aAttributes, "id" )
         ? "book ID : " + oBook:aAttributes["id"]
      else
         ? "no attribute book ID"
      endif

      cNote:=""
      cDiscount:=""
      oIterator:=TXmlIterator():New( oBook )

      do while .t.
         oCurrent:=oIterator:Next()
         if oCurrent==NIL
            ? "end branch"
            wait "values : "+cNote+" "+cDiscount
            exit
         else
            ? "current tag : " + oCurrent:cName
            if oCurrent:cName=="note"
               cNote:=oCurrent:cData
            elseif oCurrent:cName=="discount"
               cDiscount:=oCurrent:cData
            endif
         endif
      enddo

      oBook:=oScan:next()
      if oBook == NIL
         wait "no more books found"
         exit
      endif

   enddo

return NIL

////////////////////////////////////////////////////
//   test.xml
////////////////////////////////////////////////////
<?xml version="1.0"?>
<catalog>
   <book id="bk101">
      <author>Gambardella, Matthew</author>
      <title>XML Developer's Guide
         <note>111</note>
      </title>
      <genre>Computer</genre>
      <price>44.95
         <discount>11</discount>
      </price>
      <publish_date>2000-10-01</publish_date>
      <description>An in-depth look at creating applications
      with XML.</description>
   </book>
   <book id="bk102">
      <author>Ralls, Kim</author>
      <title>Midnight Rain
         <note>222</note>
      </title>
      <genre>Fantasy</genre>
      <price>5.95
         <discount>12</discount>
      </price>
      <publish_date>2000-12-16</publish_date>
      <description>A former architect battles corporate zombies,
      an evil sorceress, and her own childhood to become queen
      of the world.</description>
   </book>
   <book id="bk103">
      <author>Corets, Eva</author>
      <title>Maeve Ascendant
         <note>333</note>
      </title>
      <genre>Fantasy</genre>
      <price>5.95
         <discount>13</discount>
      </price>
      <publish_date>2000-11-17</publish_date>
      <description>After the collapse of a nanotechnology
      society in England, the young survivors lay the
      foundation for a new society.</description>
   </book>
   <book id="bk104">
      <author>Corets, Eva</author>
      <title>Oberon's Legacy
         <note>444</note>
      </title>
      <genre>Fantasy</genre>
      <price>5.95
         <discount>14</discount>
      </price>
      <publish_date>2001-03-10</publish_date>
      <description>In post-apocalypse England, the mysterious
      agent known only as Oberon helps to create a new life
      for the inhabitants of London. Sequel to Maeve
      Ascendant.</description>
   </book>
   <book id="bk105">
      <author>Corets, Eva</author>
      <title>The Sundered Grail
         <note>555</note>
      </title>
      <genre>Fantasy</genre>
      <price>5.95
         <discount>15</discount>
      </price>
      <publish_date>2001-09-10</publish_date>
      <description>The two daughters of Maeve, half-sisters,
      battle one another for control of England. Sequel to
      Oberon's Legacy.</description>
   </book>
</catalog>
//////////////////////////////////////////////////////// 


0
Reply Gerard 3/30/2010 1:07:49 PM

2 Replies
455 Views

(page loaded in 0.073 seconds)

Similiar Articles:













7/29/2012 11:27:48 AM


Reply: