1 回答
TA贡献1853条经验 获得超9个赞
你可以只使用 SimpleXML 来做到这一点:
$xml = simplexml_load_file('data.xml');
foreach ($xml->children() as $product) {
if ($product->getName() == 'car') {
carFunc($product->title, $product->price, $product->model, $product->description);
}
if ($product->getName() == 'bike') {
bikeFunc($product->title, $product->price, $product->description);
}
if ($product->getName() == 'wheels') {
wheelFunc($product->title, $product->price, $product->description);
}
}
function carFunc($title, $price, $model, $description) {
echo "Car: $title: It's a $model selling for $price. In detail: $description\n";
}
function bikeFunc($title, $price, $description) {
echo "Bike: $title: It sells for $price. In detail: $description\n";
}
function wheelFunc($title, $price, $description) {
echo "Wheel: $title: It sells for $price. In detail: $description\n";
}
输出(对于您的示例 XML)
Car: This is car: It's a 2018 selling for 44.95. In detail: An in-depth look at creating applications with XML.
Bike: this is bike: It sells for 33.58. In detail: Just the dummy description
Wheel: this is wheel: It sells for 33.58. In detail: Just the dummy description
Bike: this is bike: It sells for 33.58. In detail: Just the dummy description
- 1 回答
- 0 关注
- 130 浏览
添加回答
举报