Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

PHP SimpleXML count specific nodes

Posted on 2007-03-19
3
5,313 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
This article discusses how to create an extensible mechanism for linked drop downs.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

828 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