akniazi
akniazi

Reputation: 732

xml string validation in php

I am coding a PHP rest Service. I am sending xml string from client and service parse the xml with SimpleXmlElement. If I send "hello world" instead of xml string, the below piece of code produce warnings in the browser. how can I check the input to be valid xml string not some sentence.

 try
 {
   xmlobj = new SimpleXMLElement($xml_post);
 }
 catch(Exception $e)
 {
   $dat = $e->getMessage();
   return $dat;
 }

The warning is something like this

SimpleXMLElement::__construct() [simplexmlelement.--construct]: Entity: line 1: parser error : Start tag expected, '<' not found

I want to validate the input before calling SimpleXMLElement class construtor.

Upvotes: 1

Views: 635

Answers (1)

ingvar harjaks
ingvar harjaks

Reputation: 106

php.net has a gret doc about handling libxml errors.

start by adding the following before try-catch block:

libxml_use_internal_errors(true);

Upvotes: 1

Related Questions