对于以下示例XML,我正在尝试echo
<other>
标记结构中的每个字段:
$theXML = new SimpleXMLElement($stringBody);
<Record>
<PersonDetails>
<StuffA>050656770</StuffA>
<StuffB>Stuff B Content</StuffB>
<StuffC>Stuff C Content</StuffC>
</PersonDetails>
<PersonDetails>
<StuffA>050656770</StuffA>
<StuffB>Stuff B Content</StuffB>
<StuffC>Stuff C Content</StuffC>
</PersonDetails>
<PersonDetails>
<StuffA>050656770</StuffA>
<StuffB>Stuff B Content</StuffB>
<StuffC>Stuff C Content</StuffC>
<OtherRelatedDetails>
<OtherDetails>
<Other>
<core:AuthNum>3761626</core:AuthNum>
<core:FromDate>2007-11-22</core:FromDate>
<core:ToDate>9999-12-31</core:ToDate>
<core:AuthType>A</core:AuthType>
<core:Name>ABC</core:Name>
<core:Num>3205355</core:Num>
</Other>
<Other>
<core:AuthNum>4383908</core:AuthNum>
<core:FromDate>2007-11-22</core:FromDate>
<core:ToDate>9999-12-31</core:ToDate>
<core:AuthType>B</core:AuthType>
<core:Name>DEF</core:Name>
<core:Num>3205355</core:Num>
</Other>
<Other>
<core:AuthNum>8103583</core:AuthNum>
<core:FromDate>2007-11-22</core:FromDate>
<core:ToDate>9999-12-31</core:ToDate>
<core:AuthType>C</core:AuthType>
<core:Name>GHI</core:Name>
<core:Num>3205355</core:Num>
</Other>
</OtherDetails>
</OtherRelatedDetails>
</PersonDetails>
<PersonDetails>
<StuffA>050656770</StuffA>
<StuffB>Stuff B Content</StuffB>
<StuffC>Stuff C Content</StuffC>
</PersonDetails>
</Record>
这是我尝试对echo
每个<other>
实施的特定PHP的示例
if (!empty($theXML->$record->$PersonDetails->OtherRelatedDetails->OtherDetails->Other[0])) {
foreach ($theXML->$record->$PersonDetails->OtherRelatedDetails->OtherDetails->Other as $OtherContent) {
echo '<b>Authorisation Number: </b>' . $OtherContent->xpath("//Other[@core:AuthNum]") . '<br>';
echo '<b>From Date: </b>' . $OtherContent->xpath("//Other[@core:FromDate]") . '<br>';
echo '<b>To Date: </b>' . $OtherContent->xpath("//Other[@core:ToDate]") . '<br>';
echo '<b>Authorisation Type: </b>' . $OtherContent->xpath("//Other[@core:AuthType]") . '<br>';
echo '<b>Name: </b>' . $OtherContent->xpath("//Other[@core:Name]") . '<br>';
echo '<b>Number: </b>' . $OtherContent->xpath("//Other[@core:Num]") . '<br>';
}}
目前我正在接收:
注意:第162行的C:\ xampp \ htdocs \ project \ main \ php \ project.php中的数组到字符串转换
上面已经尝试了各种代码迭代(包括在上面的错误中提到的内容,但是在没有输入任何值的情况下使用了上面的错误),但是我正确地无法正确访问这些值并将此代码返回到我的UI中。任何帮助都感激不尽。 (请原谅任何小的XML或代码语法问题,我已经混淆了这些值,使其在本问题的目的中是通用的。)
编辑1
我尝试了以下内容:
foreach ($result->PersonDetails as $val) {
if (isset($val->OtherRelatedDetails->OtherDetails->Other)) {
foreach ($val->OtherRelatedDetails->OtherDetails->Other as $value) {
echo '<b>Authorisation Number: </b>' . $value[0] . '<br>';
echo '<b>Authorisation Type: </b>' . $value[3] . '<br>';
echo '<b>From Date: </b>' . $value[1] . '<br>';
echo '<b>To Date: </b>' . $value[2] . '<br>';
echo '<b>Name: </b>' . $value[4] . '<br>';
echo '<b>Number: </b>' . $value[5] . '<br>';
}
}}
答案 0 :(得分:0)
希望这会帮助你。我们在这里使用simplexml_load_string
来解析XML
可以在当前的帖子代码中完成的更改
解决方案1: Try this code snippet here
foreach ($result->PersonDetails as $val) {
if (isset($val->OtherRelatedDetails->OtherDetails->Other)) {
foreach ($val->OtherRelatedDetails->OtherDetails->Other as $value) {
$value=(array)$value;
$value=array_values($value);
echo '<b>Authorisation Number: </b>' . $value[0] . '<br>';
echo '<b>Authorisation Type: </b>' . $value[3] . '<br>';
echo '<b>From Date: </b>' . $value[1] . '<br>';
echo '<b>To Date: </b>' . $value[2] . '<br>';
echo '<b>Name: </b>' . $value[4] . '<br>';
echo '<b>Number: </b>' . $value[5] . '<br>';
}
}}
解决方案2: Try this code snippet here
<?php
ini_set('display_errors', 1);
libxml_use_internal_errors(true);
$result = simplexml_load_string('<?xml version="1.0"?>
<Record>
<PersonDetails>
<StuffA>050656770</StuffA>
<StuffB>Stuff B Content</StuffB>
<StuffC>Stuff C Content</StuffC>
</PersonDetails>
<PersonDetails>
<StuffA>050656770</StuffA>
<StuffB>Stuff B Content</StuffB>
<StuffC>Stuff C Content</StuffC>
</PersonDetails>
<PersonDetails>
<StuffA>050656770</StuffA>
<StuffB>Stuff B Content</StuffB>
<StuffC>Stuff C Content</StuffC>
<OtherRelatedDetails>
<OtherDetails>
<Other>
<core:AuthNum>3761626</core:AuthNum>
<core:FromDate>2007-11-22</core:FromDate>
<core:ToDate>9999-12-31</core:ToDate>
<core:AuthType>A</core:AuthType>
<core:Name>ABC</core:Name>
<core:Num>3205355</core:Num>
</Other>
<Other>
<core:AuthNum>4383908</core:AuthNum>
<core:FromDate>2007-11-22</core:FromDate>
<core:ToDate>9999-12-31</core:ToDate>
<core:AuthType>B</core:AuthType>
<core:Name>DEF</core:Name>
<core:Num>3205355</core:Num>
</Other>
<Other>
<core:AuthNum>8103583</core:AuthNum>
<core:FromDate>2007-11-22</core:FromDate>
<core:ToDate>9999-12-31</core:ToDate>
<core:AuthType>C</core:AuthType>
<core:Name>GHI</core:Name>
<core:Num>3205355</core:Num>
</Other>
</OtherDetails>
</OtherRelatedDetails>
</PersonDetails>
<PersonDetails>
<StuffA>050656770</StuffA>
<StuffB>Stuff B Content</StuffB>
<StuffC>Stuff C Content</StuffC>
</PersonDetails>
</Record>');
foreach($result->PersonDetails as $val)
{
if(isset($val->OtherRelatedDetails->OtherDetails->Other))
{
foreach($val->OtherRelatedDetails->OtherDetails->Other as $value)
{
print_r(array_values((array)$value));
}
}
}