Solved

PHP SimpleXML count specific nodes

Posted on 2007-03-19
3
5,258 Views
Last Modified: 2011-09-20
How do I count the number of "field" nodes in a XML document with SimpleXML ?

PHP Version 5.1.2, WindowsXP SP 2, Apache 2

I've tried with PHP count and Xpath count but I can't get either to work.

Here is my test code.

*** Sample PHP Code ***
<?php
$string = <<<XML
<?xml version="1.0" encoding="iso-8859-1"?>
<importschema name="import_filter.xml" delimiter="~">
      <field name="UNIQUE_IDENTIFIER" type="bigint" length="10" import ="true" />
      <field name="AGE" type="tinyint" length="2" import ="true" />
      <field name="TITLE" type="varchar" length="20" import ="true" />
      <field name="SURNAME" type="varchar" length="25" import ="true" />
      <field name="GIVEN" type="varchar" length="30" import ="true" />
      <field name="OCCUPATION" type="varchar" length="35" import ="true" />
</importschema>
XML;

$xml = simplexml_load_string($string);
$field_count = count($xml->field);

$xp = $xml->xpath('count( //field )');

print "Field Count $field_count,  XPath $xp";
0
Comment
Question by:Matthew_Way
  • 2
3 Comments
 
LVL 48

Expert Comment

by:hernst42
ID: 18746818
Does
$xp = count($xml->xpath('//field'));
works for you or does
$i = 0;
foreach ($xml->field as $f) { ++$i; }
echo $i ."\n";
work. The xpath count does not work with simple xml. On my linux with the same php your example works (except the xpath-count)
0
 

Author Comment

by:Matthew_Way
ID: 18746887
Both of your sugestions work okay on my Windows machine.

Any idea why this statement always results in 1 ?

$field_count = count($xml->field);
0
 
LVL 48

Accepted Solution

by:
hernst42 earned 500 total points
ID: 18746898
maybe a problem with the used xml-library on windows or a bug in php, don't know
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now