私はPHPで新しいです。インターンとして、XMLファイルからのデータを使用してMySQLにデータベースを設定する必要があります。問題は、それらのXMLファイルが常に同じフォーマットを持つとは限らないことです。ですから、私はphp5でコードを書いています。特定の子がタグ内に現れた回数をカウントする方法を知りたいと思います。私のXMLファイルでは、子 "ing"がノード "ing-div"内に現れる時間の数を数えたいと思う。 XMLファイルの中には、1つ、2つ、またはそれ以上の "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>
であり、これは、私が「ING-DIV」内「ING」の出現箇所の数をカウントする
$xml = simplexml_load_file($file);
$ing_elem = new SimpleXMLElement($xml);
printf("%d ing.\n", $ing_elem->{'ing-div'}->count());
をしようとしていたコードの一部です。しかし、全く動作しません。
致命的なエラー:メッセージでキャッチされない例外「例外」「文字列XMLとして解析することができませんでした」Cで:\ WAMP \ WWWペソ\フォルダのxml \ \ New_Load_All_Files_1.phpライン上の115
私はエラーメッセージを持っていますannyの提案ですか?
お時間をいただきありがとうございました。
T3000
正しくXMLを読み込んでいますか?あなたが意味するエラーは、単純なXMLはxmlファイルの読み込み/解析ができないことを意味します。これは、 '$ file'がxmlファイルへの有効なファイルパスであることを期待しています。それが正しいと確信していますか?もしそうなら、あなたはXMLが有効であると確信していますか? – bejonbee