When I try to get values it doesn't seem to work. The xml file contains multiples of the same node <post> and I thought you could call a specific node like an array, but that doesn't seem to work. Below I have included the code.
File that gets xml values:
<?php
if(file_exists('settings.xml')){
$settings = simplexml_load_file('settings.xml');
$site_title = $settings->title;
$site_name = $settings->title;
$site_stylesheet = "css/main.css";
$theme_folder = "themes/".$settings->theme;
if(file_exists('posts.xml')){
$posts = simplexml_load_file('posts.xml');
$post = $posts->post[$_GET['post']];
$post_title = $post->title;
$post_content = $post->content;
$post_author = $post->author;
include($theme_folder."/header.php");
include($theme_folder."/post.php");
include($theme_folder."/footer.php");
} else {
exit("File \"posts.xml\" does not exist!");
}
} else {
exit("File \"settings.xml\" does not exist!");
}
?>
File that is included in the file above and uses the variables that the xml passes to:
<article>
<h1><?php echo $post_title ?></h1>
<?php echo $post_content ?>
<p><?php echo $post_author ?></p>
</article>
Xml file:
<?xml version="1.0" encoding="utf-8"?>
<posts>
<post>
<title>Hello World</title>
<author>tacticalsk8er</author>
<content>Hello world this is my blogging site</content>
</post>
<post>
<title>Hello Again</title>
<author>Nick Peterson</author>
<content><![CDATA[Hello Again world this is another test for my <b>blogging site</b>]]></content>
</post>
</posts>
Of course, you can access nodes in simplexml 'array-style':
$post = $xml->post[1]; // select second node
echo $post->title;
$xml represents the root-node <posts>, $xml->post selects posts/post.
see it working: http://3v4l.org/KOiv2
see the manual: http://www.php.net/manual/en/simplexml.examples-basic.php
Related
Hello stackoverflow community,
this code display the url for my sitemap,
the connection work correctly and display the url list (not very well)
the error code i can see in the header
This page contains the following errors:
error on line 2 at column 18210: xmlParseEntityRef: no name
Below is a rendering of the page up to the first error.
<?php
header('Content-type: application/xml; charset=utf-8') ?>
<?php
echo '<?xml version="1.0" encoding="UTF-8"?>' ?>
<?php
define('DBHOST','localhost');
define('DBUSER','user');
define('DBNAME','database');
define('DBPWD','password');
$connect = new MySQLi(DBHOST,DBUSER,DBPWD,DBNAME)or die(mysqli_error());
echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
echo '<url>';
$query = $connect->query("select * from rss");
while($row=$query->fetch_array(MYSQL_ASSOC))
{
echo '<loc>'.$row['link'].'</loc>';
}
echo '</url>';
echo '</urlset>';
?>
the code above output this (does this look correct ?)
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url><loc>http://www.exemple.com/d/brown</loc>
<loc>http://www.exemple.com/d/blue</loc>
<loc>http://www.exemple.com/d/red/</loc>
<loc>http://www.exemple.com/d/yellow</loc>
//more lines
</url></urlset>
i would like to know what i did wrong on this code
thanks you very much
(French Pierre)
i fixed the problem ,the error came from one field in the rss table contain a bad character ..., but i have added missing xml parameters
lastmod, changefreq, priority
<?php
define('DBHOST','localhost');
define('DBUSER','user');
define('DBNAME','rss');
define('DBPWD','password');
$connect = new MySQLi(DBHOST,DBUSER,DBPWD,DBNAME)or die(mysqli_error());
header("Content-Type: text/xml;charset=iso-8859-1");
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">";
{
$query = $connect->query("select * from rss");
while($row=$query->fetch_array(MYSQL_ASSOC)) {
$url = $row["link"];
$time = $row["when"];
$lastmod = $time;
$datetime = new DateTime($lastmod);
$timeresult = $datetime->format('Y-m-d\TH:i:sP');
echo "<url>
<loc>$url</loc>
<lastmod>$timeresult</lastmod>
<changefreq>hourly</changefreq>
<priority>0.8</priority>
</url>
";}}
?>
</urlset>
like this the output does not give error anymore
it does pass google validation
thanks you
I have an XML file with this structure:
<markers>
<marker>
<marker_id>...</marker_id>
<map_id>...</map_id>
<title>Test Location 1</title>
<address>Blah Blah Blah</address>
<desc/>
<pic/>
<icon>...</icon>
<linkd/>
<lat>...</lat>
<lng>...</lng>
<anim>...</anim>
<category>...</category>
<infoopen>...</infoopen>
</marker>
</markers>
Basically it's pulling data from a Google Maps location XML file.
I need to echo the bit only
Here's what I have so far:
<?php
$url = 'myurl/1markers.xml';
$file = file_get_contents($url);
$xml = simplexml_load_string($file);
foreach($xml->markers as $x) {
$location = $x->marker->title;
echo $location;
}
?>
It doesn't seem to be echoing...?
I've probably not done it right somewhere in the foreach, can anybody see what I'm missing?
Thanks
Mark
Use simplexml_load_file()
<?php
$xml = simplexml_load_file("new.xml");
foreach($xml as $x) {
$location = $x->title;
echo $location;
}
?>
markers is a root tag. Refering to http://php.net/manual/en/simplexml.examples-basic.php, your code should look like:
foreach($xml->marker as $x) {
echo $x->title;
}
Could be useful to replace the file_get_contents: http://www.php.net/manual/en/function.simplexml-load-file.php
<?php
$XML = <<<'XML'
<markers>
<marker>
<marker_id>...</marker_id>
<map_id>...</map_id>
<title>Test Location 1</title>
<address>Blah Blah Blah</address>
<desc/>
<pic/>
<icon>...</icon>
<linkd/>
<lat>...</lat>
<lng>...</lng>
<anim>...</anim>
<category>...</category>
<infoopen>...</infoopen>
</marker>
</markers>
XML;
$xml = simplexml_load_string($XML);
//This seems to fix things, issue was that you were trying to access the root tag which is implied.
foreach($xml->marker as $x) {
$location = $x->title;
var_dump($location);
}
?>
I've been looking for answers to my problem on SW but no luck so far..so here it goes.
I'm writing a form where the user can search for items that are written in a xml file on the server...the search function works pretty well; the form sends the values to a php file and using simplexml it retrieves the data from the xml file using a foreach with an if/else statement. However, when there is no item found on the xml file, that's where the issue comes.
Here's my php:
<?php
$zip = $_POST['zip'];
$id = $_POST['id'];
$xml = simplexml_load_file("lista.xml");
foreach ($xml as $entry){
if (($entry->zipCode == $zip) && ($entry->id == $id)){
?>
<p>Id: <?php echo $entry->id;?></p>
<p>Zip: <?php echo $entry->zipCode;?></p>
<p>Item: <?php echo $entry->item;?></p>
<?php
}
else {echo 'nothing found';}
}?>
And this is my xml:
<?xml version="1.0" encoding="utf-8"?>
<entries>
<entry>
<id>id1</id>
<zipCode>zip1</zipCode>
<item>1</item>
</entry>
<entry>
<id>id2</id>
<zipCode>zip2</zipCode>
<item>2</item>
</entry>
<entry>
<id>id3</id>
<zipCode>zip3</zipCode>
<item>3</item>
</entry>
</entries>
The issue is that instead of showing 'nothing found' just once if there is no item within the whole xml file, it shows 'nothing found' in every entry that does not have the search query on it.
So, for instance, if $zip = zip4 and $id = id4 the answer is:
nothing found
nothing found
nothing found
instead of just one 'nothing found'
What's the correct way of writing that piece of code??
Thanks everyone in advance!!!
Use a variable to keep track of whether you found anything.
$found = false;
foreach ($xml as $entry){
if (($entry->zipCode == $zip) && ($entry->id == $id)){
$found = true;
?>
<p>Id: <?php echo $entry->id;?></p>
<p>Zip: <?php echo $entry->zipCode;?></p>
<p>Item: <?php echo $entry->item;?></p>
<?php
}
}
if (!$found) {
echo 'nothing found';
}
I know i have bothered all of you with my questions but i have a question about php and xml
i am trying to store all the values of the pages in xml so i would create a multilingual website
after searching i have got to a way but and i tried to alter it a little bit
there is my xml files:
en.xml:
<?xml version="1.0" encoding="UTF-8"?>
<main>
<!--Page Titles-->
<freeEaHdr>
<![CDATA[Get in the game!]]>
</freeEaHdr>
<freeEaSubhdr>
<![CDATA[Load up your Xperia™ PLAY with 4 exciting EA titles for<span style="color:#ff9c00;"> FREE</span>]]>
</freeEaSubhdr>
<!--Page Titles-->
</main>
and there is my ar.xml
<?xml version="1.0" encoding="UTF-8"?>
<main>
<!--Page Titles-->
<freeEaHdr>
<![CDATA[ادخل اللعبة]]>
</freeEaHdr>
<freeEaSubhdr>
<![CDATA[حمل الاكسبيريا الان]]>
</freeEaSubhdr>
<!--Page Titles-->
</main>
and i have created an select_lang.php
<?php
function select_lang(){
if(isset($_GET['lang'])){
$lang = $_GET['lang'];
if($lang == "en"){
$xml = simplexml_load_file("en.xml");
} else{
$xml = simplexml_load_file("ar.xml");
}
} else {
$xml = simplexml_load_file("en.xml");
}
return $xml;
}
?>
and the final page was index.php
<?php
include("select_lang.php");
select_lang();
?>
<div><?php echo $xml->freeEaHdr; ?></div>
<div><?php echo $xml-> freeEaSubhdr; ?></div>
english
arabic
now of course i get errors in index.php as for the main xml variable is not defined so if anybody has a solution
Thanks in advance
and sorry for bothering you
Your select_lang() function returns the created SimpleXML object $xml. You then try to use this object in your index.php file, but you haven't actually assigned the return value of select_lang() to anything.
Simply doing
$xml = select_lang();
instead of
select_lang();
will let you actually use the returned XML object in your index.php file.
try to:
<?php
include("select_lang.php");
$xml = select_lang(); //change this line
?>
<?php
include "../music/php/logic/core.php";
include "../music/php/logic/settings.php";
include "../music/php/logic/music.php";
$top = "At world's end";
// create doctype
$dom = new DOMDocument("1.0");
header("Content-Type: text/xml");
?>
<music>
<?php $_xml = "<title>".$top."</title>";
echo $_xml; ?>
</music>
I'm using this code to generate a dynamic XML document. The file is saved as PHP.
My problem is that I can't echo php variables into the xml. However I can echo "literal" type text. I can't see anything wrong with my approach, it just doesn't work!
I'm pretty new to XML so I've probably missed something glaringly simple.
I've also tried lines like:
<title><?php echo $top; ?></title>
You don't use DOM this way. You use the DOM API to create the entire document:
$doc = new DOMDocument();
$books = $doc->createElement( "books" );
$doc->appendChild( $books );
// ...
See:
http://www.ibm.com/developerworks/library/os-xmldomphp/
http://www.tonymarston.net/php-mysql/dom.html
https://web.archive.org/web/1/http://articles.techrepublic%2ecom%2ecom/5100-10878_11-6141415.html
A more verbose example (generating XHTML with DOM)
// Create head element
$head = $document->createElement('head');
$metahttp = $document->createElement('meta');
$metahttp->setAttribute('http-equiv', 'Content-Type');
$metahttp->setAttribute('content', 'text/html; charset=utf-8');
$head->appendChild($metahttp);
See this tutorial on how to use DOM for XHTML. For reuse of code, you can write your own classes extending DOM classes to get configurable components.
If you don't want to use DOM or want to use plain text for generating the XML, just approach it like any other template, e.g.
<root>
<albums>
<album id="<?php echo $albumId; ?>">
<title><?php echo $title; ?></title>
... other elements ...
</album>
</albums>
</root>
You can store your XMl string in a .php file then render it to get final formatted XMl string. In many cases simpler than playing with XMl writers
File template.php
<root>
<albums>
<album id="<?php echo $data['albumId']; ?>">
<title><?php echo $data['title']; ?></title>
... other elements ...
</album>
</albums>
</root>
Render
function render($template, array $data)
{
ob_start();
include $template;
return ob_get_clean();
}
I think it's echo($_xml);