I'm trying to store a xml as string in a variable so that I can store it in my database.
$xml = "<root>";
foreach(...){
$xml .= "<user id='$id'/>";
}
$xml .= "</root>";
When I echo it, it's not displayed at all as if my web brower reads it as html tag. It doesn't even look like $xml is storing those as texts. Now, I'm trying to do it with DOMDocument... not not quite successful yet. Any tips? :(
Edited my stupid += mistakes..
PHP uses a . as a concatenate operator, or a .= as a shortcut, not a + or +=.
$xml = "<root>";
foreach(...){
$xml .= "<user id='$id'/>";
}
$xml .= "</root>";
Related
Hi I have a Api list that is been generated using php here is the code that is been used to generate the list
<html>
<title> API LIST</title>
<body>
<?php
$jsondata = file_get_contents("api_link");
$json = json_decode($jsondata, true);
$output = "<ul>";
foreach($output['A'] as $schools){
$output .= "<li>".$schools['name']."</li>";
}
$output .="</ul>";
echo $output;
?>
the list is populated successfully but how can i add a link to the list so that when a user click on one of the items it opens a particular linked page here are the ways i have tried
$output .= "<li ".$schools['name']."</li>";
$output .= "<li>".$schools['name']. "</li>";
$output .= "<li>".$schools['name']."</li>";
I don't know where or how to add the a href code
Use
$output .= '<li>'.$schools['name'].'</li>';
You just need some changes in your code.You can try for
$output .= '<a href=https://www.google.com'.$schools['name'].'><li>'.$schools['name'].'</li></a>';
As per the comments you can use . to concatenate(join) the string and variable in php.
i have the following code.
$stmt = $conn->prepare('SELECT sponsor_path FROM sponsors WHERE conference_id = ?');
$stmt->execute(array($cid));
$sponsorsImg = $stmt->fetchall (PDO::FETCH_OBJ);
$xml_output = "<?xml version=\"1.0\" ?>";
$xml_output .= "<sponsors>";
foreach ($sponsorsImg as $spImg){
$xml_output .= "<sponsor>\n";
$xml_output .= "<image>".$spImg->sponsor_path."</image>\n";
$xml_output .= "</sponsor>\n";
}
$xml_output .= "</sponsors>";
echo $xml_output;
Instead of printing the results in xml format i get only the $spImg->sponsor_path's content in plain text. Any ideas?
Most browsers will render markup that looks like XML. So it might be the case that the XML that you want is actually produced but not displayed correctly on the browser.
Try the following:
Set the content type before outputting:
header('Content-Type: text/xml');
echo $xml_output;
If the output looks the same, then right click on the page and select view source. You should see your XML markup as intended there.
I am display mysql data in xml file using php.
there I used this one in here i want to give line breaks .if we give line breaks that will display line break tag in content ..we have to give html tags .but we dont show them in xml content ...
the output is coming like this..
At the same time I want to remove that empty p tags also ...that is.
<![CDATA[ <p> </p>]]>
this is the code i have written for xml ...
please solve this problems
header("Content-Type: application/xml; charset=utf-8");
date_default_timezone_set("Asia/Calcutta");
$this->view->data=$this->CallModel('posts')- >GalleryAndContent();
$xml = '
';
$xml.='Thehansindia
http://www.thehansindia.com
Newspaper with a difference';
foreach($this->view->data as $values)
{
$output=strip_tags($values['text_data'],"");
$output = preg_replace('/(<[^>]+) style=".?"/i', '$1',$output);
$output = preg_replace('/(<[^>]+) class=".?"/i', '$1', $output);
$output=preg_replace( '/style=(["\'])[^\1]?\1/i', '', $output, -1 );
$output=preg_replace("/<([a-z][a-z0-9])[^>]*?(/?)>/i",'',$output);
$output=str_replace(array("",""),array("",""),$output);
$output=str_replace(array("",""),array("",""),,$output);
//$xml.="<CONTENT>"."<![CDATA[".$output."]]>"."</CONTENT>
$xml.= '<item>';
$dom = new DOMDocument;
#$dom->loadHTML($output);
$xml.="<CONTENT>";
foreach ($dom->getElementsByTagName('p') as $tag){
//$tag->nodeValue=str_replace("<![CDATA[ <p> </p> ]]>","",$tag->nodeValue);
if(!empty($tag->nodeValue)){
//$tag->nodeValue=str_replace("<![CDATA[ <p>& & &</p> ]]>","",$tag->nodeValue);
$xml.="<![CDATA["."<p>".stripslashes($tag->nodeValue)."</p>"."]]>";
}
}
$xml.="</CONTENT>";
$xml.= ' </item>';
}
Example:
//Next replace all new lines with the unicode:
$xml = str_replace("\n","
", $xml);
Reference Link
I am trying to take form data (via _POST) and write it to a document using SimpleXML. This is what I have tried and I can't seem to get it to work.
<?php
$title = $_POST['title'];
$link = $_POST['link'];
$description = $_POST['description'];
$rss = new SimpleXMLElement($xmlstr);
$rss->loadfile("feed.xml");
$item = $rss->channel->addChild('item');
$item->addChild('title', $title);
$item->addChild('link', $link);
$item->addChild('description', $description);
echo $rss->asXML();
header("Location: /success.html");
exit;
?>
Any help or a point in the right direction would be much appreciated.
You use the asXML() function wrong. If you want to write your XML to a file, you must pass a filename parameter to it. Check the SimpleXMLElement::asXML manual
so your code line oututing xml should be changed from
echo $rss->asXML();
to
$rss->asXML('myNewlyCreatedXML.xml');
rather than using SimpleXMLElement you can create XML directly like this
$xml = '<?xml version="1.0" encoding="utf-8"?>';
$xml .= '<item>';
$xml .= '<title>'.$title.'</title>';
$xml .= '<link>'.$title.'</link>';
$xml .= '<description>'.$title.'</description>';
$xml .= '</item>';
$xml_file = "feed.xml";
file_put_contents($xml_file,$xml);
may this will help you
There are a few problems with your code
<?php
$title = $_POST['title'];
$link = $_POST['link'];
$description = $_POST['description'];
//$rss = new SimpleXMLElement($xmlstr); // need to have $xmlstr defined before you construct the simpleXML
//$rss->loadfile("feed.xml");
//easier to just load the file when creating your XML object
$rss = new SimpleXML("feed.xml",null,true) // url/path, options, is_url
$item = $rss->channel->addChild('item');
$item->addChild('title', $title);
$item->addChild('link', $link);
$item->addChild('description', $description);
//header("Location: /success.html");
//if you want to redirect you should put a timer on it and echo afterwards but by
//this time if something went wrong there will be output already sent,
//so you can't send more headers, i.e. the next line will cause an error
header('refresh: 4; URL=/success.html');
echo $rss->asXML(); // you may want to output to a file, rather than the client
// $rss->asXML('outfputfile.xml');
exit;
?>
I am trying to load in XML to populate a search box. The code I have works fine with a static xml file, but I need to load in a PHP file to generate a dynamic XML file based on data from a database.
I knwo the PHP file generates the XML as it loads in a browser and the page source shows the correct nodes etc, however when I reference the .php file in my JavaScript it fails to load, or show an error... Using the .xml file (which is a replica of the php output source) it loads fine.
I would like someone to check to see if I am encoding the XML correctly or missing something in my JavaScript... and advice will be greatly appreciated.
JS
var myArr = [];
$.ajax({
url: "people.php", // change to full path of file on server
type: "GET",
dataType: "xml",
success: parseXml,
complete: setupAC,
failure: function(data) {
alert("XML File could not be found");
}
});
function parseXml(xml){
$(xml).find("person").each(function(){
var thisItem = $(this).find('name').text();
myArr.push(thisItem);
alert(thisItem);
});
}
PHP
<?php
include '../../inc_global/connect.php';
$query = 'SELECT * FROM candidates';
$result = mysql_query($query, $link);
$xmlOutput = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
$xmlOutput .= "<people>\n";
while ($line = mysql_fetch_assoc($result)) {
$xmlOutput .= "<person>\n";
$xmlOutput .= "<name>" . $line['name'] . "</name>\n";
$xmlOutput .= "</person>\n";
}
$xmlOutput .= "</people>\n";
echo $xmlOutput;
mysql_close($link);
?>
Try to put header right before outputting information:
....
header ("Content-Type:text/xml");
echo $xmlOutput;
....
Fullcode:
<?php
include '../../inc_global/connect.php';
$query = 'SELECT * FROM candidates';
$result = mysql_query($query, $link);
$xmlOutput = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
$xmlOutput .= "<people>\n";
while ($line = mysql_fetch_assoc($result)) {
$xmlOutput .= "<person>\n";
$xmlOutput .= "<name>" . $line['name'] . "</name>\n";
$xmlOutput .= "</person>\n";
}
$xmlOutput .= "</people>\n";
header ("Content-Type:text/xml"); //<----------- xml header here
echo $xmlOutput;
mysql_close($link);
?>
Check what's happening in php pointing your browser to people.php.
Anyway I advice you to:
drop mysql_ for PDO
As mysql_ funcs are bad and will prevent from meeting chicks, while PDO helps;
use SimpleXML against raw XML
SimpleXML helps you structure your code without getting mad in chaining strings.
give proper HTTP header to your document
You are generating an XML document, let the browser be aware of that:
header ("Content-Type:text/xml");
echo $xmlOutput;
Can you make sure that the function parseXml(xml) doesn't need any parameter when it is called on success of ajax call. Please note that I haven't tried the code but just a guess is here.