PHP Classes

Parsing XML

Recommend this page to a friend!

      PHP OAuth Library  >  All threads  >  Parsing XML  >  (Un) Subscribe thread alerts  
Subject:Parsing XML
Summary:Quick code to parse XML
Messages:2
Author:Jason Maggard
Date:2014-01-29 20:11:31
Update:2014-01-29 21:22:19
 

  1. Parsing XML   Reply   Report abuse  
Picture of Jason Maggard Jason Maggard - 2014-01-29 20:11:31
Function: SendAPIRequest

Search for "switch($content_type)"

case 'text/xml':
// Remove all newlines, returns and tabs.
$data = str_replace(array("\n", "\t", "\r"), '', $data);
// Trim leading/trailing spaces, convert dub-quotes to single
$data = trim(str_replace('"', "'", $data));
// Load the string with SimpleXML
$sXml = simplexml_load_string($data);
// Convert to JSON
$response = json_encode($sXml);
break;

It suited my needs. You could just as easily decode the JSON.

  2. Re: Parsing XML   Reply   Report abuse  
Picture of Manuel Lemos Manuel Lemos - 2014-01-29 21:22:19 - In reply to message 1 from Jason Maggard
Thanks. The class does not include decoding of XML documents because sites that return XML encode values in many different ways. Some include values in nested tags, others in tag parameters, etc..

So I thought it would be better if the developer decides how to decode the XML results accordingly.

Anyway, for simplified XML parser, you may want to use this class:

phpclasses.org/blog/package/4/post/ ...