/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] PHP XML DOM nodeValue question

hi experts,

does nodeValue only work with XML files that have a structure like this:

[CODE]<title>The Brief Wondrous Life of Oscar Wao</title>
<author>Junot Diaz</author>
<publisher>Riverhead Hardcover</publisher>
[/CODE]

i have a XML file with a structure like the one below and i need to write a PHP script that looks for all the <page value=”nnn”/> and substracts 2. so in the example below 189 becomes 187, 419 becomes 417.

[CODE]<area coords=”179,115,198,127″ id=”7″ type=”rect”>
<highlight_behaviour id=”jump2page”/>
<click_behaviour id=”jump2page”/>
<page value=”189″/>
</area>
<area coords=”204,115,223,127″ id=”8″ type=”rect”>
<highlight_behaviour id=”jump2page”/>
<click_behaviour id=”jump2page”/>
<page value=”419″/>
</area>[/CODE]

this doesn´t work:

[code=php]$pages = $xpath->query(‘area/page’);
foreach($pages as $page)
{
$pagenumber = $page->nodeValue;
$page->nodeValue = (int)$pagenumber-2;
}
[/code]

any ideas?

best,
jogol

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@NogDogMay 12.2011 — Use [url=http://www.php.net/manual/en/domelement.hasattribute.php]hasAttribute[/url]() to see if that attribute exists, and if so get it with [url=http://www.php.net/manual/en/domelement.getattribute.php]getAttribute[/url]().
Copy linkTweet thisAlerts:
@jogolauthorMay 12.2011 — got it, thanks!
[code=php]
$dom = new DOMDocument();
$dom->load($file);
$pages=$dom->getElementsByTagName('page');
foreach($pages as $page) {
if($page->hasAttribute('value')) {
$pagenum = (int)$page->getAttribute('value')-2;
$page->setAttribute('value', '$pagenum');
}
}[/code]
×

Success!

Help @jogol 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 5.18,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

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

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...