简体   繁体   中英

PHP Accessing specific content using Xpath attribute

For the below sample XML, I'm trying to echo each field in the <other> tag structure:

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

This is a sample of the specific PHP I am trying to implement to echo each of the <other>

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>';
}}

Currently I am receiving:

Notice: Array to string conversion in C:\\xampp\\htdocs\\project\\main\\php\\project.php on line 162

I have tried various iterations of the code (including imploding the array mentioned in the above error when imploding no values were came through) above but I'm having real trouble accessing the values correctly and getting this code back an into my UI. Any help will be much appreciated. (Please excuse any small XML or Code syntax problems I have obfuscated the values to make it generic for the purposes of this question..)

EDIT 1

I have tried the following:

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>';
    }
}}

Hope this will help you out. Here we are using simplexml_load_string for parsing XML

Changes which can be done in current code of post

Solution 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>';
    }
}}

Solution 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));
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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