duanjue9296 2014-04-19 06:26
浏览 31
已采纳

如何在PHP中加载XML文档?

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?

  • 写回答

2条回答 默认 最新

  • duanqing3026 2014-04-22 13:55
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端