I'm trying to accept xml that is submitted via a text area in an html form and create a SimpleXMLIterator object from it.
When the xml is submitted, it comes through I'm using the following to clean out the linebreaks and and decode the html entities:
$cleanedXml = stripslashes(html_entity_decode($xml));
But when I try to create my simpleXMLIterator from the result I get the error String could not be parsed as XML.
When I write the $cleanedXml variable to the error log the output look correct:
2015/01/03 22:16:32 [error] 2619#0: *907 FastCGI sent in stderr: "PHP message: <fmxmlsnippet type="FMObjectList">
<Step enable="True" id="39" name="Sort Records">
<NoInteract state="True"/>
<Restore state="True"/>
<SortList Maintain="True" value="True">
<Sort type="Ascending">
<PrimaryField>
<Field table="Company" id="1" name="ID"/>
</PrimaryField>
</Sort>
<Sort type="Ascending">
<PrimaryField>
<Field table="Company" id="2" name="Name"/>
</PrimaryField>
<SummaryField>
<Field table="Company" id="3" name="idSummart"/>
</SummaryField>
<OverrideLanguage language="English"/>
</Sort>
</SortList>
</Step>
</fmxmlsnippet>';"
I can take the xml from that error_log output, write it into the $cleanedXml variable as a heredoc, and successfully create a SimpleXMLIterator from it:
$cleanedXml = stripslashes( html_entity_decode($xml) );
error_log($cleanedXml); // grab the xml from the error log and heredoc-ed it in below:
$cleanedXml =<<<XML
<fmxmlsnippet type="FMObjectList">
<Step enable="True" id="39" name="Sort Records">
<NoInteract state="True"/>
<Restore state="True"/>
<SortList Maintain="True" value="True">
<Sort type="Ascending">
<PrimaryField>
<Field table="Company" id="1" name="ID"/>
</PrimaryField>
</Sort>
<Sort type="Ascending">
<PrimaryField>
<Field table="Company" id="2" name="Name"/>
</PrimaryField>
<SummaryField>
<Field table="Company" id="3" name="idSummart"/>
</SummaryField>
<OverrideLanguage language="English"/>
</Sort>
</SortList>
</Step>
</fmxmlsnippet>
XML;
$iterator = new \SimpleXMLIterator($cleanedXml);
die(var_dump([$iterator])); // Object successfully created
Is there something I'm missing here? If I'm able to do it from the error_log output, why wouldn't I be able to it directly from the variable?
No comments:
Post a Comment