How to count the number of occurences of a child within a tag?
<p>i am new in php. As an intern, I have to populate a database in MySQL using data from XML files. The problem is that those XML files do not always have the same format. So, i am writing a code in php5 and I would like to know how to count the number of occurences a specific child appear within a tag. In my XML file, i want to count the number of time the child "ing" appears within the node "ing-div". Some XML files can have one, two, or more "ing-div" node. Any Help?</p>

<p>here is the XML file</p>

<pre><code>&lt;?xml version="1.0" encoding="UTF-8" ?&gt; 
- &lt;recipeml version="0.5"&gt;
- &lt;recipe&gt;
- &lt;head&gt;
  &lt;title&gt;100 Whole Wheat Bread for Bread Machine&lt;/title&gt; 
- &lt;categories&gt;
  &lt;cat&gt;Breadmaker&lt;/cat&gt; 
  &lt;cat&gt;Breads&lt;/cat&gt; 
  &lt;/categories&gt;
  &lt;yield&gt;8&lt;/yield&gt; 
  &lt;/head&gt;
- &lt;ingredients&gt;
- &lt;ing-div&gt;
  &lt;title&gt;REGULAR LOAF&lt;/title&gt; 
- &lt;ing&gt;
- &lt;amt&gt;
  &lt;qty&gt;1&lt;/qty&gt; 
  &lt;unit&gt;cup&lt;/unit&gt; 
  &lt;/amt&gt;
  &lt;item&gt;Water&lt;/item&gt; 
  &lt;/ing&gt;
- &lt;ing&gt;
- &lt;amt&gt;
  &lt;qty&gt;2 1/2&lt;/qty&gt; 
  &lt;unit&gt;cups&lt;/unit&gt; 
  &lt;/amt&gt;
  &lt;item&gt;Wheat bread flour&lt;/item&gt; 
  &lt;/ing&gt;
- &lt;ing&gt;
- &lt;amt&gt;
  &lt;qty&gt;1 1/4&lt;/qty&gt; 
  &lt;unit&gt;tablespoons&lt;/unit&gt; 
  &lt;/amt&gt;
  &lt;item&gt;Dry milk&lt;/item&gt; 
  &lt;/ing&gt;
- &lt;ing&gt;
- &lt;amt&gt;
  &lt;qty&gt;1&lt;/qty&gt; 
  &lt;unit&gt;teaspoon&lt;/unit&gt; 
  &lt;/amt&gt;
  &lt;item&gt;Salt&lt;/item&gt; 
  &lt;/ing&gt;
- &lt;ing&gt;
- &lt;amt&gt;
  &lt;qty&gt;1 1/2&lt;/qty&gt; 
  &lt;unit&gt;tablespoons&lt;/unit&gt; 
  &lt;/amt&gt;
  &lt;item&gt;Butter&lt;/item&gt; 
  &lt;/ing&gt;
- &lt;ing&gt;
- &lt;amt&gt;
  &lt;qty&gt;1 1/4&lt;/qty&gt; 
  &lt;unit&gt;tablespoons&lt;/unit&gt; 
  &lt;/amt&gt;
  &lt;item&gt;Honey&lt;/item&gt; 
  &lt;/ing&gt;
- &lt;ing&gt;
- &lt;amt&gt;
  &lt;qty&gt;1&lt;/qty&gt; 
  &lt;unit&gt;tablespoon&lt;/unit&gt; 
  &lt;/amt&gt;
  &lt;item&gt;Gluten&lt;/item&gt; 
  &lt;/ing&gt;
- &lt;ing&gt;
- &lt;amt&gt;
  &lt;qty&gt;2&lt;/qty&gt; 
  &lt;unit&gt;teaspoons&lt;/unit&gt; 
  &lt;/amt&gt;
  &lt;item&gt;Molasses&lt;/item&gt; 
  &lt;/ing&gt;
- &lt;ing&gt;
- &lt;amt&gt;
  &lt;qty&gt;1 1/2&lt;/qty&gt; 
  &lt;unit&gt;teaspoons&lt;/unit&gt; 
  &lt;/amt&gt;
  &lt;item&gt;Fast-Rise yeast *** OR ***&lt;/item&gt; 
  &lt;/ing&gt;
- &lt;ing&gt;
- &lt;amt&gt;
  &lt;qty&gt;2&lt;/qty&gt; 
  &lt;unit&gt;teaspoons&lt;/unit&gt; 
  &lt;/amt&gt;
  &lt;item&gt;Active-Dry yeast&lt;/item&gt; 
  &lt;/ing&gt;
  &lt;/ing-div&gt;
- &lt;ing-div&gt;
  &lt;title&gt;LARGE LOAF&lt;/title&gt; 
- &lt;ing&gt;
- &lt;amt&gt;
  &lt;qty&gt;1 1/2&lt;/qty&gt; 
  &lt;unit&gt;cups&lt;/unit&gt; 
  &lt;/amt&gt;
  &lt;item&gt;+ 2 tb Water&lt;/item&gt; 
  &lt;/ing&gt;
- &lt;ing&gt;
- &lt;amt&gt;
  &lt;qty&gt;3 3/4&lt;/qty&gt; 
  &lt;unit&gt;cups&lt;/unit&gt; 
  &lt;/amt&gt;
  &lt;item&gt;Wheat bread flour&lt;/item&gt; 
  &lt;/ing&gt;
- &lt;ing&gt;
- &lt;amt&gt;
  &lt;qty&gt;2&lt;/qty&gt; 
  &lt;unit&gt;tablespoons&lt;/unit&gt; 
  &lt;/amt&gt;
  &lt;item&gt;Dry milk&lt;/item&gt; 
  &lt;/ing&gt;
- &lt;ing&gt;
- &lt;amt&gt;
  &lt;qty&gt;1 1/2&lt;/qty&gt; 
  &lt;unit&gt;teaspoons&lt;/unit&gt; 
  &lt;/amt&gt;
  &lt;item&gt;Salt&lt;/item&gt; 
  &lt;/ing&gt;
- &lt;ing&gt;
- &lt;amt&gt;
  &lt;qty&gt;2&lt;/qty&gt; 
  &lt;unit&gt;tablespoons&lt;/unit&gt; 
  &lt;/amt&gt;
  &lt;item&gt;Butter&lt;/item&gt; 
  &lt;/ing&gt;
- &lt;ing&gt;
- &lt;amt&gt;
  &lt;qty&gt;2&lt;/qty&gt; 
  &lt;unit&gt;tablespoons&lt;/unit&gt; 
  &lt;/amt&gt;
  &lt;item&gt;Honey&lt;/item&gt; 
  &lt;/ing&gt;
- &lt;ing&gt;
- &lt;amt&gt;
  &lt;qty&gt;1 1/2&lt;/qty&gt; 
  &lt;unit&gt;tablespoons&lt;/unit&gt; 
  &lt;/amt&gt;
  &lt;item&gt;Gluten&lt;/item&gt; 
  &lt;/ing&gt;
- &lt;ing&gt;
- &lt;amt&gt;
  &lt;qty&gt;1&lt;/qty&gt; 
  &lt;unit&gt;tablespoon&lt;/unit&gt; 
  &lt;/amt&gt;
  &lt;item&gt;Molasses&lt;/item&gt; 
  &lt;/ing&gt;
- &lt;ing&gt;
- &lt;amt&gt;
  &lt;qty&gt;2 1/8&lt;/qty&gt; 
  &lt;unit&gt;teaspoons&lt;/unit&gt; 
  &lt;/amt&gt;
  &lt;item&gt;Fast-Rise yeast *** OR ***&lt;/item&gt; 
  &lt;/ing&gt;
- &lt;ing&gt;
- &lt;amt&gt;
  &lt;qty&gt;3&lt;/qty&gt; 
  &lt;unit&gt;teaspoons&lt;/unit&gt; 
  &lt;/amt&gt;
  &lt;item&gt;Active-Dry yeast&lt;/item&gt; 
  &lt;/ing&gt;
  &lt;/ing-div&gt;
  &lt;/ingredients&gt;
- &lt;directions&gt;
  &lt;step&gt;The trick to making 100% whole wheat bread in your machine is an extra knead, which gives the yeast and gluten a second chance to create a lighter loaf. When your first knead cycle is completed, simply reset the machine and start again. Some manufacturers produce home bakeries with a whole wheat cycle; if your machine doesn't have one, this start- again method works as an easy alternative. SUCCESS HINTS: The gluten gives the whole wheat flour the structure necessary for a good loaf. If your market doesn't stock wheat gluten, try your local health food store. Remember the extra knead. It's especially important in 100% whole wheat bread. Because of the extra knead, us this recipe only on the regular bake cycle. CALORIES: 125 PROTEIN: 14% CHOLESTEROL: 3.98mg CARBOHYDRATES: 73% SODIUM: 218mg FAT: 13% Posted to Bakery-Shoppe Digest V1 #410 by serge.cyr@sympatico.ca (Serge Cyr) on Nov 23, 1997&lt;/step&gt; 
  &lt;/directions&gt;
  &lt;/recipe&gt;
  &lt;/recipeml&gt; 
</code></pre>

<p>and this is part of the code where I am trying to count the number of occurences of "ing" within "ing-div"</p>

<pre><code>$xml = simplexml_load_file($file);
$ing_elem = new SimpleXMLElement($xml);
printf("%d ing.\n", $ing_elem-&gt;{'ing-div'}-&gt;count() ); 
</code></pre>

<p>but, it does not work at all. I have the error message:</p>

<p>Fatal error: Uncaught exception 'Exception' with message 'String could not be parsed as XML' in C:\wamp\www\PhP\Folder xml\New_Load_All_Files_1.php on line 115</p>

<p>anny suggestions?</p>

<p>thank you so much for your time and your help.</p>

<p>T3000</p>
