我是新的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