I need help to parse XML with many level nodes into PHP as follows:
catalog.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<Catalog>
<Category>
<Name>Biscuit</Name>
<Type>
<Type1>Chocolate Chips</Type1>
<Ingredient>Flour, Chocolate Chips</Ingredient>
<Duration>15 minute Oven Bake</Duration>
</Type>
<Type>
<Type2>Lemon Puff</Type2>
<Ingredient>Flour, Lemon Extract</Ingredient>
<Duration>15 minute Oven Bake</Duration>
</Type>
</Category>
<Category>
<Name>Cake</Name>
<Type>
<Type1>Fruit Cake</Type1>
<Ingredient>Flour, Egg, Fresh Cream, Fruits</Ingredient>
<Duration>30 minute Conventional Oven Bake</Duration>
</Type>
<Type>
<Type2>Mango Cake</Type2>
<Ingredient>Flour, Egg, Fresh Cream, Mango, Nuts</Ingredient>
<Duration>30 minute Oven Bake</Duration>
</Type>
</Category>
</Catalog>
menu.php
<?php
$xml = simplexml_load_file("catalog.xml");
foreach ($xml->xpath('//Category') as $category)
{
echo $category->Name;
foreach($xml->xpath('//Site') as $site)
{
echo $site->ID;
}
}
?>
Something is not right in my php which I tried to solve going through examples but still having problem. Sorry this is my first php coding project. Thanks in advance to many experts.
Please have a read of http://devzone.zend.com/240/simplexml/
example:
<?php
// $xml = simplexml_load_file("catalog.xml");
$catalog = new SimpleXMLElement(data());
foreach ( $catalog->Category as $category ) {
echo 'Name: ', $category->Name, "
";
foreach( $category->Type as $type ) {
if ( isset($type->Type1) ) {
echo " type1: ", $type->Type1, "
";
}
else if ( isset($type->Type2) ) {
echo " type2: ", $type->Type2, "
";
}
else {
echo " unknown type
";
}
echo ' Ingredient:', $type->Ingredient, "
";
echo ' Duration: ', $type->Duration, "
";
}
}
function data() {
return <<< eox
<?xml version="1.0" encoding="ISO-8859-1"?>
<Catalog>
<Category>
<Name>Biscuit</Name>
<Type>
<Type1>Chocolate Chips</Type1>
<Ingredient>Flour, Chocolate Chips</Ingredient>
<Duration>15 minute Oven Bake</Duration>
</Type>
<Type>
<Type2>Lemon Puff</Type2>
<Ingredient>Flour, Lemon Extract</Ingredient>
<Duration>15 minute Oven Bake</Duration>
</Type>
</Category>
<Category>
<Name>Cake</Name>
<Type>
<Type1>Fruit Cake</Type1>
<Ingredient>Flour, Egg, Fresh Cream, Fruits</Ingredient>
<Duration>30 minute Conventional Oven Bake</Duration>
</Type>
<Type>
<Type2>Mango Cake</Type2>
<Ingredient>Flour, Egg, Fresh Cream, Mango, Nuts</Ingredient>
<Duration>30 minute Oven Bake</Duration>
</Type>
</Category>
</Catalog>
eox;
}
prints
Name: Biscuit
type1: Chocolate Chips
Ingredient:Flour, Chocolate Chips
Duration: 15 minute Oven Bake
type2: Lemon Puff
Ingredient:Flour, Lemon Extract
Duration: 15 minute Oven Bake
Name: Cake
type1: Fruit Cake
Ingredient:Flour, Egg, Fresh Cream, Fruits
Duration: 30 minute Conventional Oven Bake
type2: Mango Cake
Ingredient:Flour, Egg, Fresh Cream, Mango, Nuts
Duration: 30 minute Oven Bake
Try this i have not tested it but you can get a rough idea of what you should do
foreach ($xml as $row_xml){
$category=$row_xml->Category();
$site= $row_xml->site();
}
I didn't read your code, but I have two of code I used to use objects with function to parse the XML, you can refer to the following
Note the use of Chinese, but does not affect you read the code
<?php
//不适合大本地文件跟远程文件
$dom=new DOMDocument();
$dom->load("user.xml");
$persons=$dom->getElementsByTagName("person");
//文本类型:3
/*
$person->item(0)->nodeType;//echo 输出节点类型
$person->item(0)->nodeName;//echo 输出节点名字
$person->item(0)->nodeValue;//echo 输出节点值
*/
//echo $person->item(0)->childNodes->item(0)->childNodes->item(0)->nodeType;
//找下一个的同胞节点
//echo $person->item(0)->childNodes->item(0)->nextSibling->nodeName;
echo '<table border="1" width="500" align="center">';
foreach($persons as $person){
echo '<tr>';
echo '<td>'.$person->getAttribute("id").'</td>';
$usernames=$person->getElementsByTagName("username");
echo '<td>'.$usernames->item(0)->childNodes->item(0)->nodeValue.'</td>';
$phones=$person->getElementsByTagName("phone");
echo '<td>'.$phones->item(0)->childNodes->item(0)->nodeValue.'</td>';
echo '</tr>';
}
//header("Content-type:text/xml");
//echo $dom->saveXML();
<?php
//第一步 创建解析器 xml_parser_create(编码);
$xml = xml_parser_create('utf-8');
//xml_parser_set_option — 为指定 XML 解析进行选项设置
//xml_parser_set_option($xml,XML_OPTION_CASE_FOLDING,false); false原样输出
//注册事件,将遇到开始和结束表计时使用什么函数
xml_set_element_handler($xml,"starttag","endtag");
xml_set_character_data_handler($xml,"content");
function starttag($x,$tagName,$args){
if($tagName=="USERS")
echo "<table border='1' width='800' align='center' ><caption>{$tagName}";
else if($tagName == "USER"){
echo "<tr>";
echo "<td>{$args['ID']}</td>";
}else
echo "<td>";
}
function endtag($x,$tagName){
if($tagName=="USERS")
echo "</caption></table>";
else if($tagName == "USER")
echo "</tr>";
else
echo "</td>";
}
function content($x,$content){
echo $content;
}
$printerror=false;
$xmlfile="user.xml";
//第二步 读取数据
$fp=fopen($xmlfile,"r");
while(!feof($fp)){
$data=fread($fp,1024);
//开始解析
if(!xml_parse($xml,$data)){
$printerror=true;
}
}
//关闭文件
fclose($fp);
if($printerror){
$row=xml_get_current_line_number($xml);
$col=xml_get_current_column_number($xml);
$errormess=xml_error_string(xml_get_error_code($xml));
echo "在文件{$xmlfile}中,[{$row}行,{$col}列]:{$errormess}.";
}
//关闭解析器
xml_parser_free($xml);