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 基于双目测规则物体尺寸
  • ¥15 wegame打不开英雄联盟
  • ¥15 公司的电脑,win10系统自带远程协助,访问家里个人电脑,提示出现内部错误,各种常规的设置都已经尝试,感觉公司对此功能进行了限制(我们是集团公司)
  • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?
  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢
  • ¥15 不小心不正规的开发公司导致不给我们y码,