【问题标题】:How do I serialize DOM into XML using PHP?如何使用 PHP 将 DOM 序列化为 XML?
【发布时间】:2010-09-18 07:32:33
【问题描述】:

我正在尝试以问题形式从我的 php Web 应用程序将 html 数据发送到 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 - 我很惊讶它是如此困难远的。

这在某种程度上是另一个线程的延续 - What PHP code will help me parse html data in an xml form?

【问题讨论】:

  • 明确地说,它就像没有 html 变量的文本一样完美地工作 - 现在它不能与 html 一起工作引用。我尝试了 cdata,但没有帮助。
  • 事实证明这些答案都不正确 - 我需要将文本更改为格式化内容

标签: php xml xml-serialization domdocument mechanicalturk


【解决方案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> ';

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 当然,"$dom->formatOutput = true;"是可选的,但它使人类更容易阅读 XML 输出。可以在您上线时删除以节省空白。
    猜你喜欢
    • 2013-06-17
    • 2011-04-03
    • 2011-10-07
    • 2011-07-24
    • 2012-01-05
    • 2014-01-20
    • 2010-10-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多