10/10/2018, 10:47
php-Lấy giá trị từ 1 chuỗi XML
Chào các bạn,
Mình có 1 chuỗi xml như sau: ví dụ $a=
<?xml version="1.0" encoding="utf-16"?>
<Transaction xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<ID>11</ID>
<Buyer_amount>50000.00</Buyer_amount>
<Buyer_currency>VND</Buyer_currency>
<Terminal_ID>35</Terminal_ID>
</Transaction>
Mình muốn lấy giá trị của <Buyer_amount>
Mình phải viết thế nào trong PHP để lấy được giá trị <Buyer_amount> = 50000.00
Các bạn giúp mình với,
cám ơn nhiều
Mình có 1 chuỗi xml như sau: ví dụ $a=
<?xml version="1.0" encoding="utf-16"?>
<Transaction xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<ID>11</ID>
<Buyer_amount>50000.00</Buyer_amount>
<Buyer_currency>VND</Buyer_currency>
<Terminal_ID>35</Terminal_ID>
</Transaction>
Mình muốn lấy giá trị của <Buyer_amount>
Mình phải viết thế nào trong PHP để lấy được giá trị <Buyer_amount> = 50000.00
Các bạn giúp mình với,
cám ơn nhiều
Bài liên quan
<?php
$s = simplexml_load_file('19space.xml');
$myVar="19W4";
$products = $s->xpath('//product'); //search for all "product" elements
foreach($products as $product) //go through list of all products
{
//if the type of $product matches user input (ie $myVar)
if ($product->type == $myVar)
{
echo "Found {$product->type} <br />";
}
}
?>
Call to a member function xpath() on a non-object
$Buyer_amounts = $a->xpath('Transaction/Buyer_amount');
foreach($Buyer_amounts as $Buyer_amount)
{
echo $Buyer_amount.PHP_EOL;
}
2 dòng
<?php
/**
* @author Joomlacoders
* @copyright 2010
*/
unset($xml);
$xml=simplexml_load_file("demo.xml");
echo $xml->Buyer_amount;
?>
echo $a->Buyer_amount;
hay
$xml = simplexml_load_string($a);
echo $xml;
echo $xml->Transaction->Buyer_amount;
đều ko thấy ra gì cả
file demo.xml
<Transaction xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<ID>11</ID>
<Buyer_amount>50000.00</Buyer_amount>
<Buyer_currency>VND</Buyer_currency>
<Terminal_ID>35</Terminal_ID>
</Transaction>
<?php
/**
* @author Joomlacoders
* @copyright 2010
*/
unset($xml);
$xml=simplexml_load_file("demo.xml");
echo $xml->Buyer_amount;
?>
guide
$xml = simplexml_load_string($a);
echo $xml;
echo $xml->Transaction->Buyer_amount;
echo $xml->Buyer_amount;
ko ra kết quả j hết.
hơn nữa
<?xml version="1.0" encoding="utf-16"?>
<Transaction xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<ID>11</ID>
<Buyer_amount>50000.00</Buyer_amount>
<Buyer_currency>VND</Buyer_currency>
<Terminal_ID>35</Terminal_ID>
</Transaction>
của mình là 1 biến có gt như vậy, ko phải 1 file