0

Given the following XML:

<RegItems>
   <RegItem Name="Test Regger" Email="me@here.com" OrgName="XXX">
      <Establishment Est_Name="Test Org 1" Est_Addr="Test Org 1, 1 Acacia Ave, London, W12 8QT, UK" Est_ContactName="Test One" Est_ContactNum ="321321321" Est_EmailAdd = "test@here.com" />
      <Screens>
         <Timestamp time="Sun, 10 Feb 2019 00:05:24 GMT"/>
         <Screen PDate="2019-01-19" EDate="2019-12-13" SModel="Xi75" Sno="750001"  PCModSno="PC106-0001" PORef="444"/>
      </Screens>
      <UID hash ="dea31a69b70be709fb079bf7d50b1821"/>
   </RegItem>
</RegItems>

I have the following PHP code to extract all the data, but I get blanks returned for each attribute in the formatted message.

   $xml = new SimpleXMLElement($regXML);
   var_dump ($xml);
   $regName = $xml->xpath("/RegItem")['Name'];
   $regEmail = $xml->xpath("/RegItem")['Email'];
   $regOrgName = $xml->xpath("/RegItem")['OrgName'];
   $regEstOrgName = $xml->xpath("/RegItem/Establishment")['Est_Name'];
   $regEstOrgAddr = $xml->xpath("/RegItem/Establishment")['Est_Addr'];
   $regEstOrgContact = $xml->xpath("/RegItem/Establishment")['Est_ContactName'];
   $regEstOrgTel = $xml->xpath("/RegItem/Establishment")['Est_ContactNum'];
   $regEstOrgEmail = $xml->xpath("/RegItem/Establishment")['Est_EmailAdd'];
   $regTimeStamp = $xml->xpath("/RegItem/Timestamp")['time'];

   $mesg = <<<EOF


=====================================================
   Origin XML: $regXML
=====================================================   
   Registered By Details:

   Name:         $regName
   Email:        $regEmail
   Organistion:  $regOrgName

   Registered Organisation:

   Name:           $regOrgName
   Address:        $regEstOrgAddr
   Contact Name:   $regEstOrgContact
   Contact Tel:    $regEstOrgTel
   Contact Email:  $regEstOrgEmail
   Time:           $regTimeStamp
=====================================================   
   Screens:

EOF;

   $screens = $xml->xpath("/RegItem/Screens/Screen");
   foreach($screens as $screen) 
   { 
      $screenWarrStartDate = $screen['PDate'];
      $screenWarrEndDate = $screen['EDate'];
      $screenModel = $screen['SModel'];
      $screenSerial = $screen['Sno'];
      $screenPCSerial = $screen['PCModSno'];
      $screenPORef = $screen['PORef'];

      $mesg .= <<< EOF

         Screen Model:      $screenModel
         Screen Serial No:  $screenSerial
         PC Mod Serial No:  $screenPCSerial
         PO Ref:            $screenPORef
         Warranty Start:    $screenWarrStartDate
         Warranty End:      $screenWarrEndDate

EOF;

   } 

   $mesg .= "
   =====================================================
";

   return ( $mesg );

E.g. instead of

   Name:         Test Regger
   Email:        me@here.com
   Organistion:  XXX

I get:

   Name:         
   Email:        
   Organistion:  
TenG
  • 3,843
  • 2
  • 25
  • 42

2 Answers2

2

You have a couple of issues with your code. Firstly, xpath returns an array of elements, so to access an individual value you need to give an array index. In your case, since there is only one RegItem, you can just use [0]. Secondly, RegItem is not the top-level element in the XML, so the XPath /RegItem will not match anything. You need to change it to either /RegItems/RegItem or //RegItem (the latter finds a RegItem at any depth). So for example, to get the Name attribute of RegItem you would need this:

$regName = $xml->xpath("//RegItem")[0]['Name'];
Nick
  • 138,499
  • 22
  • 57
  • 95
  • Thanks Nick. The other answer beat you to it but I tried using your shorter hand notation too and that worked as well, so +1. – TenG Feb 10 '19 at 01:15
1

You have a few issues:

  1. Your Xpath expression is incorrect. You have /RegItem. The first single / means start from the root of the document. The root is the RegItems element. You can either change it to /RegItems/RegItem or to //RegItem (double-slash means find that element anywhere in the XML tree.
  2. The Xpath query returns a list of items. If you want to access the first element you can use ->item(0), e.g. $xml->xpath("/RegItems/RegItem")->item(0)
  3. To access the property of an element, use getAttribute(), e.g. $xml->xpath("/RegItems/RegItem")->item(0)->getAttribute('Name')
Jim
  • 3,210
  • 2
  • 17
  • 23