我是新的PHP。作为一名实习生,我必须使用XML文件中的数据在MySQL中填充数据库。问题是这些XML文件并不总是具有相同的格式。所以,我在php5中编写了一个代码,我想知道如何计算特定小孩在标签中出现的次数。在我的XML文件中,我想要统计节点“ing-div”中出现子节点“ing”的次数。一些XML文件可以有一个,两个或多个“ing-div”节点。任何帮助?如何计算标签内儿童的出现次数?
这里是XML文件
<?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</item>
</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 [email protected] (Serge Cyr) on Nov 23, 1997</step>
</directions>
</recipe>
</recipeml>
而这正是我试图“荷兰国际集团-DIV”
$xml = simplexml_load_file($file);
$ing_elem = new SimpleXMLElement($xml);
printf("%d ing.\n", $ing_elem->{'ing-div'}->count());
内计数“ing”的的出现次数的数量的部分代码但它根本不起作用。我有错误消息:用C未捕获的异常“异常”与消息“字符串不能被解析为XML”::
致命错误\瓦帕\ WWW \ PHP \文件夹XML \ New_Load_All_Files_1.php上线115
anny建议?
非常感谢你的时间和你的帮助。
T3000
您确定您正在加载xml吗?您收到的错误意味着简单的XML无法读取/解析xml文件。正如它写的那样,它期望'$ file'成为xml文件的有效文件路径。你确定它是正确的吗?如果是这样,你确定xml是有效的吗? – bejonbee 2011-06-16 21:34:43