简体   繁体   English

如何使用PHP读取XML数据

[英]How to read XML Data using PHP

I have below XML format : 我有以下XML格式:

<?xml version="1.0" encoding="utf-8"?>
<Partners Default="123">
<PID id="123" Value="Kell Partners" DefGID="345">
          <GID id="345" Country="US" Value="3.5% + $0.35/transaction + $0/month" Default="True"></GID>
          <GID id="455" Country="US" Value="3.5% + $0.35/transaction + $0/month"></GID>
          <GID id="789" Country="US" Value="3.5% + $0.35/transaction + $0/month"></GID>
          <GID id="159" Country="EU" Value="3.5% + $0.35/transaction + $0/month" Default="True"></GID>
</PID>
<PID id="456" Value="Test Company 2" DefGID="224">
          <GID id="222" Country="CA" Value="3.5% + $0.35/transaction + $0/month" Default="True"></GID>
          <GID id="224" Country="CA" Value="3.5% + $0.35/transaction + $0/month"></GID>
          <GID id="225" Country="UK" Value="3.5% + $0.35/transaction + $0/month"></GID>
          <GID id="226" Country="UK" Value="3.5% + $0.35/transaction + $0/month" Default="True"></GID>
</PID>
</Partners>

I am using below code snippet to read xml data in PHP where My xml showing parents nodes as @attributes. 我正在使用下面的代码片段读取PHP中的xml数据,其中我的xml将父节点显示为@attributes。

<?php
$xmlparser=simplexml_load_file("partners.xml") or die("Cannot open the file.");
echo "<pre>";
print_r($xmlparser->attributes());

?>

In above question I want read the 在上述问题中,我想阅读

default from Partners node..I am unable to read by using above code.. 默认为“合作伙伴”节点。.我无法使用上述代码读取..

and by using Default i will get the child nodes data. 通过使用默认值,我将获得子节点数据。

<?php
$xml=simplexml_load_file("partners.xml") or die("Cannot open the file.");
echo "<pre>";
foreach($xml->children() as $child) {
    foreach($child as $value) {
        print_r($value->children());
    }
}
echo "</pre>";
?>

By this code you can get the data of GID nodes. 通过此代码,您可以获得GID节点的数据。 To get it you don't have to get Partners Default value. 要获得它,您不必获取合作伙伴默认值。

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

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