Solved

Need to access an XML file from PHP

Posted on 2014-03-25
4
464 Views
Last Modified: 2014-03-25
I've been trying to access an XML file using PHP's XML Reader but I am finding it very difficult.

An example of the XML file is below... (also attached)

What I need to know is how to access for example "EMAILADDRESS" or "LOCATIONDESC" field from the <Jobs> section

Any help would be much appreciated!


<?xml version="1.0" encoding="UTF-8"?>
<JobShopExport xmlns="http://tempuri.org/JSExport.xsd">
   <Jobs>
      <Job SysKey="0" DBID="79" RefNo="J142" Con="13" CONSULTANTNAME="USER8 " EMAILADDRESS="test@gp.vu" Sector="47" SECTORNAME="Main Sector" Title="Sales Consultant" Loc="204" LOCATIONDESC="Dudley,West Midlands" Term="4" EMPTYPEDESC="Perm" MinBsc="18000" MaxBsc="20000" MinPkg="0" MaxPkg="0" HoursPW="0" Cat="65" PUBCATDESC="Sales" LOCATIONCODE="SP10 4AE" GRIDREFX="43490" GRIDREFY="14670" SDate="" EDate="" LTDRATE="" PAYERATE="" Currency="0" CURRENCYSYMBOL="" Area="11" AREADESCRIPTION="S" CreatedOn="21/December/2005" UpdatedOn="19/May/2006" PubJobDesc="" PosID="106" POSITIONDESC="Sales Consultant" Company="Test Company Ltd" ChuntID="" ChargeUnitDesc="">
         <Atts>
            <Att AttID="162" Grade="0" />
            <Att AttID="105" Grade="0" />
            <Att AttID="106" Grade="0" />
         </Atts>
      </Job>
   </Jobs>
   <SysKeys>
      <Itm DBID="0" Desc="admin  " />
   </SysKeys>
   <Master_Atts>
      <Itm DBID="405" Desc="Position" />
      <Itm DBID="411" Desc="Qualifications" />
   </Master_Atts>
   <Att_Grades>
      <Itm DBID="0" Desc="" />
   </Att_Grades>
   <Sectors>
      <Itm DBID="47" Desc="Main Sector" />
   </Sectors>
   <EmpTerms>
      <Itm DBID="4" Desc="Perm" />
   </EmpTerms>
   <Categories>
      <Itm DBID="65" Desc="Sales" />
   </Categories>
   <Job_Areas>
      <Itm DBID="11" Desc="S" />
   </Job_Areas>
   <Position_Types>
      <Itm DBID="106" Desc="Sales Consultant" />
   </Position_Types>
   <Currencies>
      <Itm DBID="0" Desc="" />
   </Currencies>
   <Charge_Units>
      <Itm DBID="0" Desc="" />
   </Charge_Units>
   <Job_Atts>
      <Itm DBID="106" Desc="Sales Consultant" MasterID="405" />
      <Itm DBID="162" Desc="Degree" MasterID="411" />
      <Itm DBID="105" Desc="Marketing" MasterID="405" />
   </Job_Atts>
   <Consultants>
      <Itm DBID="13" Desc="USER8 " EMail="test2@account.com" />
   </Consultants>
   <Locations>
      <Itm DBID="204" Desc="Andover,Hampshire" Code="SP10 4AE" GridX="43490" GridY="14670" />
   </Locations>
</JobShopExport>

Open in new window

ee.xml
0
Comment
Question by:tryinghard45
  • 2
4 Comments
 
LVL 109

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 39953006
Please see http://iconoun.com/demo/temp_tryinghard45.php

This kind of access to XML and JSON documents seems to be easiest if you use object-oriented notation.  These data types can be turned into instances of PHP StdClass objects, then you can use iterators to access the properties.

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

// http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28396599.html

$xml = <<<ENDXML
<?xml version="1.0" encoding="UTF-8"?>
<JobShopExport xmlns="http://tempuri.org/JSExport.xsd">
   <Jobs>
      <Job SysKey="0" DBID="79" RefNo="J142" Con="13" CONSULTANTNAME="USER8 " EMAILADDRESS="test@gp.vu" Sector="47" SECTORNAME="Main Sector" Title="Sales Consultant" Loc="204" LOCATIONDESC="Dudley,West Midlands" Term="4" EMPTYPEDESC="Perm" MinBsc="18000" MaxBsc="20000" MinPkg="0" MaxPkg="0" HoursPW="0" Cat="65" PUBCATDESC="Sales" LOCATIONCODE="SP10 4AE" GRIDREFX="43490" GRIDREFY="14670" SDate="" EDate="" LTDRATE="" PAYERATE="" Currency="0" CURRENCYSYMBOL="" Area="11" AREADESCRIPTION="S" CreatedOn="21/December/2005" UpdatedOn="19/May/2006" PubJobDesc="" PosID="106" POSITIONDESC="Sales Consultant" Company="Test Company Ltd" ChuntID="" ChargeUnitDesc="">
         <Atts>
            <Att AttID="162" Grade="0" />
            <Att AttID="105" Grade="0" />
            <Att AttID="106" Grade="0" />
         </Atts>
      </Job>
   </Jobs>
   <SysKeys>
      <Itm DBID="0" Desc="admin  " />
   </SysKeys>
   <Master_Atts>
      <Itm DBID="405" Desc="Position" />
      <Itm DBID="411" Desc="Qualifications" />
   </Master_Atts>
   <Att_Grades>
      <Itm DBID="0" Desc="" />
   </Att_Grades>
   <Sectors>
      <Itm DBID="47" Desc="Main Sector" />
   </Sectors>
   <EmpTerms>
      <Itm DBID="4" Desc="Perm" />
   </EmpTerms>
   <Categories>
      <Itm DBID="65" Desc="Sales" />
   </Categories>
   <Job_Areas>
      <Itm DBID="11" Desc="S" />
   </Job_Areas>
   <Position_Types>
      <Itm DBID="106" Desc="Sales Consultant" />
   </Position_Types>
   <Currencies>
      <Itm DBID="0" Desc="" />
   </Currencies>
   <Charge_Units>
      <Itm DBID="0" Desc="" />
   </Charge_Units>
   <Job_Atts>
      <Itm DBID="106" Desc="Sales Consultant" MasterID="405" />
      <Itm DBID="162" Desc="Degree" MasterID="411" />
      <Itm DBID="105" Desc="Marketing" MasterID="405" />
   </Job_Atts>
   <Consultants>
      <Itm DBID="13" Desc="USER8 " EMail="test2@account.com" />
   </Consultants>
   <Locations>
      <Itm DBID="204" Desc="Andover,Hampshire" Code="SP10 4AE" GridX="43490" GridY="14670" />
   </Locations>
</JobShopExport>
ENDXML;

// MAKE AN OBJECT
$obj = SimpleXML_Load_String($xml);

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

// TEASE OUT SOME DATA
$job = $obj->Jobs->Job;
var_dump($job);

foreach ($job->attributes() as $key => $val)
{
    echo PHP_EOL . "$key <b>$val</b>";
}

// SOME MORE NESTED DATA
foreach ($job->Atts->Att as $att)
{
    print_r($att);
}

Open in new window

HTH, ~Ray
0
 

Author Closing Comment

by:tryinghard45
ID: 39953157
Superb Ray - many thanks!!!

(Easy when you know how eh? !!!)

Cheers

Matt
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 39953263
when you know how
Yep!  Took me a lot of trial and error to know how!

Thanks for the points and thanks for using EE, ~Ray
0

Featured Post

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System (http://www.experts-exchange.com/A_2902.html) introduces the Login System and shows how to implement a login page. The EE Collaborative Logi…
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

789 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question