<?xml version="1.0"?>
<order>
<order_businessid>2</order_businessid>
<order_categoryid>3</order_categoryid>
<product_id>1</product_id>
<product_name1>CreamCheese Cake</product_name1>
<product_price1>4.10</product_price1>
<product_Qty1>1</product_Qty1>
<product_id>3</product_id>
<product_name2>Gujarati unlimited</product_name2>
<product_price2>50.00</product_price2>
<product_Qty2>1</product_Qty2>
<product_id>5</product_id>
<product_name3>Cheese French Frish</product_name3>
<product_price3>4.10</product_price3>
<product_Qty3>1</product_Qty3>
</order>
我如何阅读这个结构?
我想用PHP输出:
ProduktID 1, has name CreamCheese Cake and it's price is 4.10
ProduktID 3, has name Gujarati unlimited and it's price is 50.00
依旧......
如果所有信息都在一个元素中,我知道如何做到这一点,然后我将循环遍历元素,获取属性和输出。
但有了这个我完全不知道,所以希望你能帮帮我..
答案 0 :(得分:2)
XML的结构非常差 - 让订单中的每个产品都具有正确的结构将使这更容易。
但这有效(使用SimpleXML):
$myxml = '<?xml version="1.0"?>
<order>
<order_businessid>2</order_businessid>
<order_categoryid>3</order_categoryid>
<product_id>1</product_id>
<product_name1>CreamCheese Cake</product_name1>
<product_price1>4.10</product_price1>
<product_Qty1>1</product_Qty1>
<product_id>3</product_id>
<product_name2>Gujarati unlimited</product_name2>
<product_price2>50.00</product_price2>
<product_Qty2>1</product_Qty2>
<product_id>5</product_id>
<product_name3>Cheese French Frish</product_name3>
<product_price3>4.10</product_price3>
<product_Qty3>1</product_Qty3>
</order>';
$xml = simplexml_load_string($myxml);
$count=1;
foreach ($xml->product_id as $prod) {
$pn = $xml->xpath('/order/product_name'.$count);
$price = $xml->xpath('/order/product_price'.$count);
$pq = $xml->xpath('/order/product_Qty'.$count);
echo "ProduktID $prod, has name $pn[0] and it's price is $price[0] and exists $pq[0] time(s)".PHP_EOL;
$count++;
}
答案 1 :(得分:1)
这将解决您的问题,但我不喜欢这种XML格式
$xmlStr = '<?xml version="1.0"?>
<order>
<order_businessid>2</order_businessid>
<order_categoryid>3</order_categoryid>
<product_id>1</product_id>
<product_name1>CreamCheese Cake</product_name1>
<product_price1>4.10</product_price1>
<product_Qty1>1</product_Qty1>
<product_id>3</product_id>
<product_name2>Gujarati unlimited</product_name2>
<product_price2>50.00</product_price2>
<product_Qty2>1</product_Qty2>
<product_id>5</product_id>
<product_name3>Cheese French Frish</product_name3>
<product_price3>4.10</product_price3>
<product_Qty3>1</product_Qty3>
</order>';
$xml = simplexml_load_string($xmlStr);
$pid = $pnam = $ppric = '';
foreach($xml as $ky=>$node) {
$pid = ( $ky == 'product_id') ? $node[0] : $pid ;
$pnam = (strpos($ky, 'product_name') === 0) ? $node[0] : $pnam;
$ppric = (strpos($ky, 'product_price') === 0) ? $node[0] : $ppric;
if( strpos($ky, 'product_price') === 0) {
echo "product id $pid has name $pnam and its price is $ppric <br>";
}
}
答案 2 :(得分:0)
有几种方法可以处理这种废话。我更喜欢正则表达式:
这个应符合您的要求:
/.*?<product_id>(.*?)<\/product_id>.*?<product_name\d*>(.*?)<\/product_name\d*>.*?<product_price\d*>(.*?)<\/product_price\d*>.*?<product_Qty\d*>(.*?)<\/product_Qty\d*>.*/s
和
ProduktID $1, has name $2 and it's price is $3 and exists $4 time(s).\n
这应该与preg_match_all一起使用,但它未经测试!
祝你好运!
答案 3 :(得分:0)
以下是使用DOMDocument
的解决方案:
$doc = new DOMDocument();
$doc->load("order.xml");
$productList = array();
foreach($doc->documentElement->childNodes as $node) {
if($node->tagName == "product_id") {
$productList[] = array(
"id" => $node->textContent,
);
}
if(preg_match("/product_([A-Za-z_]+)(\d+)/", $node->tagName, $m)) {
$productList[$m[2] - 1][$m[1]] = $node->textContent;
}
}
foreach($productList as $product) {
echo "ProduktID ", $product["id"]
, ", has name ", $product["name"]
, " and it's price is ", $product["price"]
, " with quantity ", $product["Qty"]
, "\n";
}
当然,那个XML看起来很奇怪(所以我分享@Armin的情绪)。它看起来像是“手工制作”而没有那些花哨的XML工具/库,而是使用字符串连接或仅使用echo
进行渲染。