/    Sign up×
Community /Pin to ProfileBookmark

Recurse XML to addslashes!

Hi all,

I have the following XML file:

[CODE]<?xml version=”1.0″ encoding=”UTF-8″?>
<data>
<type>Customer Data</type>
<vars>
<payer-id>MPCUST1238495242′</payer-id>
<first-name>David</first-name>
<last-name>Hopkins</last-name>
<email-address>[email protected]</email-address>
<shipping-address>
<company>Outlook Images Ltd</company>
<street>7 Oakdale Court, Oakdale Business Park</street>
<city>Blackwood</city>
<county>Gwent</county>
<country>United Kingdom</country>
<post-code>NP12 4AD</post-code>
</shipping-address>
</vars>
</data>[/CODE]

I’ve loaded it into a XML object for handling like so:

[code=php]$xml = simplexml_load_file(‘customer.xml’);[/code]

I want to be able to loop through the entire file and use the addslashes() function on the data. Does anyone know how I would go about doing this?

Thanks!

dai.hop

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@scragarApr 01.2009 — [code=php]function XMLaddSlashes_r(&$xml){
if($xml->children())
foreach($xml as &$v)
XMLaddSlashes_r($v);
else
$xml[0] = addSlashes($xml[0]);
}[/code]

I've not had a chance to test this, but I think it will work.
Copy linkTweet thisAlerts:
@dai_hopauthorApr 01.2009 — Hi scragar,

Thanks for the reply. You've helped me out a bunch in the past so first off, thanks for that!

The code is giving me a "Fatal error: An iterator cannot be used with foreach by reference" error message. I've modified it a little to match my style, it now looks like this:

[code=php]<?php

function slash(&$xml) {
if ($xml->children()) {
foreach($xml as &$v) {
slash($v);
}
} else {
$xml[0] = addslashes($xml[0]);
}
}

$xml = slash(simplexml_load_file('new-order.xml'));

echo '<pre>';
print_r($xml);
echo '</pre>';

?>[/code]


I'm running PHP version 5.2.6 and it looks like the iterator can no longer be used in the foreach loop.

Anymore ideas?

Thanks!

dai.hop
Copy linkTweet thisAlerts:
@scragarApr 01.2009 — Remove the second ampersand( &$v ), see if it works then, I had it in because I wanted to make sure it was kept by reference. It should stop throwing the error then.

If the code doesn't change anything when it's run, but it doesn't throw any errors you might want to try something like:
[code=php]
foreach($xml as $k=>$v) {
slash($xml->$k);
[/code]
Which should work.


EDIT: the function doesn't return anything, and runs on a pass by ref basis, you are calling it wrong:
[code=php]$xml = simplexml_load_file('new-order.xml');
slash($xml);[/code]

It's so much harder to work with returning the values back up the tree, since there are so many ways it can go wrong.
Copy linkTweet thisAlerts:
@dai_hopauthorApr 01.2009 — I removed the second ampersand and had no joy, so modified the code as suggested:

[code=php]function slash(&$xml) {
if ($xml->children()) {
foreach($xml as $k => $v) {
slash($xml->$k);
}
} else {
$xml[0] = addslashes($xml[0]);
}
}[/code]


This now throws a "Fatal error: Allowed memory size of 8388608 bytes exhausted (tried to allocate 32 bytes)" error message which was surprising!

Perhaps my syntax isn't correct?
Copy linkTweet thisAlerts:
@scragarApr 01.2009 — Hold on, I'll be able to get back on my computer soon, I'll have access to my lamp stack then, and I'll be able to test code before I post it :p

I was kind of hoping that code would work. I might have to edit it to work by returning a value, it will be larger and uglier then though, not exactly looking forwards to it.
Copy linkTweet thisAlerts:
@dai_hopauthorApr 01.2009 — Sweet! The script now appears to be working!

The final code looks like this:

[code=php]<?php

function slash(&$xml) {
if ($xml->children()) {
foreach($xml as $v) {
slash($v);
}
} else {
$xml[0] = addslashes($xml[0]);
}
}

$xml = simplexml_load_file('new-order.xml');
slash($xml);

echo '<pre>';
print_r($xml);
echo '</pre>';

?>[/code]


Thanks a million!
×

Success!

Help @dai_hop spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...