Solved

Reading XML with DOMDocument

Posted on 2011-03-25
6
684 Views
Last Modified: 2013-12-13
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
Comment
Question by:worldofwires
  • 3
  • 2
6 Comments
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 35221872
Could you post an example of that xml file ?
0
 

Author Comment

by:worldofwires
ID: 35222069
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
 

Author Comment

by:worldofwires
ID: 35222098
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
Easy Project Management (No User Manual Required)

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 35222385
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
 

Author Comment

by:worldofwires
ID: 35222432
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
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 35222561
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

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
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. 
The viewer will learn how to dynamically set the form action using jQuery.
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…

706 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now