?
Solved

PHP SimpleXML count specific nodes

Posted on 2007-03-19
3
Medium Priority
?
5,403 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
[X]
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
  • 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 1500 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

752 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