Suppose your xml url is https://technologyt20.tech.blog/yourxml.xml

Fetch xml file data by using wordpress function file_get_contents() function.

Example: $xml_data = file_get_contents( 'https://technologyt20.tech.blog/yourxml.xml');

Use this custom function to validate xml data.
it will return true if xml is valid.

function check_your_valid_xml( $xml_data ) {
    $xml_data = trim( $xml_data );
    if ( empty( $xml_data ) ) {
      return false;
    }

    libxml_use_internal_errors( true ); // Disables standard      libxml errors and enables user error handling.
    simplexml_load_string( $xml_data ); // Converts a well-  formed XML string into a SimpleXMLElement object.
    $errors = libxml_get_errors(); // Returns an array of error objects, and an empty array if there are no errors in the libxml error buffer.
    libxml_clear_errors(); // Clears the libxml error buffer.

    return empty( $errors );
}
$valid_xml = check_your_valid_xml( $xml_data );

if ( true === $valid_xml ) {
     echo 'Your xml is valid.';
} else {
    echo 'Your xml not valid.';
}

Leave a comment