i want to replace the node value in my XML - php

This is my xml code.
I want to replace value of End,
for example, i.e, End = 'enddate' ;
End = 'somedate' ;
Please help
<SailingDateRange MaxDuration="P07D" MinDuration="P07D" End="enddate" Start="startdate"></SailingDateRange>
I also tried the below code
$sail_dat_to = $_GET['date'];
$xml->SailingDateRange->End = $sail_dat_to;
But it adding a new line rather than replace the value.
pLease help

$xml = simplexml_load_file('file.xml');
$xml->SailingDateRange['End'] = 'somedate';
$xml->asXml('file.xml');
Read more: http://php.net/manual/en/simplexml.examples-basic.php

Related

Update array in a file

I have an include with a single array in it that holds 3 instructions; a "y/n" switch and a start and end date. The include meetingParams.php looks like this:
<?php
$regArray = array("n","2018-03-03","2018-03-07");
?>
I want to update those array values from time to time using a web based form. Where I get stuck is finding the correct syntax to do that. Right now I have the following:
$registration = $_POST['registration'];
$startMeeting = $_POST['startMeeting'];
$endMeeting = $_POST['endMeeting'];
$replace = array($registration, $startMeeting, $endMeeting);
$search = file_get_contents('includes/meetingParams.php');
$parsed = preg_replace('^$regArray.*$', $replace, $search);
file_put_contents("includes/meetingParams.php", $parsed);
When I run this code, the file meetingParams.php get's replaced with an empty file. What am I missing?
This should work fine:
$content = '<?php
$regArray = array("'.$registration.'","'.$startMeeting.'","'.$endMeeting.'");
?>';
file_put_contents("includes/meetingParams.php", $content);
Try this.
include_once "includes/meetingParams.php";
$registration = $_POST['registration'];
$startMeeting = $_POST['startMeeting'];
$endMeeting = $_POST['endMeeting'];
$regArray = array($registration, $startMeeting, $endMeeting);
Explanation
There is no need to use file_get_contents since you are using a PHP file you can simply include it.
What that means is that you are placing that file inside your script. Then there is no need to use RegEx to replace the array, just reassign its value.

Unable to insertBefore on an XML file

I'm trying to write a script that will update an RSS XML file. I want it to take the existing file and add a new item to the top of the items list. I've previous gotten it to add to the end of the file, but now it's currently not adding the new item at all. I've been checking around online, but I still can't get it to work. Here is what I have so far:
$rssDoc = new DOMDocument();
$rss_file = $_SERVER['DOCUMENT_ROOT'].'/test_site/feed.xml';
$rssDoc->load($rss_file);
$items = $rssDoc->getElementsByTagName('item');
$newItem = $rssDoc->createElement('item');
$rssTitle = $rssDoc->createElement('title');
$rssTitle->appendChild($rssDoc->createTextNode($title));
$newItem->appendChild($rssTitle);
$rssDesc = $rssDoc->createElement('description');
$rssDesc->appendChild($rssDoc->createTextNode($string));
$newItem->appendChild($rssDesc);
$rssLink = $rssDoc->createElement('link');
$rssLink->appendChild($rssDoc->createTextNode($link));
$newItem->appendChild($rssLink);
$rssDate = $rssDoc->createElement('pubDate');
$rssDate->appendChild($rssDoc->createTextNode($pubDate));
$newItem->appendChild($rssDate);
$firstItem = $items->item(0);
$firstItem->insertBefore($newItem,$firstItem->firstChild);
$rssDoc->formatOutput = true;
echo $rssDoc->saveXML();
What am I missing?
"What am I missing?"
The output: $rssDoc->save( 'filename.xml' ) http://php.net/domdocument.save.php
I got it working. I changed these lines:
$firstItem = $items->item(0);
$firstItem->insertBefore($newItem,$firstItem->firstChild);
To this line:
$items->item(0)->parentNode->insertBefore($newItem,$items->item(0));

XML Address Trouble

I have this xml that I want to insert some info, the code is working fine but when it comes to the address of the XML I'm having some trouble.
I have a link in other page with:
produtoadicionado.php?page=adicionar&cod_produto=1&id=1&nome_produto=Arroz
What I want is complete the name of the XML with the value of $id before. Which in this case is 1.
So it will call and add in: 1_produtos.xml
But is not working. Is creating a new xml named $id_produtos.xml
produtoadicionado.php
<?php
$page = $_GET["page"];
$cod_produto = $_GET["cod_produto"];
$id = $_GET["id"];
$nome = $_GET["nome_produto"];
if ($page == 'adicionar')
{
$xml = simplexml_load_file('$id_produtos.xml');
$produto = $xml->addChild('produto');
$produto->addChild('nome', $nome);
$produto->addChild('cod', $cod_produto);
file_put_contents('$id_produtos.xml', $xml->asXML());
}
?>
Please help me!
Change these lines:
simplexml_load_file('$id_produtos.xml');
file_put_contents('$id_produtos.xml', $xml->asXML());
into this:
simplexml_load_file("{$id}_produtos.xml");
file_put_contents("{$id}_produtos.xml", $xml->asXML());
Please, note the use of double quote instead of the single quote which let PHP to interpret the vars name and replace that with their value. You can read more here.

PHP Retrieve DATA from XML

My first attempt at retrieving data from XML for a maps application has failed. Here is a piece of the XML Feed.
<?xml version="1.0" encoding="UTF-8"?>
<DirectionsResponse>
<status>OK</status>
<route>
<leg>
<start_address>Winkfield, Bracknell, Berkshire RG42 6LY, UK</start_address>
<end_address>Wentworth, Surrey GU25 4, UK</end_address>
</leg>
</route>
</DirectionsResponse>
I want to get the start and end address and return them via AJAX to the application.
The PHP
<?php
$start = $_POST['start'];
$end = $_POST['end'];
$xml = simplexml_load_file('http://maps.googleapis.com/maps/api/directions/xml?origin='.$start.'&destination='.$end.'&sensor=false');
// data to fetch
$start = $xml->xpath("/DirectionsResponse/route/leg/start_address");
$end = $xml->xpath("/DirectionsResponse/route/leg/end_address");
$start = array($start);
// output
echo json_encode( array('output'=>$start[0]));
?>
Annoyingly this is returning an object to the page.
Response :: {"output":[{"0":"Winkfield, Windsor, Berkshire SL4 2ES, UK"}]}
Anyone know how to stop that from happening. I just want the value Winkfield, Windsor, Berkshire SL4 2ES, UK.
Haven't tested your specific case, but i remember running into something similar when using SimpleXML, you might want to use (string) to cast it out of the object
array('output'=> (string)$start[0])
Or rather just leave out $start = array($start) and just do
array('output'=> (string)$start)
On reading the SimpleXML XPath documentation (http://www.php.net/manual/en/simplexmlelement.xpath.php) again i think your problem might be this:
Returns an array of SimpleXMLElement objects or FALSE in case of an error.
So the XPath returns an array, then you wrap that in an array and take the first element of that array, so all you end up with is the original array - remove the array wrap and you should be fine
function XMLReader()
{
$MyArray = array();
$doc = new DOMDocument();
$doc->load( 'XMLFilePath.xml' );
$info = $doc->getElementsByTagName( "leg" );
foreach( $info as $Type )
{
$details = $Type->getElementsByTagName( "start_address" );
$detail = $details->item(0)->nodeValue;
$MyArray [] = $detail;
}
return $MyArray;
}
and same for end_Address.
I wish this answer is helpful.
Just echo $start? Also, why do you make an array of start and then output the first element, it doesnt make any sense at all.
echo $xml->route->leg->start_address;
This is the edited answer. I checked it. Its working properly on the XML.

A weird result, as a result of this PHP script

require_once 'include/BestBuy/Service/Remix.php';
$skuid = rawurldecode($_GET['skuid']);
$apiKey = 'tfvs7h89pwn4pzmyj9nxemmg'; // Your API key
$remix = new BestBuy_Service_Remix($apiKey);
$result = $remix->product('$skuid','json')
->show(array('url'))
->query();
$data = json_decode ($result, true);
$feed = $data['url'];
print <<< FEEDS
$feed
FEEDS;
When I put this script into my page, the $feed will echo the current URL.
But when I manually supply the script with an integer, replacing ($skuid) it will be successful.
It's really weird, But I think it has something to do with me using a variable in that specific array.
And it is also weird, because It was working before I re arranged some of the HTML.
I'm trying to approach this problem the most logical way.
Please help.
Thanks.
should you have $skuid in quotes? I would expect:
$result = $remix->product($skuid,'json')
rather than
$result = $remix->product('$skuid','json')

Categories