Web Developer's Virtual Library: Encyclopedia of Web Design Tutorials, Articles and Discussions


WDVL Newsletter

Active Server Pages
JSP/Java Servlets
Microsoft SQL Server
Daily Backup
Dedicated Servers
Streaming Audio/Video
24-hour Support    

jobs.webdeveloper.com

Hiermenus


e-commerce
Partner With Us















Developer Channel
FlashKit.com
JavaScript.com
JavaScriptSource
Developer Jobs
ScriptSearch
StreamingMediaWorld
Web Developer's Journal
Web Developer's Virtual Library
WebDeveloper.com
Webreference
Web Hosts
XMLfiles.com

internet.com
IT
Developer
Internet News
Small Business
Personal Technology

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers


The DOM Model Page 38

March 15, 2002


Now we repeat this same pattern for the remainder of the nodes under Travelpackage that we want to display as HTML. The second XPath statement //Travelpackage[@name="a"]/* finds all the children of Travelpackage where name="a" and stores the result back into $tmpArray.

Again we print out the values to the browser by looping through the array with a while statement:

          $path = xpath_eval($context, 
                        "//Travelpackage[@name=\"$var[$x]\"]/*");
          $tmpArray = $path->nodeset;
          while (list() = each($tmpArray)) {
              $i++;
              echo("<tr><td>");
              echo($tmpArray[$i]->name); 
              echo("</td><td>");
              echo($tmpArray[$i]->content);
              echo("</td></tr>\n");
          }
          $i=0;

The next XPath statement //Travelpackage[@name="a"]/Package/* finds all the children of Package where Travelpackage name = "a". Again the nodes that are found when evaluating the XPath statement are stored in $tmpArray. Rather than looping through the array, we reference the elements of the array directly:

          $path = xpath_eval($context, 
                        "//Travelpackage[@name=\"$var[$x]\"]/Package/*");
          $tmpArray = $path->nodeset;
          echo("<tr><td>");
          echo($tmpArray[0]->name);
          echo("</td><td>");
          echo($tmpArray[0]->content);
          echo("</td></tr>\n");
          echo("<tr><td>");
          echo($tmpArray[1]->name);
          echo("</td><td>");
          echo($tmpArray[1]->content);
          echo("</td></tr>\n");
          echo("<tr><td colspan=2><hr></td></tr>\n");
      }
      ?>
    </table>
  </body>
</html>

If the XML document was more than two conceptual recordsets long then we would have to increase the size of the $var array to account for the different size. Of course this means that we have to know the size of the XML file. If you don't know the size of the XML document or it varies in size then we need to use a sizeof() function on $tmpArray.

This figure shows the output of the above code:

Dom Model

The DOM Model Page 37
Professional PHP4 Programming
The RAX Model Page 39


Up to => Home / Authoring / Languages / PHP / Pro




Jupiter Online Media: internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and Jupiter Online Media

Jupitermedia Corporate Info


Legal Notices, Licensing, & Permissions, Privacy Policy.

Web Hosting | Newsletters | Tech Jobs | Shopping | E-mail Offers