Reputation: 3414
Please check my code.
questions.php
<?php
include("db.php");
$dom = new DOMDocument("1.0");
$node = $dom->createElement("questions");
$parnode = $dom->appendChild($node);
$week = date("W");
$query = "SELECT * FROM ".QUIZ_QUESTION." WHERE week='".$week."'";
$result = mysql_query($query);
header("Content-type: application/xml; charset=ISO-8859-1");
while ($row = mysql_fetch_assoc($result)){
$node = $dom->createElement("question");
$question = $parnode->appendChild($node);
$ansAtrribute = $question->setAttribute("id",$row['id']);
$que = $dom->createElement("que",$row['question']);
$quenode = $question->appendChild($que);
$qryOpt = mysql_query("SELECT * FROM ".QUIZ_OPTION." WHERE question_id='".$row['id']."'");
while($gerOptions = mysql_fetch_array($qryOpt)){
$ans = $dom->createElement("ans",$gerOptions['option']);
$ansNode = $question->appendChild($ans);
$ansAtrribute = $ansNode->setAttribute("ans",$gerOptions['is_correct']);
}
}
echo $dom->saveXML();
?>
I am directly run the questions.php
here is the XML output:
Output:
-<questions>
-<question id="1">
<que>What is PHP?</que>
<ans ans="0">Fruit</ans>
<ans ans="1">Language</ans>
<ans ans="0">Game</ans>
</question>
</questions>
quiz.php
<?php
$xmlDoc = new DOMDocument("1.0");
$xmlDoc->load("questions.php");
$questions = $xmlDoc->getElementsByTagName("question");
$question= $questions->item($qnum)->getElementsByTagName("que")->item(0)->nodeValue;
$ans1= $questions->item($qnum)->getElementsByTagName("ans")->item(0)->nodeValue;
$ans1r= $questions->item($qnum)->getElementsByTagName("ans")->item(0)->getAttribute('ans');
$ans2= $questions->item($qnum)->getElementsByTagName("ans")->item(1)->nodeValue;
$ans2r= $questions->item($qnum)->getElementsByTagName("ans")->item(1)->getAttribute('ans');
$ans3= $questions->item($qnum)->getElementsByTagName("ans")->item(2)->nodeValue;
$ans3r= $questions->item($qnum)->getElementsByTagName("ans")->item(2)->getAttribute('ans');
?>
I am running the quiz.php
but the questions.php
doesn't load.
I am getting following error:
Warning: DOMDocument::load() [domdocument.load]: Start tag expected, '<' not found in file:///G:/xampp/htdocs/txtweb/quiz/questions.php, line: 30 in G:\xampp\htdocs\txtweb\quiz\quiz.php on line 3
Fatal error: Call to a member function getElementsByTagName() on a non-object in G:\xampp\htdocs\txtweb\quiz\quiz.php on line 6
I am posting my complete code. Please let me know where is error of my code?
Upvotes: 1
Views: 530
Reputation: 6218
By executing
$xmlDoc->load("questions.php");
you do load the PHP source file itself, not the rendered output. Your web server is responsible to interpret and execute a PHP file and then compute the result (which in this case will be the resulting XML file).
Sou to get the XML file you will have to use a HTTP request, something like
$xmlDoc->load("http://YOURURL/questions.php");
should work. Of course you will have to replace YOURURL
with your actual URL and path to the script.
Upvotes: 4
Reputation: 566
use libxml_use_internal_errors(TRUE);
in top of php file i.e. quiz.php like this
<?php
libxml_use_internal_errors(TRUE);
$xmlDoc = new DOMDocument();
$xmlDoc->load("questions.php");
this removes unnecesary internal warnings.
and update your xml file with this-
<rdf:RDF>
-<questions>
-<question id="1">
<que>What is PHP?</que>
<ans ans="0">Fruit</ans>
<ans ans="1">Language</ans>
<ans ans="0">Game</ans>
</question>
</questions>
</rdf:RDF>
Upvotes: 0