2011-06-16 9 views
0

私は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

+1

正しくXMLを読み込んでいますか?あなたが意味するエラーは、単純なXMLはxmlファイルの読み込み/解析ができないことを意味します。これは、 '$ file'がxmlファイルへの有効なファイルパスであることを期待しています。それが正しいと確信していますか?もしそうなら、あなたはXMLが有効であると確信していますか? – bejonbee

答えて

0
$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; 
} 

編集:

あなたは、ブラウザからのフォーマットされた出力を貼り付け&コピーしてはいけません。

<?xml version="1.0" encoding="UTF-8" ?> 
- <recipeml/> 

これは不正な形式のXMLです。 XMLはルートノードの外側にテキストを持つことはできません。

0

私はあなたが次の行必要とは思わない:

$ing_elem = new SimpleXMLElement($xml); 

あなたがたSimpleXMLElementオブジェクト

を作成するためにsimplexml_load_file($ファイル)を使用しているので、だからあなたのSimpleXMLオブジェクトのみが、これを使用して作成することができますライン:

$ing_elem = simplexml_load_file($file); 

あなたが発生する可能性があるエラーに関するより多くの情報を取得するために次のコードを使用することができます。

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; 
    } 
} 
+0

しかし、もし$ ide_elem = new SimpleXMLElement($ xml)という行をideleteすれば;変数$ ing_elemはもう定義されないので、次の行に問題があります。 – T3000

関連する問題