• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 784
  • Last Modified:

Reading XML with DOMDocument

I'm looking for a way to pull through certain attributes from an XML element IF they exist. For a whole element, I'm using the following code which works fine:
$data=$doc->getElementsByTagName("salary");
    if($data->length) $arr['salary']=$data->item(0)->nodeValue;
    else $arr['salary']=0;

Open in new window


However, I've got a disparity between the XML files which I'm trying to read, some have extra attributes which I'd like to add to an array if they are present. If they are not, I'll add a 0 or NULL value to the said array. Below is my (slightly modified) code for getting the attributes:

if($data->getAttribute("title")!="") 
array_push($arr['deductions'], array($data->getAttribute("title"), $data->getAttribute("home"), $data->getAttribute("host")));

Open in new window


So, for instance, some XML files files have the extra attribute 'switch', if present I'd like to add that to the array, if not, add a 0 value.

Does anyone know a good way of doing this? I'd rather keep on using the DOM as I don't want to rewrite the whole routine using SimpleXML which looks like an easier alternative.

Thanks in advance for any help,
John
0
worldofwires
Asked:
worldofwires
  • 3
  • 2
1 Solution
 
Lukasz ChmielewskiCommented:
Could you post an example of that xml file ?
0
 
worldofwiresAuthor Commented:
Hi,

I've attached two 'censored' files as the contents is a little sensitive. Example 1 is the old file structure which the pasted code in my orignal post works fine with. Example 2 adds a couple of new attributes. I need the code to be backwards compatible so they can read both example 1 and 2 files without erroring.

I've tried messing about with $data->length with no avail.

If you need any more information, let me know. I really apprecaite the help.
EE-Example1.xml
EE-Example2.xml

Many thanks,
John
0
 
worldofwiresAuthor Commented:
I think I've got a solution but it's not as neat as I'd have liked.

Using the Example 2 file from my post above, I'm saying:

if($data->getAttribute("paidin")) $paidin=$data->getAttribute("paidin");
else $paidin=1;
if($data->getAttribute("type")) $type=$data->getAttribute("type");
else $type=1;

Open in new window


I then use the code from the first post to push into an array like this:
if($data->getAttribute("title")!="") 
array_push($arr['deductions'], array($data->getAttribute("title"), $data->getAttribute("home"), $data->getAttribute("host"), $paidin, $type));

Open in new window


It works and I can use it but if anyone knows a better way to work with the DOMDocument, I'd be interested as I'm trying to learn as much as possible about the method.

I was expecting the if($data->getAttribute("type")) to produce an error if it didn't exist but it doesn't seem to, which is nice.

Thanks,
John
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

 
Ray PaseurCommented:
Sorry if you do not want to use SimpleXML - it really is as simple as its name implies!

This code snippet shows how I might go about extracting the attributes from an object.  You can see it in action here:
http://www.laprbass.com/RAY_temp_worldofwires.php

A strategy I might use...  Set up a model array containing the expected attribute keys with the values set to zero.  To capture the attributes from each object , first make a copy of this array.  Then in the foreach iterator that accesses the attributes() method on the object (see line 49-52) assign the key=>val pairs to elements of the copied array.  Any new and unexpected attributes will automatically appear in the array.  Any expected attributes that are missing will still have the value of zero.  In the alternative, you might choose to discard unexpected attributes.  You can do this with a test using array_key_exists().

HTH, and best of luck with the project, ~Ray
<?php // RAY_temp_worldofwires.php
error_reporting(E_ALL);
echo "<pre>";


// DEMONSTRATE HOW TO GET THE ATTRIBUTES FROM XML


// TEST DATA STRINGS FROM THE POST AT EE
$xm1 = <<<XML_1
<?xml version="1.0"?>
<record>
  <home_fam>0</home_fam>
  <host_fam>1</host_fam>
  <car>0</car>
  <pre_tax_remuneration title="Salary" home="34646.00" host="2359413.39"/>
  <pre_tax_remuneration title="Allowance 1" home="8661.5" host="589853.35"/>
  <pre_tax_remuneration title="Allowance 2" home="8661.5" host="589853.35"/>
</record>
XML_1;

$xm2 = <<<XML_2
<?xml version="1.0"?>
<record>
  <home_fam>0</home_fam>
  <host_fam>1</host_fam>
  <car>0</car>
  <pre_tax_remuneration title="Salary" home="34646.00" host="2359413.39" paidin="home" type="1"/>
  <pre_tax_remuneration title="Allowance 1" home="8661.5" host="589853.35" paidin="home" type="1"/>
  <pre_tax_remuneration title="Allowance 2" home="8661.5" host="589853.35" paidin="host" type="1"/>
</record>
XML_2;


// MAKE OBJECTS FROM THE TEST DATA
$ob1 = SimpleXML_Load_String($xm1);
$ob2 = SimpleXML_Load_String($xm2);


// ACTIVATE THIS TO VISUALIZE THE OBJECTS
// var_dump($ob1);
// var_dump($ob2);


// DETECT THE INFORMATION IN "pre_tax_remuneration"
foreach ($ob1->pre_tax_remuneration as $thing)
{
    // SEE http://php.net/manual/en/simplexmlelement.attributes.php
    foreach ($thing->attributes() as $key => $val)
    {
        echo PHP_EOL . "$key => '$val'";
    }
    echo PHP_EOL;
}

foreach ($ob2->pre_tax_remuneration as $thing)
{
    // SEE http://php.net/manual/en/simplexmlelement.attributes.php
    foreach ($thing->attributes() as $key => $val)
    {
        echo PHP_EOL . "$key => '$val'";
    }
    echo PHP_EOL;
}

Open in new window

0
 
worldofwiresAuthor Commented:
Hi Ray,

Thanks for this, it's not that I don't want to use SimpleXML, just I don't want to have to rewrite it. I've used SimpleXML for other tasks and agree that it's much easier to use. I wrote the code I'm amending over 3 years ago before I had much experience of such matters.

It appears that I can get away with my method stated above but I really like the simplicity and effectiveness of the code you posted. I'm now very tempted to rewrite it.

Out of curiosity, where is the PHP_EOL defined? Anyway, consider this solved, thanks very much for your assistance (again).

John
0
 
Ray PaseurCommented:
Glad to help and thanks for the points.   It's a great question and I will add it to my teaching library.

Regarding where is the PHP_EOL defined? - beats the shit outta me.   I just happen to know that it means the OS-context-sensitive End-of-Line character sequence.

See this link:
http://www.php.net/manual/en/language.constants.php#74836

If you follow that link to this link, you can see the list.  But it comes without any explanation!
http://www.php.net/manual/en/reserved.constants.php

This search did not turn up anything more illuminating (but I did not search very hard)
http://lmgtfy.com?q=PHP+Constants+Defined

All the best, ~Ray

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now