使用PHP生成XML文件

时间:2015-08-31 21:39:46

标签: php xml simplexml

我需要使用具有以下结构的PHP生成XML文件。此文件是测验的结构,每个问题有五个答案。问答文本附在CDATA中。

<?xml version="1.0" encoding="UTF-8"?>
<QuizMaster>
<header version="0.36" exportVersion="1" />
    <data>
        <quiz>
            <autostart>false</autostart>
            <forms activated="false" position="0" />
            <questions>
                <question answerType="single">
                    <questionText><![CDATA[What is the city?]]></questionText>
                    <correctMsg />
                    <incorrectMsg />
                    <answers>
                        <answer points="1" correct="true">
                            <answerText html="false"><![CDATA[Kolkata]]></answerText>
                        </answer>
                        <answer points="1" correct="false">
                            <answerText html="false"><![CDATA[Delhi]]></answerText>
                        </answer>
                        <answer points="1" correct="false">
                            <answerText html="false"><![CDATA[Mumbai]]></answerText>
                        </answer>
                        <answer points="1" correct="false">
                            <answerText html="false"><![CDATA[Chennai]]></answerText>
                        </answer>
                        <answer points="1" correct="false">
                            <answerText html="false"><![CDATA[Goa]]></answerText>
                        </answer>
                    </answers>
                </question>
            </questions>
        </quiz>
    </data>
</QuizMaster>

我尝试过以下PHP代码。它从第二个答案(//答案2)开始给出错误,其中声明了 answer 元素的属性。错误类似于警告:SimpleXMLElement :: addAttribute():属性已存在于第46行的C:\ wamp \ www \ quiz stack.php 中。我已经在这个项目的其他几个地方加入了这种编码,但在所有情况下,每个问题只有一个答案。

<?php
class SimpleXMLExtended extends SimpleXMLElement
{
    public function addCData($cdata_text)
    {
        $node = dom_import_simplexml($this);
        $no = $node->ownerDocument;
        $node->appendChild($no->createCDATASection($cdata_text));
    }
}

$xmlFile = 'quiz_file.xml';
$xml = new SimpleXMLExtended('<QuizMaster/>');
// header
$xml->header = NULL;
$xml->header->addAttribute('version', '0.36');
$xml->header->addAttribute('exportVersion', '1');
$xml->data = NULL;
// quiz
$xml->data->quiz = NULL;
$xml->data->quiz->autostart = ('false');
// forms
$xml->data->quiz->forms = NULL;
$xml->data->quiz->forms->addAttribute('activated', 'false');
$xml->data->quiz->forms->addAttribute('position', '0');
// Question Start
$xml->data->quiz->questions = NULL;
// Question answer loop starts here
$xml->data->quiz->questions->question = NULL;
$xml->data->quiz->questions->question->addAttribute('answerType', 'single');
// Question text
$xml->data->quiz->questions->question->questionText = NULL;
$xml->data->quiz->questions->question->questionText->addCData('What is the     city?');
$xml->data->quiz->questions->question->correctMsg = NULL;
$xml->data->quiz->questions->question->incorrectMsg = NULL;
// Answers start
$xml->data->quiz->questions->question->answers = NULL;
// Answer 1
$xml->data->quiz->questions->question->answers->answer = NULL;
$xml->data->quiz->questions->question->answers->answer->addAttribute('points', '1');
$xml->data->quiz->questions->question->answers->answer->addAttribute('correct', 'true');
$xml->data->quiz->questions->question->answers->answer->answerText = NULL;
$xml->data->quiz->questions->question->answers->answer->answerText->addAttribute('html', 'false');
$xml->data->quiz->questions->question->answers->answer->answerText->addCData('Kolkata');
// Answer 2
$xml->data->quiz->questions->question->answers->answer = NULL;
$xml->data->quiz->questions->question->answers->answer->addAttribute('points', '1');
$xml->data->quiz->questions->question->answers->answer->addAttribute('correct', 'false');
$xml->data->quiz->questions->question->answers->answer->answerText = NULL;
$xml->data->quiz->questions->question->answers->answer->answerText->addAttribute('html', 'false');
$xml->data->quiz->questions->question->answers->answer->answerText->addCData('Delhi');
// Answer 3
$xml->data->quiz->questions->question->answers->answer = NULL;
$xml->data->quiz->questions->question->answers->answer->addAttribute('points', '1');
$xml->data->quiz->questions->question->answers->answer->addAttribute('correct', 'false');
$xml->data->quiz->questions->question->answers->answer->answerText = NULL;
$xml->data->quiz->questions->question->answers->answer->answerText->addAttribute('html', 'false');
$xml->data->quiz->questions->question->answers->answer->answerText->addCData('Mumbai');
// Answer 4
$xml->data->quiz->questions->question->answers->answer = NULL;
$xml->data->quiz->questions->question->answers->answer->addAttribute('points', '1');
$xml->data->quiz->questions->question->answers->answer->addAttribute('correct', 'false');
$xml->data->quiz->questions->question->answers->answer->answerText = NULL;
$xml->data->quiz->questions->question->answers->answer->answerText->addAttribute('html', 'false');
$xml->data->quiz->questions->question->answers->answer->answerText->addCData('Chennai');
// Answer 5
$xml->data->quiz->questions->question->answers->answer = NULL;
$xml->data->quiz->questions->question->answers->answer->addAttribute('points', '1');
$xml->data->quiz->questions->question->answers->answer->addAttribute('correct', 'false');
$xml->data->quiz->questions->question->answers->answer->answerText = NULL;
$xml->data->quiz->questions->question->answers->answer->answerText->addAttribute('html', 'false');
$xml->data->quiz->questions->question->answers->answer->answerText->addCData('Goa');
// Write Data
$xml->saveXML($xmlFile);
?>

如果我从除 answer 1 之外的每个答案中注释掉有问题的两行(其中 addAttribute 应用于 answer 元素),那么生成文件,但只有一个答案包含答案5 中的文本。

在没有更改 SimpleXMLElement 对象的情况下,有人可以建议是否有解决此问题的方法吗?

1 个答案:

答案 0 :(得分:0)

您正在使用与单个answer节点一起使用的代码,您应该添加子代:

$answer2 = $xml->data->quiz->questions->question->answers->addChild('answer');
$answer2->addAttribute('points', '1');
$answer2->addAttribute('correct', 'false');
$answer2->answerText->addAttribute('html', 'false');
$answer2->answerText->addCData('Delhi');

$answer3 = $xml->data->quiz->questions->question->answers->addChild('answer');
$answer3->addAttribute('points', '1');
$answer3->addAttribute('correct', 'false');
$answer3->answerText->addAttribute('html', 'false');
$answer3->answerText->addCData('Mumbai');

将此方法用于所有答案(事件是第一个答案)。

现在,深入了解一下,您可能还需要为addChild节点使用answerText方法:

$answer3Text = $answer3->addChild('answerText');
$answer3Text->addAttribute('html', 'false');
$answer3Text->addCData('Mumbai');

因此,对于您的所有答案,代码应如下所示:

$answer3 = $xml->data->quiz->questions->question->answers->addChild('answer');
$answer3->addAttribute('points', '1');
$answer3->addAttribute('correct', 'false');
$answer3Text = $answer3->addChild('answerText');
$answer3Text->addAttribute('html', 'false');
$answer3Text->addCData('Mumbai');