Sjwdavies
Sjwdavies

Reputation: 4173

PHP XML Object To HTML Friendly Text String?

I'm using PHP5 and it's inbuilt SOAP functionality. I'm catching the SOAP Fault errors, and email myself when one is triggered.

What I really need to do is include the __getLastRequest() and __getLastResponse(), however as these are XML objects, when I try to include them by echoing them into the body of my HTML email, for obvious reasons they don't appear in full.

Is there a function or class I can use to convert these objects into a HTML friendly string?

I've googled this but without any joy. If possible i'd like to avoid using a class external to PHPs own functionality but if needs must i'll have to.

EDITED:

How can I format the following XML so that it gives me an indented browser friendly version?

<?xml version="1.0" encoding="UTF-8"?><SOAP-ENV:Envelope xmlns:SOAP-ENV="schemas.xmlsoap.org/soap/envelope/"; xmlns:ns1="example.com/"><SOAP-ENV:Body><ns1:Ad… 0NE</ns1:Postcode><ns1:BuildingName></ns1:BuildingName><ns1:BuildingNo></ns1:BuildingNo><ns1:SubBuilding></ns1:SubBuilding><ns1:Organisation></ns1:Organisation><ns1:Street></ns1:Street><ns1:SubStreet></ns1:SubStreet><ns1:Town></ns1:Town><ns1:District></ns1:District></ns1:address><ns1:AccountName>[email protected]</ns1:AccountName><ns1:Password>password</ns1:Password></ns1:AddressLookupUK></SOAP-ENV:Body></SOAP-ENV:Envelope>

Upvotes: 1

Views: 1315

Answers (2)

TimoSolo
TimoSolo

Reputation: 7325

You could use http://www.php.net/manual/en/book.tidy.php eg:

<?php
$xml = '<?xml version="1.0" encoding="UTF-8"?>...</SOAP-ENV:Envelope>';
$config = array(
            'indent'         => true,
            'output-xml'     => true,
            'input-xml'     => true,
            'wrap'         => '1000');

// Tidy
$tidy = new tidy();
$tidy->parseString($xml, $config, 'utf8');
$tidy->cleanRepair();
echo tidy_get_output($tidy);
?>

but you have to install the php extension first.

Upvotes: 2

prodigitalson
prodigitalson

Reputation: 60413

EDIT:

Basic print out of the xml. You may need to adjust to fit the actual xml it was hard to read since it was posted unformatted. Youll also protentialy need to handle nested elements i suppose which would require you to loop through the children. and i suppose attributes as well if you have any... You can refer to the SimpleXML docs for more detail on this.

foreach($xml->Body as $element => $value)
{
  echo "$element: $children";
}

Umm use htmlspecialchars or htmlentities to encode the string...? that way it will display as "code" in ana html email.

Upvotes: 0

Related Questions