Solved

PHP SimpleXML count specific nodes

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
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…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

776 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