What PHP code will help me parse html data in an xml form?

人盡茶涼 提交于 2019-12-11 18:33:03

问题


FalseAWS.MechanicalTurk.XMLParseErrorThere was an error parsing the XML question or answer data in your request. Please make sure the data is well-formed and validates against the appropriate schema. (1284779956270)Array00

I'm trying to send entire emails to mechanical turk, and I am using the mtturk.lib.php library to send this. I tried urlencode and htmlentities to attempt to send it, I'm sure there's a function that will make this code "formatted well enough" to send it through.

$thequestion = '<a href="linkgoeshere" target="_blank">click here</a>';

$QuestionXML = '<QuestionForm xmlns="http://mechanicalturk.amazonaws.com/AWSMechanicalTurkDataSchemas/2005-10-01/QuestionForm.xsd">
  <Question>
    <QuestionContent>
      <Text>'.$thequestion.'</Text>
    </QuestionContent>
    <AnswerSpecification>
      <FreeTextAnswer/>
    </AnswerSpecification>
  </Question>
</QuestionForm> ';

回答1:


HTML is not a form of XML; don't try to parse it as such. Your best bet is to use a HTML5 parser, or, failing to obtain that, an SGML parser.




回答2:


html code inside an xml document may be embedded in a number of ways:

  1. escape it all the hard way with htmlspecialchars() and send it
  2. escape it with a <![CDATA[ ... ]]> section
  3. convert it to XHTML, specify the right namespace in an xmlns attribute

different xml parsers may support or not the third method, I'd go with the first or the second one.



来源:https://stackoverflow.com/questions/3740257/what-php-code-will-help-me-parse-html-data-in-an-xml-form

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!