Delphi XML traversing -


i new delphi.

i wanted find authors under each subject.

here xml

<?xml version="1.0"?> <catalog>    <subject id="computer">        <book id="bk101">                                                                       <author>gambardella, matthew</author>                                             <title>xml developer's guide</title>                                              <genre>computer</genre>                                                           <price>44.95</price>                                                              <publish_date>2000-10-01</publish_date>                                           <description>an in-depth @ creating applications                            xml.</description>                                                        </book>                                                                           <book id="bk112">                                                                    <author>galos, mike</author>                                                      <title>visual studio 7: comprehensive guide</title>                             <genre>computer</genre>                                                           <price>49.95</price>                                                              <publish_date>2001-04-16</publish_date>                                           <description>microsoft visual studio 7 explored in depth,                      looking @ how visual basic, visual c++, c#, , asp+                         integrated comprehensive development                                       environment.</description>                                                     </book>                                                                       </subject>    <subject id="literature">        <book id="bk102">                                                                    <author>ralls, kim</author>                                                       <title>midnight rain</title>                                                      <genre>fantasy</genre>                                                            <price>5.95</price>                                                               <publish_date>2000-12-16</publish_date>                                           <description>a former architect battles corporate zombies,                        evil sorceress, , own childhood become queen                          of world.</description>                                                    </book>                                                                           <book id="bk103">                                                                    <author>corets, eva</author>                                                      <title>maeve ascendant</title>                                                    <genre>fantasy</genre>                                                            <price>5.95</price>                                                               <publish_date>2000-11-17</publish_date>                                           <description>after collapse of nanotechnology                               society in england, young survivors lay                                   foundation new society.</description>                                    </book>                                                                           <book id="bk104">                                                                    <author>corets, eva</author>                                                      <title>oberon's legacy</title>                                                    <genre>fantasy</genre>                                                            <price>5.95</price>                                                               <publish_date>2001-03-10</publish_date>                                           <description>in post-apocalypse england, mysterious                           agent known oberon helps create new life                             inhabitants of london. sequel maeve                                    ascendant.</description>                                                       </book>                                                                           <book id="bk105">                                                                    <author>corets, eva</author>                                                      <title>the sundered grail</title>                                                 <genre>fantasy</genre>                                                            <price>5.95</price>                                                               <publish_date>2001-09-10</publish_date>                                           <description>the 2 daughters of maeve, half-sisters,                            battle 1 control of england. sequel                              oberon's legacy.</description>                                                 </book>                                                                           <book id="bk106">                                                                    <author>randall, cynthia</author>                                                 <title>lover birds</title>                                                        <genre>romance</genre>                                                            <price>4.95</price>                                                               <publish_date>2000-09-02</publish_date>                                           <description>when carla meets paul @ ornithology                              conference, tempers fly feathers ruffled.</description>                 </book>                                                                           <book id="bk107">                                                                    <author>thurman, paula</author>                                                   <title>splish splash</title>                                                      <genre>romance</genre>                                                            <price>4.95</price>                                                               <publish_date>2000-11-02</publish_date>                                           <description>a deep sea diver finds true love twenty                              thousand leagues beneath sea.</description>                                </book>                                                                           <book id="bk108">                                                                    <author>knorr, stefan</author>                                                    <title>creepy crawlies</title>                                                    <genre>horror</genre>                                                             <price>4.95</price>                                                               <publish_date>2000-12-06</publish_date>                                           <description>an anthology of horror stories roaches,                        centipedes, scorpions  , other insects.</description>                        </book>                                                                           <book id="bk109">                                                                    <author>kress, peter</author>                                                     <title>paradox lost</title>                                                       <genre>science fiction</genre>                                                    <price>6.95</price>                                                               <publish_date>2000-11-02</publish_date>                                           <description>after inadvertant trip through heisenberg                       uncertainty device, james salway discovers problems                           of being quantum.</description>                                                </book>                                                                           <book id="bk110">                                                                    <author>o'brien, tim</author>                                                     <title>microsoft .net: programming bible</title>                              <genre>computer</genre>                                                           <price>36.95</price>                                                              <publish_date>2000-12-09</publish_date>                                           <description>microsoft's .net initiative explored in                           detail in deep programmer's reference.</description>                      </book>                                                                           <book id="bk111">                                                                    <author>o'brien, tim</author>                                                     <title>msxml3: comprehensive guide</title>                                      <genre>computer</genre>                                                           <price>36.95</price>                                                              <publish_date>2000-12-01</publish_date>                                           <description>the microsoft msxml3 parser covered in                            detail, attention xml dom interfaces, xslt processing,                    sax , more.</description>                                                    </book>                                                                       </subject> </catalog> 

you can use xpath select authors, using expression //catalog/subject/book/author, delphi have lot of options manage xml file, in case easy use microsoft xml dom implementation

check sample application

{$apptype console}  uses   activex,   variants,   comobj,   sysutils;  procedure readxmlfile(const filename:tfilename); const   msxml2_domdocument='msxml2.domdocument.6.0'; var   xmldoc         : olevariant;   nodes          : olevariant;   lnodes         : integer;                : integer; begin   //create instance xml dom    xmldoc       := createoleobject(msxml2_domdocument);   try     xmldoc.async := false;     //load file     xmldoc.load(filename);     //set xpath mode     xmldoc.setproperty('selectionlanguage','xpath');     //check errors in xml file       if (xmldoc.parseerror.errorcode <> 0)        raise exception.createfmt('error in xml data %s',[xmldoc.parseerror]);      //select nodes match expression     nodes := xmldoc.selectnodes('//catalog/subject/book/author');     //get number of nodes selected     lnodes:= nodes.length;     writeln(format('%d authors found',[lnodes]));     //traverse nodes      i:=0 nodes.length - 1       writeln(format('author name %s',[nodes.item(i).text]));       xmldoc :=unassigned;   end; end;   begin  try     coinitialize(nil);     try       readxmlfile(extractfilepath(paramstr(0))+'test.xml');           couninitialize;     end;  except     on e:exception         writeln(e.classname, ':', e.message);  end;  writeln('press enter exit');  readln;       end. 

finally application return this

12 authors found author name gambardella, matthew author name galos, mike author name ralls, kim author name corets, eva author name corets, eva author name corets, eva author name randall, cynthia author name thurman, paula author name knorr, stefan author name kress, peter author name o'brien, tim author name o'brien, tim 

Comments

Popular posts from this blog

c# - how to write client side events functions for the combobox items -

exception - Python, pyPdf OCR error: pyPdf.utils.PdfReadError: EOF marker not found -