[英]Getting XML attribute from the inner node using PHP SimpleXML is failing
Chaps, I am trying to get attributes of the 'file' node in the following XML with SimpleXml but every time is gives me back null. 糟糕,我正在尝试使用SimpleXml在以下XML中获取“文件”节点的属性,但每次都使我返回null。 I have successfully got the attributes for the study node but fail to get the 'file' atts .
我已经成功获取了研究节点的属性,但未获取“文件”属性。
here is the xml : 这是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>
and here is my code : 这是我的代码:
$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
);
}
I can get "uid" and "acc" but I can't get "cat" and "date" inside file node. 我可以在文件节点中获取“ uid”和“ acc”,但无法获取“ cat”和“ date”。 When I look at the array of the xml in my debug variables I can see uid and acc attributes but no sign of cat and date attributes.
当我在调试变量中查看xml数组时,可以看到uid和acc属性,但没有看到cat和date属性的迹象。
I Would really appreciate any help. 我将非常感谢您的帮助。
I prefer to stick to SimpleXML as all my code is using that so far. 我更喜欢坚持使用SimpleXML,因为到目前为止我的所有代码都在使用它。
Cheers 干杯
This is how I did it based on Ghost answer. 这是我根据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;
}
If you're trying to use print_r()/var_dump()
to check, then it will do no justice. 如果您尝试使用
print_r()/var_dump()
进行检查,那么它将毫无道理。 But it is there, try to traverse to it along with using ->attributes()
inside the foreach as well: 但是它在那里,尝试遍历它,同时也在foreach中使用
->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
)
)
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.