Modify attribute if node exist with Xpath?
up vote
0
down vote
favorite
I'm trying to modify a node attribute if the node exist or create it if not in a xml file using Xpath.
xml file looks like that :
<krpano>
<hotspot name="hs1" ath="0" atv="0"/>
<hotspot name="hs2" ath="0" atv="0"/>
</krpano>
and here is my php code :
<?php
$str_json = file_get_contents('php://input');
$json_data = json_decode($str_json);
$file = 'myxmlfile.xml';
$xml = simplexml_load_file($file);
$krpano = $xml->xpath("//krpano");
$hotspot = $xml->xpath('//hotspot[@name="'.$json_data->name.'"]');
if ($hotspot){
$xml->xpath('//hotspot[@name="'.$json_data->name.'"]/@ath->'.$json_data->xpos.'');
$xml->xpath('//hotspot[@name="'.$json_data->name.'"]/@atv->'.$json_data->ypos.'');
}else{
$newhs = $krpano[0]->addChild('hotspot');
$newhs->addAttribute('name', $json_data->name);
$newhs->addAttribute('ath', $json_data->xpos);
$newhs->addAttribute('atv', $json_data->ypos);
}
$xml->asXML($file);
?>
if the node doesn't exist then it's added, no problem, but if it exist the attribute values aren't changed.
php xpath nodes simplexml
add a comment |
up vote
0
down vote
favorite
I'm trying to modify a node attribute if the node exist or create it if not in a xml file using Xpath.
xml file looks like that :
<krpano>
<hotspot name="hs1" ath="0" atv="0"/>
<hotspot name="hs2" ath="0" atv="0"/>
</krpano>
and here is my php code :
<?php
$str_json = file_get_contents('php://input');
$json_data = json_decode($str_json);
$file = 'myxmlfile.xml';
$xml = simplexml_load_file($file);
$krpano = $xml->xpath("//krpano");
$hotspot = $xml->xpath('//hotspot[@name="'.$json_data->name.'"]');
if ($hotspot){
$xml->xpath('//hotspot[@name="'.$json_data->name.'"]/@ath->'.$json_data->xpos.'');
$xml->xpath('//hotspot[@name="'.$json_data->name.'"]/@atv->'.$json_data->ypos.'');
}else{
$newhs = $krpano[0]->addChild('hotspot');
$newhs->addAttribute('name', $json_data->name);
$newhs->addAttribute('ath', $json_data->xpos);
$newhs->addAttribute('atv', $json_data->ypos);
}
$xml->asXML($file);
?>
if the node doesn't exist then it's added, no problem, but if it exist the attribute values aren't changed.
php xpath nodes simplexml
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I'm trying to modify a node attribute if the node exist or create it if not in a xml file using Xpath.
xml file looks like that :
<krpano>
<hotspot name="hs1" ath="0" atv="0"/>
<hotspot name="hs2" ath="0" atv="0"/>
</krpano>
and here is my php code :
<?php
$str_json = file_get_contents('php://input');
$json_data = json_decode($str_json);
$file = 'myxmlfile.xml';
$xml = simplexml_load_file($file);
$krpano = $xml->xpath("//krpano");
$hotspot = $xml->xpath('//hotspot[@name="'.$json_data->name.'"]');
if ($hotspot){
$xml->xpath('//hotspot[@name="'.$json_data->name.'"]/@ath->'.$json_data->xpos.'');
$xml->xpath('//hotspot[@name="'.$json_data->name.'"]/@atv->'.$json_data->ypos.'');
}else{
$newhs = $krpano[0]->addChild('hotspot');
$newhs->addAttribute('name', $json_data->name);
$newhs->addAttribute('ath', $json_data->xpos);
$newhs->addAttribute('atv', $json_data->ypos);
}
$xml->asXML($file);
?>
if the node doesn't exist then it's added, no problem, but if it exist the attribute values aren't changed.
php xpath nodes simplexml
I'm trying to modify a node attribute if the node exist or create it if not in a xml file using Xpath.
xml file looks like that :
<krpano>
<hotspot name="hs1" ath="0" atv="0"/>
<hotspot name="hs2" ath="0" atv="0"/>
</krpano>
and here is my php code :
<?php
$str_json = file_get_contents('php://input');
$json_data = json_decode($str_json);
$file = 'myxmlfile.xml';
$xml = simplexml_load_file($file);
$krpano = $xml->xpath("//krpano");
$hotspot = $xml->xpath('//hotspot[@name="'.$json_data->name.'"]');
if ($hotspot){
$xml->xpath('//hotspot[@name="'.$json_data->name.'"]/@ath->'.$json_data->xpos.'');
$xml->xpath('//hotspot[@name="'.$json_data->name.'"]/@atv->'.$json_data->ypos.'');
}else{
$newhs = $krpano[0]->addChild('hotspot');
$newhs->addAttribute('name', $json_data->name);
$newhs->addAttribute('ath', $json_data->xpos);
$newhs->addAttribute('atv', $json_data->ypos);
}
$xml->asXML($file);
?>
if the node doesn't exist then it's added, no problem, but if it exist the attribute values aren't changed.
php xpath nodes simplexml
php xpath nodes simplexml
edited Nov 8 at 11:35
executable
808221
808221
asked Nov 8 at 11:14
jeromebg
62
62
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
You can't alter attributes like that with XPath,
$xml->xpath('//hotspot[@name="'.$json_data->name.'"]/@ath->'.$json_data->xpos.'');
$xml->xpath('//hotspot[@name="'.$json_data->name.'"]/@atv->'.$json_data->ypos.'');
you just need to modify them directly from the hotspot you already have...
$hotspot[0]['ath'] = $json_data->xpos;
$hotspot[0]['atv'] = $json_data->ypos;
GREAT !!! Thx a lot, you made my day ;)
– jeromebg
Nov 8 at 11:25
Please consider marking this (and any other of your questions) as answered - meta.stackexchange.com/questions/5234/…
– Nigel Ren
Nov 8 at 11:30
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
You can't alter attributes like that with XPath,
$xml->xpath('//hotspot[@name="'.$json_data->name.'"]/@ath->'.$json_data->xpos.'');
$xml->xpath('//hotspot[@name="'.$json_data->name.'"]/@atv->'.$json_data->ypos.'');
you just need to modify them directly from the hotspot you already have...
$hotspot[0]['ath'] = $json_data->xpos;
$hotspot[0]['atv'] = $json_data->ypos;
GREAT !!! Thx a lot, you made my day ;)
– jeromebg
Nov 8 at 11:25
Please consider marking this (and any other of your questions) as answered - meta.stackexchange.com/questions/5234/…
– Nigel Ren
Nov 8 at 11:30
add a comment |
up vote
0
down vote
You can't alter attributes like that with XPath,
$xml->xpath('//hotspot[@name="'.$json_data->name.'"]/@ath->'.$json_data->xpos.'');
$xml->xpath('//hotspot[@name="'.$json_data->name.'"]/@atv->'.$json_data->ypos.'');
you just need to modify them directly from the hotspot you already have...
$hotspot[0]['ath'] = $json_data->xpos;
$hotspot[0]['atv'] = $json_data->ypos;
GREAT !!! Thx a lot, you made my day ;)
– jeromebg
Nov 8 at 11:25
Please consider marking this (and any other of your questions) as answered - meta.stackexchange.com/questions/5234/…
– Nigel Ren
Nov 8 at 11:30
add a comment |
up vote
0
down vote
up vote
0
down vote
You can't alter attributes like that with XPath,
$xml->xpath('//hotspot[@name="'.$json_data->name.'"]/@ath->'.$json_data->xpos.'');
$xml->xpath('//hotspot[@name="'.$json_data->name.'"]/@atv->'.$json_data->ypos.'');
you just need to modify them directly from the hotspot you already have...
$hotspot[0]['ath'] = $json_data->xpos;
$hotspot[0]['atv'] = $json_data->ypos;
You can't alter attributes like that with XPath,
$xml->xpath('//hotspot[@name="'.$json_data->name.'"]/@ath->'.$json_data->xpos.'');
$xml->xpath('//hotspot[@name="'.$json_data->name.'"]/@atv->'.$json_data->ypos.'');
you just need to modify them directly from the hotspot you already have...
$hotspot[0]['ath'] = $json_data->xpos;
$hotspot[0]['atv'] = $json_data->ypos;
answered Nov 8 at 11:22
Nigel Ren
22.3k61832
22.3k61832
GREAT !!! Thx a lot, you made my day ;)
– jeromebg
Nov 8 at 11:25
Please consider marking this (and any other of your questions) as answered - meta.stackexchange.com/questions/5234/…
– Nigel Ren
Nov 8 at 11:30
add a comment |
GREAT !!! Thx a lot, you made my day ;)
– jeromebg
Nov 8 at 11:25
Please consider marking this (and any other of your questions) as answered - meta.stackexchange.com/questions/5234/…
– Nigel Ren
Nov 8 at 11:30
GREAT !!! Thx a lot, you made my day ;)
– jeromebg
Nov 8 at 11:25
GREAT !!! Thx a lot, you made my day ;)
– jeromebg
Nov 8 at 11:25
Please consider marking this (and any other of your questions) as answered - meta.stackexchange.com/questions/5234/…
– Nigel Ren
Nov 8 at 11:30
Please consider marking this (and any other of your questions) as answered - meta.stackexchange.com/questions/5234/…
– Nigel Ren
Nov 8 at 11:30
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53206619%2fmodify-attribute-if-node-exist-with-xpath%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown