Updated April 4, 2023
Definition of XML Error
XML defines errors and warnings in their specifications and XML handles them with XML error handler which in turn uses SAX protocols. Generally Speaking, XML documents are used widely in various applications to store structured data which may include Missing elements, misformatted and even be with mismatch values. Even a well-formatted XML document has errors which are syntactically correct but may be due to missing attributes etc. These handlers are well defined while opening and closing of XML data Sources as XML define error classes once the warning issued by the XMLParser.
Syntax and Parameters
Following are the different functions used with PHP to detect an error in their function:
web.xml file uses the following structure in their android application to specify error page for each of the element specified. Here <error-page> element tag is used.
<web-app>
<error-page>
<exception-type>What type of exceptions throwed(java.x)</exception-type>
<location>error web page- failed(error.filename)</location>
</error-page>
</web-app>
enum xmlErrorLevel {
XML_ERR_NONE = 0
XML_ERR_WARNING = 1
XML_ERR_ERROR = 2
XML_ERR_FATAL = 3
};
- This function has three parameters with corresponding values.
Functions: libxml error handler in PHP for XML.
libxml_get_errors (void): array
- This function returns an array of error in the buffer
xmlGeneric
void xmlGenericErrorFunc (void * ctx,
const char * msg,
... ...)
Here ctx denotes parsing context,
Msg is the error message.
xmlCopyError
int xmlCopyError (xmlErrorPtr from,
xmlErrorPtr to)
Here making copy of an error and this function returns ‘0’ is case of no error and ‘-1’ in case of error generated. here from is the source error and to is the destination error. To display validation error to resolve it we use XSR_GET_PARSING_DIAGNOSTIC to get detailed error information.
<?xml version="1.0" encoding="UTF-8"?>
<articles>
<article id="x34675">
<name>Apache Spark Architecture</name>
<month>december</month>
<author name="kay vennisla"/>
<reviews lang=""/>
<feedback > high rating</feedback>
<reviews lang="de">The best content with diagrams</reviews>
</article>
</articles>
The schema file XSD uses XSR_function as :
CALL XSR_GET_PARSING_DIAGNOSTICS(
blob('<?xml version="1.0"?>
<Articles xmlns="http://my.exho"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://my.exho http://my.exho/simple">
<article>
<Name>Thomas</Name>
<Month>Watson</Month>
</Name>
<Author>30x</Author>
<reviews />
</Articles>
'),'','','',1,?,?)@
How does Error Function Work in XML?
The syntax error is identified by the parser. When it is been identified it generates XML Exception with the certain information which is given below:
To handle the errors efficiently processing steps should be taken with the parser statement and special register as well.
- The register XML_EVENT has ‘Exceptions’
- XML_CODE has numeric exception codes. It is set to zero while handling the exceptions. If No CODE phase is entered then the control is back to the XML_Parser and now the XML_Code is set to Zero.
- XML_TEXT has the point where the error is detected in the text.
The error message that is thrown by the parser is
an error: (domain_definition):6: Start Tag: invalid element name
<vcpu>2</vcpu><
The above statement has three lines of Statements. The first statement specifies the cached error message and the next two lines has the description context of the XML Document holding the error and a specified pointer to point the exact error. The keywords present in this message is explained:
- (domain_definition): It provides the xml file name which contains the error. The filename specified inside the parenthesis is termed as a local file.
- number: Gives the line number where the error is.
- Start Tag: invalid element name in the DOC element of the XML file.
XML Specifications specifies three types of error, they are:
- Fatal Error: One simple way to introduce fatal errors in an XML document is to remove the closing tag to create a situation like no corresponding end tags. Exceptions with the range 1- 49 are considered to be a fatal error and the parser doesn’t do normal parsing even though the exception is handled.
- Error: This is a simple recoverable error.
- Warning Message: A simple general Warning.
The above types of errors are implemented in java to parse an error file in XML. The sample function part is illustrated below:
class Xmlerr implements ErrorHandler {
public void fun_warning (SAXParseException e1) throws SAXException {
show ("Warning message", e1);
throw (e1);
}
public void fun_error(SAXParseException e) throws SAXException {
show (" Prompt Error", e2);
throw (e2);
}
public void func_fatalError(SAXParseException e3) throws SAXException {
show (" prompt Fatal Error", e3);
throw (e3);
}
private void display(String t, SAXParseException e) {
System.out.println(t+ ": " + e.getMessage());
System.out.println("LNO of file " + e.getLineNumber() + " CNo"
+ e.getColumnNumber());
System.out.println(" ID: " + e.getSystemId());
}
}
xml_parse () function returns error during parsing an XML document and issues a message RNX0351. This provides a corresponding error code for the determined error also shows the offset where the error was found. Every XML request is validated for complete check for well-formed documents if it generates any syntax errors no response is returned back meanwhile this parser function is invoked.
Examples of XML Error
Using PHP-With simple String load of XML file lets see how the errors are parsed in PHP using XML function _get_errors().
Example #1 – Using_get_errors()
Code:
<?php
libxml_use_internal_errors(true);
$fd = simplexml_load_string("<?xml version='1.0><error><xml></error>");
if ($fd === false) {
echo "Loading error in xml";
foreach(libxml_get_errors() as $error) {
echo "x", $error->message;
}
}
?>
The above code displays the output like throwing the error in the line specifying end of data as I missed a quotes in xml declaration.
Output:
Example #2 – Using _internal_error
Code:
<?php
libxml_use_internal_errors(true);
$myXMLData =
"<?xml version='1.0' encoding='UTF-8'>
<article>
<topic>SAAS-Introduction</invalidtopic>
<author>Maclay John</author>
</article>";
$x = simplexml_load_string($myXMLData);
if ($x === false) {
echo "Invalid XML: ";
foreach(libxml_get_errors() as $error) {
echo "<br>", $error->message;
}
} else {
print_r($x);
}
?>
Output:
Example #3 – Handling Internal errors
Code:
<?php
// This statement implies error handling
var_dump(libxml_use_internal_errors(true));
// DOM Document of XML file is been loaded
$d = new DOMDocument;
if (!$d->load('ff.xml')) {
foreach (libxml_get_errors() as $error) {
// and here the function handle errors
}
libxml_clear_errors();
}
?>
Output:
Example #4 – Request and Response
Implementation of request and response message to the server application by the client.
Code:
<?xml version="1.0" encoding="UTF-8"?>
<request>
<control>
<senderid>EDUCBA</senderid>
<password>888888</password>
<controlid>7aece3-63d5-8844-ht07</controlid>
<control>
<errormessage>
<error>
<errorno>XL01000003</errorno>
<description>No XML File</description>
<correction> immediate call to admin to give services. </correction>
</error>
</errormessage>
</request>
Output:
Conclusion
Therefore, we have seen errors in Various Programming Language. The above-briefed function concludes at the maximum the error codes and line numbers at the place which will be reported by the message in the Result. To conclude with the above basic explanation we can successfully avoid the problems in the XML file.
Recommended Articles
This is a guide to XML Error. Here we also discuss the definition and how does error function work in xml? along with different examples and its code implementation. You may also have a look at the following articles to learn more –