1

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?

4
  • I actually have not much idea about PHP, but I would guess that load(question.php") doesn't load the response given by the web server, but instead loads the actual source file questions.php, hence you have no starting <` Commented Apr 22, 2014 at 13:40
  • @dirkk question.php is showing the xml format. Please check my output Commented Apr 22, 2014 at 13:41
  • Yes, if you are running it via the web server, i.e. access it through your browser. But the file itself is a PHP source file as you have shown in your question. Commented Apr 22, 2014 at 13:43
  • @dirkk yes you are right. I have tested if i write static content questions.xml instead of questions.php i am getting the correct output. But how can i use it in with php extension. This is dynamic content. Commented Apr 22, 2014 at 13:46

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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>

1 Comment

So you get an error and your reaction is to simply suppress the error message? Wow, that is terrible advice. Error messages usually have a reason and a programmer should consider them.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.