Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I load POST data into SimpleXML?

I need to post XML data from a textarea input to PHP so I can parse it and output a table.

I've tried a few methods and none seem to be working.

Currently I have this:

jQuery('#btnRegistrarXML').live('click', function(){
var xml;

if (jQuery('#txtRegXML').val() == ""){
  AddMsg('You must paste XML from the excel export into the textarea.', 'error');
} else {
  jQuery.post('registrar-xml-to-table.php', {xml:escape(jQuery('#txtRegXML').val())}, function(data){
    jQuery('#regXMLasTable').empty();
    jQuery('#regXMLasTable').append(data);
  });
}

displayMsgs();
});

The PHP is:

$xmlraw = urldecode($_POST['xml']);

$dom = new DOMDocument;
$dom->preserveWhiteSpace = FALSE;
$dom->loadXML($xmlraw);
$dom->formatOutput = TRUE;
$xmlstr = $dom->saveXml();

$xml = simplexml_load_string($xmlstr);
echo "<p>xmlraw:</p>";
echo $xmlraw;
echo "<p>xml:</p>";
echo $xml;

foreach ($xml->document as $doc) {
  echo '<p class="alert alert-error">'.$doc->title.'</p>';
}

The first echo $xmlraw is working - it outputs the XML string all on one line - the post is sending the data through properly.

The second echo $xml doesn't output anything and the foreach doesn't output anything either - something is not working in the PHP

I've also tried loading $xmlraw directly into simplexml_load_string($xmlraw) but it doesn't work - I'm assuming because it's not well formed?

The XML I'm using to test is:

<?xml version="1.0"?>
<document>
  <title>
    Foobar
  </title>
</document>

You can paste the XML into the textarea on this ( http://tsdexter.com/webservice-testing/programs-list.php ) page and then if you inspect element underneath xmlraw: you can see that the raw xml string was echoed - however, underneath xml: there is nothing and also the foreach doesn't output anything either.

Here's a jsFiddle so you can see the HTML/JS - it doesn't actually do anything though because it can't ajax to the PHP page - so I included the PHP version above to test. http://jsfiddle.net/tsdexter/EDqQB/

Any ideas?

like image 460
tsdexter Avatar asked May 01 '26 19:05

tsdexter


1 Answers

A few questions, which may or may not constitute an answer, but needed more space than a comment.

  1. Why are you running urldecode on the $_POST variable? PHP should be doing that for you, unless you are double-escaping it somewhere.
  2. Why are you loading into DOM and then SimpleXML? I know you said you tried without, but they use the same XML parser underneath, so there is no way this will help you rather than introducing more confusion.
  3. What does var_dump(simplexml_load_string($rawxml)) give you? My guess would be FALSE, meaning an error. In which case, check you have warnings turned on (error_reporting(E_ALL); ini_set('display_errors', '1');) and detailed XML error handling turned off (libxml_use_internal_errors(false);).
  4. In case it does give you a SimpleXMLElement object, don't spend too long looking at the var_dump output. Try one of these debugging functions instead.
like image 130
IMSoP Avatar answered May 04 '26 08:05

IMSoP