如何使用PHP将DOM序列化为XML?
我正试图将问题表单中的html数据从我的php web应用程序发送到mechanical turk,以便用户可以通过电子邮件查看整个html文档。到目前为止,我遇到了困难。在下面链接的线程中,我尝试使用html5-lib.php解析html数据,但我认为我仍然缺少一个步骤来完成这项工作 以下是我收到的当前错误:如何使用PHP将DOM序列化为XML?,php,xml,xml-serialization,domdocument,mechanicalturk,Php,Xml,Xml Serialization,Domdocument,Mechanicalturk,我正试图将问题表单中的html数据从我的php web应用程序发送到mechanical turk,以便用户可以通过电子邮件查看整个html文档。到目前为止,我遇到了困难。在下面链接的线程中,我尝试使用html5-lib.php解析html数据,但我认为我仍然缺少一个步骤来完成这项工作 以下是我收到的当前错误: Catchable fatal error: Object of class DOMNodeList could not be converted to string in urlgoe
Catchable fatal error: Object of class DOMNodeList could not be converted to string in urlgoeshere.php on line 35
这是我正在使用的当前代码
$thequestion = '<a href="linkgoeshere" target="_blank">click here</a>';
$thequestion = HTML5_Parser::parseFragment($thequestion);
var_dump($thequestion);
echo $thequestion;
//htmlspecialchars($thequestion);
$QuestionXML = '<QuestionForm xmlns="http://mechanicalturk.amazonaws.com/AWSMechanicalTurkDataSchemas/2005-10-01/QuestionForm.xsd">
<Question>
<QuestionContent>
<Text>'.$thequestion.'</Text> //<--- Line35
</QuestionContent>
<AnswerSpecification>
<FreeTextAnswer/>
</AnswerSpecification>
</Question>
</QuestionForm> ';
$thequestion='';
$thequestion=HTML5_解析器::parseFragment($thequestion);
var_dump($问题);
回应这个问题;
//htmlspecialchars($问题);
$QuestionXML='0
“.$thequestion.”/看看如何在PHP中使用DOM/xml。如果要在XML中嵌入HTML,请使用如下CDATA部分:
$QuestionXML = '<QuestionForm xmlns="http://mechanicalturk.amazonaws.com/AWSMechanicalTurkDataSchemas/2005-10-01/QuestionForm.xsd">
<Question>
<QuestionContent>
<Text><![CDATA['.$thequestion.']]></Text>
</QuestionContent>
<AnswerSpecification>
<FreeTextAnswer/>
</AnswerSpecification>
</Question>
</QuestionForm> ';
$QuestionXML='0
';
不确定你到底想要什么。这就是我创建需要传输的XML的方法。
如果我误解了这个问题,请告诉我
您似乎还缺少QuestionIdentifier节点,根据.xsd文件,它是必需的
<?
$dom = new DOMDocument('1.0','UTF-8');
$dom->formatOutput = true;
$QuestionForm = $dom->createElement('QuestionForm');
$QuestionForm->setAttribute('xmlns','http://mechanicalturk.amazonaws.com/AWSMechanicalTurkDataSchemas/2005-10-01/QuestionForm.xsd');
// could loop this part for all the questions of the XML
$thequestion = '<a href="linkgoeshere" target="_blank">click here</a>';
//Not sure what this is supposed to be, but its required. Check the specs of the app for it.
$questionID = "";
$Question = $dom->createElement('Question');
$QuestionIdentifier = $dom->createElement('QuestionIdentifier',$questionID);
$QuestionContent = $dom->createElement('QuestionContent');
$QuestionContent->appendChild($dom->createElement('Text',$thequestion));
$AnswerSpecification = $dom->createElement('AnswerSpecification');
$AnswerSpecification->appendChild($dom->createElement('FreeTextAnswer'));
$Question->appendChild($QuestionIdentifier);
$Question->appendChild($QuestionContent);
$Question->appendChild($AnswerSpecification);
$QuestionForm->appendChild($Question);
// End loop
$dom->appendChild($QuestionForm);
$xmlString = $dom->saveXML();
print($xmlString);
?>
createElement('Question');
$QuestionIdentifier=$dom->createElement('QuestionIdentifier',$questionID);
$QuestionContent=$dom->createElement('QuestionContent');
$QuestionContent->appendChild($dom->createElement('Text',$thequestion));
$AnswerSpecification=$dom->createElement('AnswerSpecification');
$AnswerSpecification->appendChild($dom->createElement('FreeTextAnswer'));
$Question->appendChild($QuestionIdentifier);
$Question->appendChild($QuestionContent);
$Question->appendChild($AnswerSpecification);
$QuestionForm->appendChild($Question);
//端环
$dom->appendChild($QuestionForm);
$xmlString=$dom->saveXML();
打印($xmlString);
?>
当然,“$dom->formatOutput=true;”是可选的,但它使人们更容易阅读XML输出。可以在你上线时删除以节省空白。说得清楚,它就像没有html变量的文本一样完美地工作-现在它在html中不起作用了。我尝试了cdata,但没有帮助。结果证明这两个答案都不正确-我需要将文本更改为格式化内容