从 XML PHP5 获取内容

发布于 2024-08-23 17:01:28 字数 3004 浏览 4 评论 0原文

我正在开发一个支付解决方案,需要一些 PHP 方面的帮助。我正在执行一个 HTTPRequest,作为响应我将得到一些 XML。 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>

基本上我想要做的是从标签中获取内容并将它们保存在字符串中。

我尝试了这个:

$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;

但显然有问题,因为它不会回显任何内容..有建议吗?

I´m working on a paymentsolution and need some help with the PHP. I´m doing a HTTPRequest and in response I will get some XML. The XML Could look like this:

<?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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

我的鱼塘能养鲲 2024-08-30 17:01:28

如果您只是尝试从 XML 字符串中读取一些数据,最简单的方法可能是使用 SimpleXML,而不是 DOM —— DOM 非常适合编写 XML,但对于阅读来说,SimpleXML 很容易使用。

例如,您可以使用类似的内容作为起点:

注意我使用了 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 />';

这将为您提供以下输出:

12
authurl

基本上,使用 SimpleXML,XML 树可用作对象 - XML 的根节点不存在于树中,因为它是根;这就是为什么我不必包含 payer 来访问数据。

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.

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.

$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 ; which is why I didn't have to include payer to access the data.

做个少女永远怀春 2024-08-30 17:01:28

您可以查看 SimpleXML - 它比 DOMDocument 使用起来简单得多。它将以解析的对象形式返回 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);

You might look at SimpleXML - it's much, much more straightforward to use then DOMDocument. It'll give you the XML data back in a parsed object form, so that you can do things like 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>';

// 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);
但可醉心 2024-08-30 17:01:28

尝试使用 simplexml_load_string($string);
然后像这样做一个 foreach

$xml = simplexml_load_string($string);

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

Try using simplexml_load_string($string);
then just do a foreach with it like this

$xml = simplexml_load_string($string);

foreach($xml as $item)
{
echo $item->description; // would print the description
// continiu your logic here
}
熟人话多 2024-08-30 17:01:28

不要将 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;

Don't use DOM for that kind of things, use SimpleXML instead.

$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;
音盲 2024-08-30 17:01:28

这是一个使用 DOM 的解决方案

第一个建议:设置 error_reporting(-1) 以查看任何潜在的错误。如果执行此操作,您将看到 PHP 针对

Notice: Trying to get property of non-object

对 DOMNodeList 元素的 ->item(0) 调用发出几条通知。相当简单的解决方案是使用

$orderXML->loadXml($order);

load() 来代替,因为 load 从文件加载 XML 文档。

Here be a solution with DOM

First suggestion: set error_reporting(-1) to see any potential errors. If you do this you will see PHP raise several Notices about

Notice: Trying to get property of non-object

for the ->item(0) calls to the DOMNodeList elements. The rather simple solution is to use

$orderXML->loadXml($order);

instead of load(), because load loads an XML document from a file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文