0

我正在尝试将 html 数据以问题形式从我的 php Web 应用程序发送到机械土耳其人,以便用户可以从电子邮件中查看整个 html 文档以使用。到目前为止,我遇到了困难。在下面链接的线程中,我尝试使用 html5-lib.php 解析 html 数据,但我认为我仍然缺少一个步骤来完成此操作。

这是我收到的当前错误:

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> ';

我不是 100% 确定解析器是否是我需要做的才能正确发送它 - 我想做的就是通过这个 xml 类型的文档发送 html - 我很惊讶到目前为止它是如此困难。

这在某种程度上是另一个线程的延续 - 哪些 PHP 代码将帮助我以 xml 形式解析 html 数据?

4

2 回答 2

1

查看DOMDocument以在 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> ';
于 2010-09-18T07:43:40.280 回答
0

不确定你到底在追求什么。这就是我创建需要传输的 XML 的方式。如果我误解了这个问题,请告诉我

根据 .xsd 文件,您似乎还缺少 QuestionIdentifier 节点。

<?
$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);
?>
于 2010-09-18T15:39:12.300 回答