如何使用 PHP 将 DOM 序列化为 XML?

发布于 2024-09-19 19:05:36 字数 1281 浏览 10 评论 0原文

我正在尝试将 html 数据以问题形式从我的 php Web 应用程序发送到 Mechanical Turk,以便用户可以从电子邮件中查看整个 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 数据?

I am attempting to send html data in a question form from my php web application to mechanical turk so a user can see the entire html document from an email to work with. I have had difficulty thus far. In the thread linked below, I attempted to parse the html data using html5-lib.php, but I think I'm still missing a step in order to complete this.

Here's the current error I'm receiving:

Catchable fatal error: Object of class DOMNodeList could not be converted to string in urlgoeshere.php on line 35

Here's the current code I'm working with...

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

I'm not 100% sure if the parser is what I need to do in order to have this sent correctly - All I want to do is send html through this xml type document - I'm surprised it's been this difficult thus far.

This is somewhat of a continuation of another thread -
What PHP code will help me parse html data in an xml form?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

请持续率性 2024-09-26 19:05:36

查看 DOMDocument 以了解如何使用 DOM/ PHP 中的 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> ';

Take a look at DOMDocument for working with DOM/xml in PHP. If you want to embed HTML in your XML, use CDATA sections like this:

$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> ';
柏林苍穹下 2024-09-26 19:05:36

不确定你到底在追求什么。这就是我创建需要传输的 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);
?>

Not sure exactly what you're after. This is how i would create the XML needed to be transferred.
Please let me know if i misunderstood the question

It also seems that you are missing the QuestionIdentifier node which is mandatory according to the .xsd file.

<?
$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);
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文