473,387 Members | 1,464 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Count Elements in XML using PHP

16
I have a XML file that is automaticaly generated from a database. It contains variable numbers of <product>.....</product> element.
How can I count number of "<product>.....</product>" elements in php?

Thanks
Aug 28 '08 #1
3 18401
Dormilich
8,658 Expert Mod 8TB
that depends on which php version you are using. with php5 there might be a hint in the DomDocument definition on www.php.net, this could reduce it to something like getElementsByTagName().length (javascript).

Nevertheless, there is a count() function in XSL:
Expand|Select|Wrap|Line Numbers
  1. <xsl:value-of select="count(//product)" />
would output the number of all <product> elements. You can certainly process such a stylesheet with php and you get your count number.

regards
Aug 28 '08 #2
Atli
5,058 Expert 4TB
You could of course always try to use a regular expression. The preg_match_all function will search for a given pattern and return the number of matches it finds.
Aug 28 '08 #3
ak1dnar
1,584 Expert 1GB
I think there is a easy way for you. Have you ever tried DOMDocument

[PHP]$xml= "products.xml";
$xmlDoc = new DOMDocument();
$xmlDoc->load($xml);
$product = $xmlDoc->getElementsByTagName("product");
$numOfproducts = $product->length;
echo $numOfproducts;[/PHP]

This will print the output as 3 for your xml file in In this thread :D

Good luck!
Aug 29 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

12
by: Frank Stephan | last post by:
Hi, im using Struts with nested tags for form validation. So I end up with form elements named like this risikenManuell.praemieNeu risikenManuell.praemieNeu .... I would like to do a...
9
by: Rafal Konopka | last post by:
How can you caount the number of occurrences of elements in an array? For example var arr1 = ; how can I count how many 1s, 2s, 3s, etc.? Despite the example, I never actually know what...
3
by: thomasp | last post by:
If I have an array declared as Dim aryTargets(99) as integer, can I get the count of how many of the elements actually are filled without looping through the array and testing the value of each...
4
by: ralf321 | last post by:
Hello! i want to count elements in xslt. All with meta=13 contains a AND meta 14 contains b but it dont work any Idee why? Thanks. i try it with count(object/node/data)
3
by: Henrik Goldman | last post by:
I have a map structure containing the following information: ->first contains a time_t and is used for lookups ->second holds my associated data to this time When calculating averages over...
0
by: harsha1305 | last post by:
Hi all, I need to create a pointer to array of structure. Definition of structure: typedef struct { char b_name; unsigned long int sig; unsigned long int count; volatile unsigned char...
0
MarkoKlacar
by: MarkoKlacar | last post by:
Hi! I have a problem, I want to select the number of elements within an element. All I need is the number (count), and I just want to it once for "one row" so to speak. Picture this XML file:...
1
by: Edsel | last post by:
Hi all, Can anyone help with this? I would like to extract the attributes from a XML node and then transform them back to XML as elements using XSLT? <unsorted> <office...
1
by: sony.m.2007 | last post by:
Hi, I have a XML file like below <?xml version="1.0" encoding="utf-8" ?> <details> <detail> <description>name</description> <t1>10</t1> <t1>100</t1> <t1>1000</t1>
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.