?
Solved

Reading XML with DOMDocument

Posted on 2011-03-25
6
Medium Priority
?
737 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Setting up LaraDock for Laravel

Learn how to set up LaraDock in a Laravel project - LaraDock gives us an easy way to run a Laravel application using Docker in a single command.

 
LVL 111

Accepted Solution

by:
Ray Paseur earned 2000 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 111

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article discusses four methods for overlaying images in a container on a web page
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

752 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