how do i read a specifi part of an xml tree

Hi

Part of the XML i need to interpret has multiple Valuation elements under the parent Valuations. I only want to get the information contained in the Valuation child where the Type is OFFICIAL (example below) how can I achieve this please, this is what Im using in PHP to pull it but it only works if there is only 1 Valuation child

Response->PropertyDetail->Valuations->Valuation

 <gpd:Valuations>              
            <gpd:Valuation>
              <gpd:Type>OFFICIAL</gpd:Type>
....
LVL 3
Neil ThompsonSenior Systems DeveloperAsked:
Who is Participating?
 
Ray PaseurConnect With a Mentor Commented:
Nevermind... I cleaned it up and got it to work.  Have a look and post back with any questions, thanks.

See http://www.laprbass.com/RAY_temp_neilt.php

<?php // ray_temp_neilt.php
error_reporting(E_ALL);
echo '<pre>';

// CREATE XML STRING
$xml = <<<XML
<Valuations>
  <Valuation>
    <Type>OFFICIAL</Type>
    <TypeDescription>dfg dfg dfg</TypeDescription>
    <PropertyDesignation>C</PropertyDesignation>
    <FromDate>dfg-04-01</FromDate>
    <IsComposite>false</IsComposite>
    <ValuationOffice>
      <NACCOdtl_PartyRef>dfgf</NACCOdtl_PartyRef>
      <NACCOdtl_OrganizationName>dfg dfg</NACCOdtl_OrganizationName>
      <NACCOdtl_OrganizationDepartmentName>.</NACCOdtl_OrganizationDepartmentName>
      <NACCOdtl_OrganizationCharityReferenceNumber>.</NACCOdtl_OrganizationCharityReferenceNumber>
      <NACCOdtl_DisplayName>dfg dfg</NACCOdtl_DisplayName>
      <NACCOdtl_ContactDetails>
        <pda_Telephone>
          <pda_TelNationalNumber>dfg dfg</pda_TelNationalNumber>
        </pda_Telephone>
      </NACCOdtl_ContactDetails>
    </ValuationOffice>
    <DomesticValuation>
      <PropertyBand>F</PropertyBand>
      <YearlyPrecepts>
        <YearlyPrecept>
          <Year>2012</Year>
          <FinancialYear>2012/13</FinancialYear>
          <TotalPreceptCharge>2146.29</TotalPreceptCharge>
          <Precepts>
            <Precept>
              <PreceptCode>aaa</PreceptCode>
              <PreceptName>bbbCouncil</PreceptName>
              <PreceptCharge>1513.46</PreceptCharge>
            </Precept>
            <Precept>
              <PreceptCode>bbb</PreceptCode>
              <PreceptName>bbb SERVICE</PreceptName>
              <PreceptCharge>98.15</PreceptCharge>
            </Precept>
            <Precept>
              <PreceptCode>ccc</PreceptCode>
              <PreceptName>ccc cc Authority</PreceptName>
              <PreceptCharge>200.32</PreceptCharge>
            </Precept>
            <Precept>
              <PreceptCode>ddd</PreceptCode>
              <PreceptName>dfg dfg dfg</PreceptName>
              <PreceptCharge>334.36</PreceptCharge>
            </Precept>
          </Precepts>
        </YearlyPrecept>
      </YearlyPrecepts>
    </DomesticValuation>
  </Valuation>
  <Valuation>
    <Type>DISABLED</Type>
    <TypeDescription>dfs sdfsdf Type</TypeDescription>
    <PropertyDesignation>C</PropertyDesignation>
    <FromDate>sdf-04-01</FromDate>
    <IsComposite>false</IsComposite>
    <ValuationOffice>
      <NACCOdtl_PartyRef>sdf</NACCOdtl_PartyRef>
      <NACCOdtl_OrganizationName>sdf sdf</NACCOdtl_OrganizationName>
      <NACCOdtl_OrganizationDepartmentName>.</NACCOdtl_OrganizationDepartmentName>
      <NACCOdtl_OrganizationCharityReferenceNumber>.</NACCOdtl_OrganizationCharityReferenceNumber>
      <NACCOdtl_DisplayName>sdf sdf</NACCOdtl_DisplayName>
      <NACCOdtl_ContactDetails>
        <pda_Telephone>
          <pda_TelNationalNumber>CT sdf sdf</pda_TelNationalNumber>
        </pda_Telephone>
      </NACCOdtl_ContactDetails>
    </ValuationOffice>
    <DomesticValuation>
      <PropertyBand>E</PropertyBand>
      <DisabledPerson>
        <pda_CitizenName>
          <pda_CitizenNameTitle>MR</pda_CitizenNameTitle>
          <pda_CitizenNameForename>sdf</pda_CitizenNameForename>
          <pda_CitizenNameSurname>sdf</pda_CitizenNameSurname>
        </pda_CitizenName>
        <pda_CitizenRegistration>
          <pda_NationalInsuranceNumber>sdf</pda_NationalInsuranceNumber>
        </pda_CitizenRegistration>
        <pda_ContactDetails/>
        <pda_CitizenBirthDate/>
        <pda_BirthDate>sdf-sds-05</pda_BirthDate>
        <pda_VerifiedBy>not verified</pda_VerifiedBy>

        <NACCOdtl_PartyRef>sdsd</NACCOdtl_PartyRef>
        <NACCOdtl_DisplayName>Mr sdf sdf</NACCOdtl_DisplayName>
      </DisabledPerson>
      <ReviewDate>sdf-sdsd-06</ReviewDate>
      <YearlyPrecepts>
        <YearlyPrecept>
          <Year>2012</Year>
          <FinancialYear>2012/13</FinancialYear>
          <TotalPreceptCharge>1816.09</TotalPreceptCharge>
          <Precepts>
            <Precept>
              <PreceptCode>aaa</PreceptCode>
              <PreceptName>sdf f sdf</PreceptName>
              <PreceptCharge>1280.62</PreceptCharge>
            </Precept>
            <Precept>
              <PreceptCode>bbb</PreceptCode>
              <PreceptName>sdf sdfsdf sdf</PreceptName>
              <PreceptCharge>83.05</PreceptCharge>
            </Precept>
            <Precept>
              <PreceptCode>ccc</PreceptCode>
              <PreceptName>sdfd df sdf</PreceptName>
              <PreceptCharge>169.50</PreceptCharge>
            </Precept>
            <Precept>
              <PreceptCode>ddd</PreceptCode>
              <PreceptName>sdf df sdf</PreceptName>
              <PreceptCharge>282.92</PreceptCharge>
            </Precept>
          </Precepts>
        </YearlyPrecept>
      </YearlyPrecepts>
    </DomesticValuation>
  </Valuation>
</Valuations>
XML;

$obj = SimpleXML_Load_String($xml);

// USE AN ITERATOR TO FIND THE OFFICIAL VALUATIONS
foreach ($obj->Valuation as $v)
{
    if ($v->Type == 'OFFICIAL')
    {
        $x = $v->ValuationOffice->NACCOdtl_ContactDetails->pda_Telephone->pda_TelNationalNumber;
        echo PHP_EOL . "pda_TelNationalNumber: $x";

        $x = $v->DomesticValuation->PropertyBand;
        echo PHP_EOL . "PropertyBand: $x";

        $x = $v->DomesticValuation->YearlyPrecepts->YearlyPrecept->Year;
        echo PHP_EOL . "Year: $x";

        $x = $v->DomesticValuation->YearlyPrecepts->YearlyPrecept->TotalPreceptCharge;
        echo PHP_EOL . "TotalPreceptCharge: $x";
    }
}

Open in new window

0
 
Ray PaseurCommented:
You can use the foreach() iterator like this...

foreach (Response->PropertyDetail->Valuations as Valuation)
{
    // DO SOMETHING with the Valuation
}

If you want to post a link to an XML document of the kind you're processing, I'll be glad to show you a tested and working code sample.

Best regards, ~Ray
0
 
Neil ThompsonSenior Systems DeveloperAuthor Commented:
Thanks Ray

Attached XML, ideally if possible rather than iterating I'd just like to use the Valuation where the type is OFFICIAL : <Type>OFFICIAL</Type>

Regards
Neil
ray.xml
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
Ray PaseurCommented:
I think you'll have to iterate to locate the OFFICIAL.  Back in a moment... ~Ray
0
 
Ray PaseurCommented:
Can you post a valid XML document, please.  This one appears to have mismatched tags (probably an artifact of hand-editing).  Thanks, ~Ray
0
 
Neil ThompsonSenior Systems DeveloperAuthor Commented:
bear with me, its full of personal data that ill have to clean first.
0
 
Neil ThompsonSenior Systems DeveloperAuthor Commented:
clean xml attached, just got content in both <Type>

<gpd:Type>OFFICIAL</gpd:Type>
<gpd:TypeDescription>approved text</gpd:TypeDescription>

 elements
ray2.xml
0
 
Ray PaseurCommented:
OK, Give me a moment to work the new XML.  Back in a flash, ~Ray
0
 
Neil ThompsonSenior Systems DeveloperAuthor Commented:
Superb job, you save me once again Ray!
0
 
Ray PaseurCommented:
Here's the version using the new (valid) XML.  Note that I munged the namespace, a semi-hack I use often to get quick results.

<?php // ray_temp_neilt.php
error_reporting(E_ALL);
echo '<pre>';

// FUNCTION TO MUNG THE XML (CHEAT NAMESPACE NOTATION)
function mungXML($xml)
{
    // A REGULAR EXPRESSION TO MUNG THE XML
	$rgx
	= '#'           // REGEX DELIMITER
	. '('           // GROUP PATTERN 1
	. '\<'          // LOCATE A LEFT WICKET
	. '/{0,1}'      // MAYBE FOLLOWED BY A SLASH
	. '.*?'         // ANYTHING OR NOTHING
	. ')'           // END GROUP PATTERN
	. '('           // GROUP PATTERN 2
	. ':{1}'        // A COLON (EXACTLY ONE)
	. ')'           // END GROUP PATTERN
	. '#'           // REGEX DELIMITER
	;
	// INSERT THE UNDERSCORE INTO THE TAG NAME
	$rep
	= '$1'          // BACKREFERENCE TO GROUP 1
	. '_'           // LITERAL UNDERSCORE IN PLACE OF GROUP 2
	;
	// PERFORM THE REPLACEMENT
	return preg_replace($rgx, $rep, $xml);
}

// THE XML STRING IS HERE
$url = 'http://filedb.experts-exchange.com/incoming/2012/09_w37/602905/ray2.xml';
$xml = file_get_contents($url);
$new = mungXml($xml);

// CREATE AN OBJECT
$obj = SimpleXML_Load_String($new);

// ACTIVATE THIS TO SEE THE OBJECT
// var_dump($obj);

// USE AN ITERATOR TO FIND THE OFFICIAL VALUATIONS
foreach ($obj->{'SOAP-ENV_Body'}->gpd_GetPropertyDetail->gpd_Response->gpd_PropertyDetail as $d)
{
    foreach ($d->gpd_Valuations->gpd_Valuation as $v)
    {
        if ($v->gpd_Type == 'OFFICIAL')
        {
            $x = $v->gpd_TypeDescription;
            echo PHP_EOL . "gpd_TypeDescription: $x";
        }
    }
}

Open in new window

Thanks for the points! ~Ray
0
All Courses

From novice to tech pro — start learning today.