【问题标题】:Getting XML attribute from the inner node using PHP SimpleXML is failing使用 PHP SimpleXML 从内部节点获取 XML 属性失败
【发布时间】:2015-02-01 23:23:29
【问题描述】:

章节, 我正在尝试使用 SimpleXml 在以下 XML 中获取“文件”节点的属性,但每次都返回 null。我已成功获取研究节点的属性,但未能获取“文件” atts。

这里是xml:

<?xml version="1.0" encoding="UTF-8"?>
<studies>
  <study uid="1.3.12.2" acc="181">
    <date>20051218</date>
    <time>2156</time>
    <ref>CG</ref>
    <desc>Abdomen</desc>
    <id></id>
    <path>S00001</path>
    <modality>CR</modality>
    <reports>
  <file cat="UNK" date="20141124">Card_Cloud.txt</file>
</reports>
</study>

这是我的代码:

$studyXML = new SimpleXMLElement($studyXML);
$studyXML_array = array();
foreach ($studyXML ->study as $study)
{

// Getting uid and accession from XML attributes
$uid = (!empty($study)) ? (String)$study->attributes()->uid : '';
$acc = (!empty($study)) ? (String)$study->attributes()->acc : '';



// Getting the reports and putting them in an array
$reports = array ();
foreach($study->reports as $rep)
{
  $cat = (String)$rep->attributes()->cat;
  $reports[] = (String)$rep->file;

}

// Constructing the xml as an array
$studyXML_array[] = array
                    (
                      'uid'      => $uid,
                      'acc'      => $acc,
                      'date'     => (String)$study->date,                          
                      'reports'  => $reports
                    );

}

我可以得到“uid”和“acc”,但不能在文件节点中得到“cat”和“date”。 当我查看调试变量中的 xml 数组时,我可以看到 uid 和 acc 属性,但没有 cat 和 date 属性的迹象。

非常感谢任何帮助。

我更喜欢坚持使用 SimpleXML,因为到目前为止我的所有代码都在使用它。

干杯

【问题讨论】:

  • 既然你已经有了一个对象,你不需要用它来创建一个数组,对吧?

标签: php xml simplexml


【解决方案1】:

如果您尝试使用print_r()/var_dump() 进行检查,那么它将不公平。但它就在那里,尝试在 foreach 中使用-&gt;attributes() 来遍历它:

$studyXML = new SimpleXMLElement($studyXML);
$studyXML_array = array();
foreach ($studyXML ->study as $study)
{

    // Getting uid and accession from XML attributes
    $uid = (!empty($study)) ? (String)$study->attributes()->uid : '';
    $acc = (!empty($study)) ? (String)$study->attributes()->acc : '';

    // Getting the reports and putting them in an array
    $reports = array();
    // loop each attribute
    foreach($study->reports->file->attributes() as $key => $rep)
    {
        $reports[$key] = (string) $rep;
    }

    // Constructing the xml as an array
    $studyXML_array[] = array(
        'uid'      => $uid,
        'acc'      => $acc,
        'date'     => (String) $study->date,
        'reports'  => $reports
        );
}

echo '<pre>';
print_r($studyXML_array);

Sample Output:

Array
(
    [0] => Array
        (
            [uid] => 1.3.12.2
            [acc] => 181
            [date] => 20051218
            [reports] => Array
                (
                    [cat] => UNK
                    [date] => 20141124
                )

        )

)

【讨论】:

  • @Sinaaria 很高兴这有帮助
  • 我仍然不确定为什么在调试变量中或 print_r()/var_dump() 时看不到它。有什么想法吗?
  • @Sinaaria 这里是关于这个问题的一个非常有说服力的答案stackoverflow.com/questions/12432739/…
  • 谢谢老兄。我真的很感谢你的帮助:) 干杯
【解决方案2】:

这就是我根据 Ghost 回答的方式。

$reports = array ();
foreach($study->reports->file as $rep)
{
  $temp = array();
  $temp['repName'] = (String)$rep;

  // Getting cat and date attributes of the report
  foreach($rep->attributes() as $key => $rep)
  {
    $temp[$key] = (string) $rep;
  }

  $reports[] = $temp;
}

【讨论】:

    猜你喜欢
    • 2012-10-03
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    • 2011-09-03
    • 1970-01-01
    • 2012-05-19
    • 2014-04-26
    相关资源
    最近更新 更多