EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login
Home Software Development Software Development Tutorials XML Tutorial XML Error
Secondary Sidebar
XML Tutorial
  • Basic
    • What is XML?
    • XML Tags
    • XML URL
    • XPath Sibling
    • XML root element
    • XML Encryption
    • XML Parsing php
    • xml parsing with java
    • Dataset XML
    • XML Parser in C#
    • XML Tree
    • XML boolean
    • XML sitemap
    • XML Array
    • XML reserved characters
    • XML Viewer
    • XML Uses
    • XML Special Characters
    • XML generator
    • XML file format
    • XML DOM
    • XML ampersand
    • XML Mapping
    • XML File
    • XML Element
    • XML HttpRequest
    • XML XSD
    • XML Schema
    • XML Namespaces
    • XML Comments
    • XML Attributes
    • XML Encoding
    • XML Validation
    • XML CDATA
    • XML Database
    • XML Technologies
    • XML Error
    • XML DTD
    • XML Date
    • XML Parsers
    • String in XML
    • XML with CSS
    • XML Versions
    • XML Features
    • XML Commands
    • Servlet web.xml
    • XPath Injection
    • XPath Functions
    • XPath starts-with
    • XPath Selector
    • XPath Count
    • XPath XML
    • XML Parsing in Oracle
    • XML parsing in python
  • Xpath
    • What is XPath?
    • XPath namespace
    • XPath for JSON
    • XPath Last Element
    • Xpath Substring
    • XPath First Element
    • XPath local-name
    • XPath Multiple Attributes
    • XPath Matches
    • XPath Position
    • XPath Index
    • XPath regex
    • XPath id contains
    • XPath innertext
    • XPath Multiple Conditions
    • XPath Helper
    • XPath Generator
    • XPath ID
    • XPath Locator

XML Error

By Priya PedamkarPriya Pedamkar

XML Error

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:

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

All in One Software Development Bundle(600+ Courses, 50+ projects)
Python TutorialC SharpJavaJavaScript
C Plus PlusSoftware TestingSQLKali Linux
Price
View Courses
600+ Online Courses | 50+ projects | 3000+ Hours | Verifiable Certificates | Lifetime Access
4.6 (86,883 ratings)

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:

XML Error-1.1

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-1.2

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-1.3

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:

Example-1.4

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 –

  1. XML with CSS
  2. XML Versions
  3. XML Schema
  4. XML Namespaces
Popular Course in this category
XML Training (5 Courses, 6+ Projects)
  5 Online Courses |  6 Hands-n Projects |  40+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

C# Programming, Conditional Constructs, Loops, Arrays, OOPS Concept

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more