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
vnntech.com viết 12:52 ngày 10/10/2018
Tham khảo ví dụ này nhé bác:


<?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 />";
}
}
?>
sulico16 viết 13:02 ngày 10/10/2018
Mình thử đoạn code này nhưng toàn bị lỗi
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;
}
Profit viết 12:51 ngày 10/10/2018
Oạch gì mà dài thế
2 dòng
PHP Code:
<?php
/**
 * @author Joomlacoders
 * @copyright 2010
 */
unset($xml);
$xml=simplexml_load_file("demo.xml");
echo 
$xml->Buyer_amount;
?>
sulico16 viết 12:55 ngày 10/10/2018
Mình

echo $a->Buyer_amount;

hay

$xml = simplexml_load_string($a);
echo $xml;
echo $xml->Transaction->Buyer_amount;

đều ko thấy ra gì cả
Profit viết 12:52 ngày 10/10/2018
Thôi sao mà dốt quá vậy như vậy mà cũng ko hiểu nữa đến chịu

file demo.xml
<?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>
File demo.php

PHP Code:
<?php
/**
 * @author Joomlacoders
 * @copyright 2010
 */
unset($xml);
$xml=simplexml_load_file("demo.xml");
echo 
$xml->Buyer_amount;
?>
Còn vẫn chưa hiểu thì phải học lại class OOP
guide
http://www.php.net/manual/en/ref.simplexml.php
sulico16 viết 13:01 ngày 10/10/2018
chắc bạn ko đọc kỹ trên kia minh` post rồi,


$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
Profit viết 13:03 ngày 10/10/2018
php của cậu version nào ?
sulico16 viết 13:02 ngày 10/10/2018
PHP của mình Version 5.2.6
Profit viết 13:01 ngày 10/10/2018
Cậu chạy từ web server đừng chạy bằng editor.
Bài liên quan
0