-1

I'm using this XML to Json Zend module.

The problem is I can receive a bad formated XML string from the webservice that I'm connected. When this XML is processed at the fromXml Zend method it throws a warning.

So I would like to validate the XML before call the fromXml function. How can I know if a XML is valid? Prefereable using Zend?

I tried to use the code as following but I hadn't sucess yet: (this XML string works when using the fromXml)

<?php
$xml1 = 'asdf';
$xml2 = '<?xml version="1.0" encoding="iso-8859-1" ?>
<sroxml>
   <version>1.0</version>
   <quantity>1</quantity>
   <SearchType>Objetcs List</SearchType>
   <ResultType>All events</ResultType>
     <object>
       <number>AA299100299BB</number>
       <event>
          <type>BDI</type>
          <status>01</status>
          <date>07/01/2013</date>
          <hour>12:12</hour>
          <description>Received</description>
          <receiver>MARY WILLIAN                                      </receiver>
          <document>AA. 111639676            </document>
          <comment>                         </comment>
          <local>Some local</local>
          <code>11151970</code>
          <city>Some City</city>
          <uf>AB</uf>
          <sto>11102272</sto>
      </event>
     </object>
</sroxml>';

$validator = new DOMDocument();
$validator->loadXML($xml2, LIBXML_DTDLOAD|LIBXML_DTDVALID|LIBXML_ERR_WARNING |LIBXML_DTDATTR);
echo "<br>testing<pre>"; var_dump($validator->validate()); echo "</pre><br>";
Andrew Barber
  • 39,603
  • 20
  • 94
  • 123
GarouDan
  • 3,743
  • 9
  • 49
  • 75
  • By validation do you mean that tags match and there is one root element, or xsd schema validation? – Mirko Adari Feb 27 '13 at 19:41
  • The only thing that I would like is when I try to convert the XML string to a Json, if the XML isnt valid I got a empty json. – GarouDan Feb 27 '13 at 19:44
  • @GarouDan: So all you want to do is to capture the error so if there's an error you know the XML was invalid?! – hakre Feb 28 '13 at 13:31
  • I really think that knows if a XML is parsable is a constructive thing. But if the community no I can't do nothing. Otherwise thank you @mkaatman to helping me. – GarouDan Feb 28 '13 at 13:37
  • @GarouDan: I try to find out what your question is about so we can file it properly here on site. Did the `simplexml_load_string` in *mkaatman*'s answer made it for you? It sounds to me that you're looking for *well-formedness* here, not *validity* specifically. – hakre Feb 28 '13 at 13:41
  • Possible duplicate of: [Check if remote file is well-formed XML with PHP](http://stackoverflow.com/questions/441044/check-if-remote-file-is-well-formed-xml-with-php) – hakre Feb 28 '13 at 13:49

1 Answers1

5
$dom = new DOMDocument();
$dom->loadXML($xml_string);
if ($dom->validate()) {
    echo "This document is valid!\n";
}

To test just if a string is parsable as XML, try:

simplexml_load_string(($string)

simplexml_load_string returns false if not and a object if its all ok.

GarouDan
  • 3,743
  • 9
  • 49
  • 75
Matt
  • 5,315
  • 1
  • 30
  • 57
  • 2
    These flags on the loadXML method will enable DTD validation and reporting as well: LIBXML_DTDLOAD|LIBXML_DTDVALID|LIBXML_ERR_WARNING |LIBXML_DTDATTR); – Don Day Feb 27 '13 at 19:50
  • This is not working for a valid XML of mine. (A XML that I can convert to Json using the method above). I tried to use print_r($dom->loadXML($xml_string, LIBXML_DTDLOAD|LIBXML_DTDVALID|LIBXML_ERR_WARNING |LIBXML_DTDATTR)); to see if something is wrong but nothing appeared. What can I do? – GarouDan Feb 27 '13 at 20:06
  • tried print_r($dom->validate()); after $dom->loadXML($xml_string, LIBXML_DTDLOAD|LIBXML_DTDVALID|LIBXML_ERR_WARNING |LIBXML_DTDATTR); but nothing happens. – GarouDan Feb 27 '13 at 20:08
  • Do you have a DTD for the document? If you're just wanting to know if it's well formed XML try: `simplexml_load_string($xml2);` returns false on failure to parse. – Matt Feb 27 '13 at 21:26
  • Thx a lot. This worked. I need to change the $xml2 string above because has a break line and this mess the things. After this and using simplexml_load_string worked fine. I will edit your answer and embrace it. Thx again. – GarouDan Feb 28 '13 at 11:24