我是php新手。作为实习生,我必须使用XML文件中的数据填充MySQL中的数据库。问题是那些XML文件并不总是具有相同的格式。所以,我在php5中编写代码,我想知道如何计算特定子项在标记中出现的次数。在我的XML文件中,我想计算子节点“ing”出现在节点“ing-div”中的时间。某些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 serge.cyr@sympatico.ca (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() );
但是,它根本不起作用。我有错误消息:
致命错误:在第115行的C:\ wamp \ www \ PhP \ Folder xml \ New_Load_All_Files_1.php中显示消息'字符串无法解析为XML'的未捕获异常'异常'
anny建议?
非常感谢您的时间和帮助。
T3000
答案 0 :(得分: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;
}
编辑:
你不应该复制&amp;从浏览器粘贴格式化的输出。
<?xml version="1.0" encoding="UTF-8" ?>
- <recipeml/>
这是格式错误的XML。 XML不能在根节点之外有文本。
答案 1 :(得分:0)
我认为您不需要以下内容:
$ing_elem = new SimpleXMLElement($xml);
因为您使用simplexml_load_file($ file)来创建SimpleXMLElement对象
因此,只能使用以下行创建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;
}
}