How to count the number of occurences of a child within a tag?
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?
here is the XML file
<?xml version="1.0" encoding="UTF-8" ?>
- <recipeml version="0.5">
- <recipe>
- <head>
<title>100 Whole Wheat Bread for Bread Machine</title>
- <categories>
<cat>Breadmaker</cat>
<cat>Breads</cat>
</categories>
<yield>8</yield>
</head>
- <ingredients>
- <ing-div>
<title>REGULAR LOAF</title>
- <ing>
- <amt>
<qty>1</qty>
<unit>cup</unit>
</amt>
<item>Water</item>
</ing>
- <ing>
- <amt>
<qty>2 1/2</qty>
<unit>cups</unit>
</amt>
<item>Wheat bread flour</item>
</ing>
- <ing>
- <amt>
<qty>1 1/4</qty>
<unit>tablespoons</unit>
</amt>
<item>Dry milk</item>
</ing>
- <ing>
- <amt>
<qty>1</qty>
<unit>teaspoon</unit>
</amt>
<item>Salt</i开发者_运维问答tem>
</ing>
- <ing>
- <amt>
<qty>1 1/2</qty>
<unit>tablespoons</unit>
</amt>
<item>Butter</item>
</ing>
- <ing>
- <amt>
<qty>1 1/4</qty>
<unit>tablespoons</unit>
</amt>
<item>Honey</item>
</ing>
- <ing>
- <amt>
<qty>1</qty>
<unit>tablespoon</unit>
</amt>
<item>Gluten</item>
</ing>
- <ing>
- <amt>
<qty>2</qty>
<unit>teaspoons</unit>
</amt>
<item>Molasses</item>
</ing>
- <ing>
- <amt>
<qty>1 1/2</qty>
<unit>teaspoons</unit>
</amt>
<item>Fast-Rise yeast *** OR ***</item>
</ing>
- <ing>
- <amt>
<qty>2</qty>
<unit>teaspoons</unit>
</amt>
<item>Active-Dry yeast</item>
</ing>
</ing-div>
- <ing-div>
<title>LARGE LOAF</title>
- <ing>
- <amt>
<qty>1 1/2</qty>
<unit>cups</unit>
</amt>
<item>+ 2 tb Water</item>
</ing>
- <ing>
- <amt>
<qty>3 3/4</qty>
<unit>cups</unit>
</amt>
<item>Wheat bread flour</item>
</ing>
- <ing>
- <amt>
<qty>2</qty>
<unit>tablespoons</unit>
</amt>
<item>Dry milk</item>
</ing>
- <ing>
- <amt>
<qty>1 1/2</qty>
<unit>teaspoons</unit>
</amt>
<item>Salt</item>
</ing>
- <ing>
- <amt>
<qty>2</qty>
<unit>tablespoons</unit>
</amt>
<item>Butter</item>
</ing>
- <ing>
- <amt>
<qty>2</qty>
<unit>tablespoons</unit>
</amt>
<item>Honey</item>
</ing>
- <ing>
- <amt>
<qty>1 1/2</qty>
<unit>tablespoons</unit>
</amt>
<item>Gluten</item>
</ing>
- <ing>
- <amt>
<qty>1</qty>
<unit>tablespoon</unit>
</amt>
<item>Molasses</item>
</ing>
- <ing>
- <amt>
<qty>2 1/8</qty>
<unit>teaspoons</unit>
</amt>
<item>Fast-Rise yeast *** OR ***</item>
</ing>
- <ing>
- <amt>
<qty>3</qty>
<unit>teaspoons</unit>
</amt>
<item>Active-Dry yeast</item>
</ing>
</ing-div>
</ingredients>
- <directions>
<step>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</step>
</directions>
</recipe>
</recipeml>
and this is part of the code where I am trying to count the number of occurences of "ing" within "ing-div"
$xml = simplexml_load_file($file);
$ing_elem = new SimpleXMLElement($xml);
printf("%d ing.\n", $ing_elem->{'ing-div'}->count() );
but, it does not work at all. I have the error message:
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
anny suggestions?
thank you so much for your time and your help.
T3000
$xml = new DOMDocument();
$xml->load(path/to/file);
$xpath = new DOMXPath($xml);
$ingdiv = $xpath->query("/recipeml/recipe/ingredients/ing-div");
$length = $ingdiv->length;
// iterate over all <ing-div> from last to first
while($length--)
{
// number of <ing> in the specific <ing-div>
print $xpath->query("ing", $ingdiv->item($length))->length;
}
Edit:
You should not Copy & Paste the formated output from a browser.
<?xml version="1.0" encoding="UTF-8" ?>
- <recipeml/>
This is malformed XML. XML cannot have text outside the root node.
I don't think you need the following line:
$ing_elem = new SimpleXMLElement($xml);
since you are using simplexml_load_file($file) to create a SimpleXMLElement object
So your simpleXML object can be created only using this line:
$ing_elem = simplexml_load_file($file);
You can use the following code fragment to get more information about errors which might occur:
libxml_use_internal_errors(true);
$ing_elem = simplexml_load_file($file);
if (!$ing_elem) {
echo "Failed loading XML\n";
foreach(libxml_get_errors() as $error) {
echo "\t", $error->message;
}
}
精彩评论