0
我有下面的XML:獲取自定義的XML的子元素屬性
<?xml version="1.0" encoding="UTF-8"?>
<Entries>
<Category name="Gallery Wrap">
<Entry>
<Status>Active</Status>
<ProductId>12x18_12framewrapwalnut</ProductId>
<Size>
<Height>12</Height>
<Width>18</Width>
<Depth>12</Depth>
</Size>
<Options>
<Frame cost="86" id="12x18_12framewrapwalnut">Canvas</Frame>
</Options>
<Description>12x18 Walnut Framed Gallery Wrap</Description>
<Weight>.1</Weight>
</Entry>
....
</Category>
</Entries>
我試圖讓該元素的自定義屬性。這是到目前爲止,我的PHP代碼:
$products = simplexml_load_file('product_list.xml');
foreach ($products as $category) {
$attributes = $category->attributes();
echo '<h2>' . $attributes['name'] . '</h2>';
echo '<table>';
echo '<tr><th></th><th>Name</th><th>Your Cost</th><th>Set Price</th></tr>';
foreach ($category->Entry as $product) {
foreach ($product->Options as $option) {
$option_attributes = $option->attributes();
$option_vars = get_object_vars($option);
foreach ($option_vars as $option_name => $option_value) {
echo '<tr><td><input type="checkbox" value="' . $option_attributes->id . '" /></td><td>' . $product->Description . ' - ' . $option_name . '</td><td>' . $option_attributes->cost . '</td><td><input type="text" size="5" value="' . $option_attributes->cost . '" data-cost="' . $option_attributes->cost . '" class="price" /></td></tr>';
}
}
}
echo '</table>';
}
我建立一個形式,這需要他們要包括哪些產品XML數據讓我們的用戶,制定自己的價格(所有這方面我可以處理之後,就可以提取「成本」和「ID」屬性)。
是一個文件或字符串,反正你會想,除非你就像使用它 –
是您的XML切斷使用SimpleXML的?費用在哪裏?請包含一個更完整的版本以供再現。 – Parfait
我用simplexml_load_file()添加了行,很抱歉讓它關閉。 XML的其餘部分只是更多元素而且是完全冗餘的。 「成本」(在這個例子中它是「86」)在元素以及「id」 –
sccr410