简体   繁体   English

从XML PHP5获取内容

[英]Get content from XML PHP5

I´m working on a paymentsolution and need some help with the PHP. 我正在进行支付,需要一些PHP的帮助。 I´m doing a HTTPRequest and in response I will get some XML. 我在做HTTPRequest,作为回应,我会得到一些XML。 The XML Could look like this: XML可能如下所示:

<?xml version="1.0" encoding="utf-8" ?>
<payer>
    <purchase_list>
            <freeform_purchase>
                <line_number>1</line_number>
                <description>description</description>
                <price_including_vat>12</price_including_vat>
                <vat_percentage>
                    15
                </vat_percentage>
                <quantity>10</quantity>
            </freeform_purchase>
    </purchase_list>
    <payment_urls>
        <auth_url>authurl</auth_url>
        <settle_url>settleurl</settle_url>
    </payment_urls>
</payer>

Basically what I want to do is to get the content from the tags and save them in strings. 基本上我想要做的是从标签中获取内容并将其保存在字符串中。

I tried this: 我试过这个:

$order = '<?xml version="1.0" encoding="utf-8" ?>
<payer>
    <purchase_list>
            <freeform_purchase>
                <line_number>1</line_number>
                <description>description</description>
                <price_including_vat>12</price_including_vat>
                <vat_percentage>
                    15
                </vat_percentage>
                <quantity>10</quantity>
            </freeform_purchase>
    </purchase_list>
    <payment_urls>
        <auth_url>authurl</auth_url>
        <settle_url>settleurl</settle_url>
    </payment_urls>
</payer>';

$orderXML = new DOMDocument();
$orderXML->load($order);

$payerXML = $orderXML->getElementsByTagName( 'payer' );
$purchase_listXML = $orderXML->getElementsByTagName( 'purchase_list' );
$freeform_purchaseXML = $orderXML->getElementsByTagName( 'freeform_purchase' );
$linenumberXML = $orderXML->getElementsByTagName( 'line_number' );
$descriptionXML = $orderXML->getElementsByTagName( 'description' );
$price_inc_vatXML = $orderXML->getElementsByTagName( 'price_including_vat' );
$vat_percentageXML = $orderXML->getElementsByTagName( 'vat_percentage' );
$quantityXML = $orderXML->getElementsByTagName( 'quantity' );
$settle_urlXML = $orderXML->getElementsByTagName( 'settle_url' );
$auth_urlXML = $orderXML->getElementsByTagName( 'auth_url' );

$theLineNumber = $linenumberXML->item(0)->nodeValue;
$theValue = $descriptionXML->item(0)->nodeValue;
$freeform_price = $price_inc_vatXML->item(0)->nodeValue;
$freeform_vat = $vat_percentageXML->item(0)->nodeValue;
$freeform_quantity = $quantityXML->item(0)->nodeValue;

$Settle_url = $settle_urlXML->item(0)->nodeValue;
$Auth_url = $auth_urlXML->item(0)->nodeValue;

echo 'thLineNumber - ' . $theLineNumber;
echo $theValue;
echo $freeform_price;
echo $freeform_vat;
echo $freeform_quantity;
echo $Settle_url;
echo $Auth_url;

But obviously there is something wrong since it won´t echo anything.. Suggestions? 但显然有一些错误,因为它不会回应任何东西。建议?

If you are only trying to read some data from an XML string, the simplest way would probably be to use SimpleXML , and not DOM -- DOM is well suited when it comes to writing XML, but for reading, SimpleXML is much easy to work with. 如果您只是尝试从XML字符串中读取一些数据,最简单的方法可能是使用SimpleXML ,而不是DOM -DOM非常适合编写XML,但是对于阅读,SimpleXML很容易工作用。

For instance, you could use something like this as a starting point : 例如,你可以使用这样的东西作为起点:
Note I used the simplexml_load_string function to load the XML string. 注意我使用simplexml_load_string函数来加载XML字符串。

$string = <<<STR
<?xml version="1.0" encoding="utf-8" ?>
<payer>
    <purchase_list>
            <freeform_purchase>
                <line_number>1</line_number>
                <description>description</description>
                <price_including_vat>12</price_including_vat>
                <vat_percentage>
                    15
                </vat_percentage>
                <quantity>10</quantity>
            </freeform_purchase>
    </purchase_list>
    <payment_urls>
        <auth_url>authurl</auth_url>
        <settle_url>settleurl</settle_url>
    </payment_urls>
</payer>
STR;

$xml = simplexml_load_string($string);

echo intval($xml->purchase_list->freeform_purchase->price_including_vat) . '<br />';
echo (string)$xml->payment_urls->auth_url . '<br />';

Which would give you the following output : 哪个会给你以下输出:

12
authurl

Basically, with SimpleXML, the XML tree is available as an object -- the root node of the XML not being present in the tree, as it's the root ; 基本上,使用SimpleXML,XML树可用作对象 - 树的根节点不存在于树中,因为它是根; which is why I didn't have to include payer to access the data. 这就是为什么我不必包括payer来访问数据。

You might look at SimpleXML - it's much, much more straightforward to use then DOMDocument. 您可以查看SimpleXML - 使用DOMDocument要简单得多。 It'll give you the XML data back in a parsed object form, so that you can do things like this: 它将以解析的对象形式返回XML数据,以便您可以执行以下操作:

$order = '<?xml version="1.0" encoding="utf-8" ?>
<payer>
    <purchase_list>
            <freeform_purchase>
                <line_number>1</line_number>
                <description>description</description>
                <price_including_vat>12</price_including_vat>
                <vat_percentage>
                    15
                </vat_percentage>
                <quantity>10</quantity>
            </freeform_purchase>
    </purchase_list>
    <payment_urls>
        <auth_url>authurl</auth_url>
        <settle_url>settleurl</settle_url>
    </payment_urls>
</payer>';

// Alternative version: $xml = new SimpleXMLElement($order);
// (completely equivalent to the below line)
$xml = simplexml_load_string($order);

$vat_pct = intval($xml->purchase_list->freeform_purchase->vat_percentage);

Try using simplexml_load_string($string); 尝试使用simplexml_load_string($ string); then just do a foreach with it like this 然后就像这样做一个foreach

$xml = simplexml_load_string($string);

foreach($xml as $item)
{
echo $item->description; // would print the description
// continiu your logic here
}

Don't use DOM for that kind of things, use SimpleXML instead. 不要将DOM用于那种事情,而是使用SimpleXML

$payer = simplexml_load_string($order);
$freeform_purchase = $payer->purchase_list->freeform_purchase;

echo "Line number: ", $freeform_purchase->line_number, "\n";
echo "Description: ", $freeform_purchase->description, "\n";
echo "Auth URL: ", $payer->payment_urls->auth_url, "\n";
echo "Settle URL: ", $payer->payment_urls->settle_url, "\n";

// if you use the values as strings, cast them as strings
$description = (string) $freeform_purchase->description;

// if you have to use the values for math, cast them as integers
$vat_percentage = (int) $freeform_purchase->vat_percentage;

Here be a solution with DOM 这是DOM的解决方案

First suggestion: set error_reporting(-1) to see any potential errors. 第一个建议:设置error_reporting(-1)以查看任何潜在的错误。 If you do this you will see PHP raise several Notices about 如果你这样做,你会看到PHP提出几个通知

Notice: Trying to get property of non-object

for the ->item(0) calls to the DOMNodeList elements. 对于->item(0)调用DOMNodeList元素。 The rather simple solution is to use 相当简单的解决方案是使用

$orderXML->loadXml($order);

instead of load() , because load loads an XML document from a file. 而不是load() ,因为load从文件加载XML文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM