Java XML Interview Questions & Answers

5 avg. rating (100% score) - 1 votes

Java XML Interview Questions & Answers

Are you prepared in attending an interview? Then do not worry. If you are preparing for Java XML job interview and don’t know how to crack interview and what level or difficulty of questions to be asked in job interviews then go through Wisdomjobs Java XML interview questions and answers page to crack your job interview. In computing world, Extensible Markup Language (XML) is a markup language that defines a set of rules for encoding documents in a format that is both human-readable and machine-readable. XML has come into common use for the interchange of data over the Internet. Below is the list of frequently asked Java XML interview questions and answers which gets you ready to face the interviews:

Java XML Interview Questions

Java XML Interview Questions
    1. Question 1. What Xml Stands For?

      Answer :

      XML stands for Extensible Markup Language.

    2. Question 2. What Are The Advantages Of Using Xml?

      Answer :

      Following are the advantages that XML provides:

      • Technology agnostic - Being plain text, XML is technology independent. It can be used by any technology for data storage and transmission purpose.
      • Human readable- XML uses simple text format. It is human readable and understandable.
      • Extensible - in XML, custom tags can be created and used very easily.
      • Allow Validation - Using XSD, DTD and XML structure can be validated easily.

    3. Question 3. What Are The Disadvantages Of Using Xml?

      Answer :

      Following are the disadvantages of XML usage:

      • Redundant Syntax - Normally XML file contains lot of repeatitive terms.
      • Verbose-Being a verbose language, XML file size increases the transmission and storage costs.

    4. Question 4. What Is Xml Parsing?

      Answer :

      Parsing XML refers to going through XML document to access data or to modify data in one or other way.

    5. Question 5. What Is Xml Parser?

      Answer :

      XML Parser provides way how to access or modify data present in an XML document. Java provides multiple options to parse XML document.

    6. Question 6. Name Some Of The Parsers Which Are Commonly Used To Parse Xml Documents?

      Answer :

      Following are various types of parsers which are commonly used to parse XML documents:

      • Dom Parser - Parses the document by loading the complete contents of the document and creating its complete hiearchical tree in memory.
      • SAX Parser - Parses the document on event based triggers. Does not load the complete document into the memory.
      • JDOM Parser - Parses the document in similar fashion to DOM parser but in more easier way.
      • StAX Parser - Parses the document in similar fashion to SAX parser but in more efficient way.
      • XPath Parser - Parses the XML based on expression and is used extensively in conjuction with XSLT.
      • DOM4J Parser - A java library to parse XML, XPath and XSLT using Java Collections Framework , provides support for DOM, SAX and JAXP.

    7. Question 7. What Dom Stands For?

      Answer :

      DOM stands for Document Object Model.

    8. Question 8. What Is Dom?

      Answer :

      DOM stands for Document Object Model and it is an official recommendation of the World Wide Web Consortium (W3C). It defines an interface that enables programs to access and update the style, structure,and contents of XML documents. XML parsers that support the DOM implement that interface.

    9. Question 9. When To Use A Dom Parser?

      Answer :

      You should use a DOM parser when:

      • You need to know a lot about the structure of a document
      • You need to move parts of the document around (you might want to sort certain elements, for example)
      • You need to use the information in the document more than once

    10. Question 10. What Dom Parser Returns?

      Answer :

      When you parse an XML document with a DOM parser, you get back a tree structure that contains all of the elements of your document. The DOM provides a variety of functions you can use to examine the contents and structure of the document.

    11. Question 11. What Are The Advantages Of Dom Parsing?

      Answer :

      The DOM is a common interface for manipulating document structures. One of its design goals is that Java code written for one DOM-compliant parser should run on any other DOM-compliant parser without changes.

    12. Question 12. What Are The Key Components/interfaces Of Dom Parsing?name Some Of The Important Dom Parsing Methods.?

      Answer :

      When you are working with the DOM, there are several methods you'll use often:

      • Document.getDocumentElement() - Returns the root element of the document.
      • Node.getFirstChild() - Returns the first child of a given Node.
      • Node.getLastChild() - Returns the last child of a given Node.
      • Node.getNextSibling() - These methods return the next sibling of a given Node.
      • Node.getPreviousSibling() - These methods return the previous sibling of a given Node.
      • Node.getAttribute(attrName) - For a given Node, returns the attribute with the requested name.

    13. Question 13. Can We Create An Xml Document Using Dom Parser?

      Answer :

      Yes! Using DOM parser, we can parse, modify or create a XML document.

    14. Question 14. What Sax Stands For?

      Answer :

      SAX stands for Simple API for XML.

    15. Question 15. What Is A Sax Parser?

      Answer :

      SAX Parser is an event-based parser for xml documents.

    16. Question 16. How A Sax Parser Works?

      Answer :

      SAX (the Simple API for XML) is an event-based parser for xml documents.Unlike a DOM parser, a SAX parser creates no parse tree. SAX is a streaming interface for XML, which means that applications using SAX receive event notifications about the XML document being processed an element, and attribute, at a time in sequential order starting at the top of the document, and ending with the closing of the ROOT element.

    17. Question 17. When To Use A Sax Parser?

      Answer :

      You should use a SAX parser when:

      • You can process the XML document in a linear fashion from the top down
      • The document is not deeply nested
      • You are processing a very large XML document whose DOM tree would consume too much memory.Typical DOM implementations use ten bytes of memory to represent one byte of XML
      • The problem to be solved involves only part of the XML document
      • Data is available as soon as it is seen by the parser, so SAX works well for an XML document that arrives over a stream

    18. Question 18. What Are The Disadvantages Of Sax Parsing?

      Answer :

      • We have no random access to an XML document since it is processed in a forward-only manner
      • If you need to keep track of data the parser has seen or change the order of items, you must write the code and store the data on your own

    19. Question 19. Name Some Of The Important Sax Parsing Methods?

      Answer :

      ContentHandler Interface specifies the callback methods that the SAX parser uses to notify an application program of the components of the XML document that it has seen.

      • void startDocument() - Called at the beginning of a document.
      • void endDocument() - Called at the end of a document.
      • void startElement(String uri, String localName, String qName, Attributes atts) - Called at the beginning of an element.
      • void endElement(String uri, String localName,String qName) - Called at the end of an element.
      • void characters(char[] ch, int start, int length) - Called when character data is encountered.
      • void ignorableWhitespace( char[] ch, int start, int length) - Called when a DTD is present and ignorable whitespace is encountered.
      • void processingInstruction(String target, String data) - Called when a processing instruction is recognized.
      • void setDocumentLocator(Locator locator)) - Provides a Locator that can be used to identify positions in the document.
      • void skippedEntity(String name) - Called when an unresolved entity is encountered.
      • void startPrefixMapping(String prefix, String uri) - Called when a new namespace mapping is defined.
      • void endPrefixMapping(String prefix) - Called when a namespace definition ends its scope.

    20. Question 20. Name Some Methods For Processing The Attributes Connected To An Element In Sax Parsing?

      Answer :

      Attributes Interface specifies methods for processing the attributes connected to an element.

      • int getLength() - Returns number of attributes.
      • String getQName(int index)
      • String getValue(int index)
      • String getValue(String qname)

       

    21. Question 21. Can We Create An Xml Document Using Sax Parser?

      Answer :

      No! Using SAX parser, we can only parse or modify a XML document.

    22. Question 22. What Is Jdom Parser?

      Answer :

      JDOM is an open source, java based library to parse XML document and it is typically java developer friendly API.

    23. Question 23. What Are The Benefits Of Jdom Parser?

      Answer :

      It is java optimized, it uses java collection like List and Arrays. It works with DOM and SAX APIs and combines the best of the two. It is of low memory footprint and is nearly as fast as SAX.

    24. Question 24. When To Use A Jdom Parser?

      Answer :

      You should use a JDOM parser when:

      • You need to know a lot about the structure of a document.
      • You need to move parts of the document around (you might want to sort certain elements, for example).
      • You need to use the information in the document more than once.
      • You are a java developer and want to leverage java optimized parsing of XML.

    25. Question 25. What Are The Advantages Of Jdom Parser?

      Answer :

      When you parse an XML document with a JDOM parser, you get the flexibility to get back a tree structure that contains all of the elements of your document without impacting the memory footprint of the application. The JDOM provides a variety of utility functions you can use to examine the contents and structure of the document in case document is well structured and its structure is known.

      JDOM gives java developers flexibility and easy maintainablity of xml parsing code. It is light weight and quick API.

    26. Question 26. Name Some Of The Important Jdom Classes?

      Answer :

      The JDOM defines several Java classes. Here are the most common classes :

      • Document - Represents the entire XML document. A Document object is often referred to as a DOM tree.
      • Element - Represents an XML element. Element object has methods to manipulate its child elements,its text, attributes and namespaces.
      • Attribute Represents an attribute of an element. Attribute has method to get and set the value of attribute. It has parent and attribute type.
      • Text Represents the text of XML tag.
      • Comment Represents the comments in a XML document.

    27. Question 27. Name Some Of The Important Jdom Methods?

      Answer :

      When you are working with the JDOM, there are several methods you'll use often:

      • SAXBuilder.build(xmlSource) - Build the JDOM document from the xml source.
      • Document.getRootElement() - Get the root element of the XML.
      • Element.getName() - Get the name of the XML node.
      • Element.getChildren() - Get all the direct child nodes of an element.
      • Node.getChildren(Name) - Get all the direct child nodes with a given name.
      • Node.getChild(Name) - Get first child node with given name.

    28. Question 28. Can We Create An Xml Document Using Jdom Parser?

      Answer :

      Yes! Using JDOM parser, we can parse, modify and create a XML document.

    29. Question 29. What Is A Stax Parser?

      Answer :

      StAX is a JAVA based API to parse XML document in a similar way as SAX parser does but StAX is a PULL API where as SAX is a PUSH API. It means in case of StAX parser, client application need to ask StAX parser to get information from XML whenever it needs but in case of SAX parser, client application is required to get information when SAX parser notifies the client application that information is available.

    30. Question 30. Can We Create An Xml Document Using Stax Parser?

      Answer :

      Yes! Using StAX parser, we can parse, modify and create a XML document.

    31. Question 31. Is Stax Parser A Pull Api?

      Answer :

      Yes! StAX is a PULL API.

    32. Question 32. When You Should Use A Stax Parser?

      Answer :

      You should use a StAX parser when:

      • You can process the XML document in a linear fashion from the top down.
      • The document is not deeply nested.
      • You are processing a very large XML document whose DOM tree would consume too much memory. Typical DOM implementations use ten bytes of memory to represent one byte of XML.
      • The problem to be solved involves only part of the XML document.
      • Data is available as soon as it is seen by the parser, so StAX works well for an XML document that arrives over a stream.

    33. Question 33. What Are The Disadvantages Of Stax Parser?

      Answer :

      • We have no random access to an XML document since it is processed in a forward-only manner
      • If you need to keep track of data the parser has seen or change the order of items, you must write the code and store the data on your own.

    34. Question 34. Explain Xmleventreader Class Of Stax Parser?

      Answer :

      This class provide iterator of events which can be used to iterate over events as they occur while parsing the XML document.

      • StartElement asStartElement() - used to retrieve value and attributes of element.
      • EndElement asEndElement() - called at the end of a element.
      • Characters asCharacters() - can be used to obtain characters such a CDATA, whitespace etc.

    35. Question 35. Explain Xmleventwriter Class Of Stax Parser?

      Answer :

      This interface specifies methods for creating an event.

      • add(Event event) - Add event containing elements to XML.

    36. Question 36. Explain Xmlstreamreader Class Of Stax Parser?

      Answer :

      This class provide iterator of events which can be used to iterate over events as they occur while parsing the XML document

      • int next() - used to retrieve next event.
      • boolean hasNext() - used to check further events exists or not
      • String getText() - used to get text of an element
      • String getLocalName() - used to get name of an element

    37. Question 37. Explain Xmlstreamwriter Class Of Stax Parser?

      Answer :

      This interface specifies methods for creating an event:

      • writeStartElement(String localName) - Add start element of given name.
      • writeEndElement(String localName) - Add end element of given name.
      • writeAttribute(String localName, String value) - Write attribute to an element.

    38. Question 38. What Is Xpath?

      Answer :

      The XPath is an official recommendation of the World Wide Web Consortium (W3C). It defines a language to find information in an XML file. It is used to traverse elements and attributes of an XML document. XPath provides various type of expressions which can be used to enquire relevant information from the XML document.

    39. Question 39. What Are The Key Components Of Xpath?

      Answer :

      Following are the key components of XPath:

      • Structure Definitions - XPath defines the parts of an XML document like element, attribute, text, namespace, processing-instruction, comment, and document nodes.
      • Path Expressions XPath provides powerful path expressions select nodes or list of nodes in XML documents.
      • Standard FunctionsXPath provides a rich library of standard functions for manipulation of string values, numeric values, date and time comparison, node and QName manipulation, sequence manipulation, Boolean values etc.
      • Major part of XSLTXPath is one of the major element in XSLT standard and is must have knowledge in order to work with XSLT documents.
      • W3C recommendationXPath is official recommendation of World Wide Web Consortium (W3C).

    40. Question 40. Can We Create An Xml Document Using Dom4j Parser?

      Answer :

      Yes! Using DOM4J parser, we can parse, modify and create a XML document.

    41. Question 41. Name Some Of The Important Dom4j Methods?

      Answer :

      When you are working with the DOM4J, there are several methods you'll use often:

      • SAXReader.read(xmlSource)() - Build the DOM4J document from the xml source.
      • Document.getRootElement() - Get the root element of the XML.
      • Element.node(index) - Get the XML node at particular index in the element.
      • Element.attributes() - Get all the attributes of an element.
      • Node.valueOf(@Name) - Get the value of an attribute with given name of the element.

    42. Question 42. When Are The Advantages Of Using A Dom4j Parser?

      Answer :

      DOM4J gives java developers flexibility and easy maintainablity of xml parsing code. It is light weight and quick API.

    43. Question 43. Name Some Of The Important Dom4j Classes?

      Answer :

      The DOM4J defines several Java classes.

      Here are the most common classes:

      • Document - Represents the entire XML document. A Document object is often referred to as a DOM tree.
      • Element - Represents an XML element. Element object has methods to manipulate its child elements,its text, attributes and namespaces.
      • Attribute Represents an attribute of an element. Attribute has method to get and set the value of attribute. It has parent and attribute type.
      • Node Represents Element, Attribute or ProcessingInstruction

    44. Question 44. When Are The Benefits Of Using A Dom4j Parser?

      Answer :

      When you parse an XML document with a DOM4J parser, you get the flexibility to get back a tree structure that contains all of the elements of your document without impacting the memory footprint of the application. The DOM4J provides a variety of utility functions you can use to examine the contents and structure of the document in case document is well structured and its structure is known. DOM4J uses XPath expression to navigate through the XML document.

    45. Question 45. When To Use A Dom4j Parser?

      Answer :

      You should use a DOM4J parser when:

      • You need to know a lot about the structure of a document
      • You need to move parts of the document around (you might want to sort certain elements, for example)
      • You need to use the information in the document more than once
      • You are a java developer and want to leverage java optimized parsing of XML.

    46. Question 46. What Is Dom4j Parser?

      Answer :

      DOM4J is an open source, java based library to parse XML document and it is highly flexible, high-performance, and memory-efficient API. It is java optimized, it uses java collection like List and Arrays. It works with DOM, SAX, XPath and XSLT. It can parse large XML document with very low memory footprint.

    47. Question 47. Can We Create An Xml Document Using Xpath Parser?

      Answer :

      No! XPath parser is used to to navigate XML Document only. It is better to use DOM parser for creating XML.

    48. Question 48. What Is Path Expression In Xpath?

      Answer :

      XPath uses a path expression to select node or list of nodes from an xml document. Following is the list of useful paths and expression to select any node/ list of nodes from an xml document.

      Expressions:

      • node-name: Select all nodes with the given name "nodename"
      • /: Selection starts from the root node
      • //: Selection starts from the current node that match the selection
      • .: Selects the current node
      • ..: Selects the parent of the current node
      • @: Selects attributes
      • student: Example − Selects all nodes with the name "student"
      • class/student:  Example: Selects all student elements that are children of class
      • //student: Selects all student elements no matter where they are in the document

Face Book Twitter Google Plus Instagram Youtube Linkedin Myspace Pinterest Soundcloud Wikipedia

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Java XML Tutorial