JAVA and XML Technical
Interview Questions Answers
What does XML stand for?
XML stands for Extensible Markup Language.What are the advantages of using XML?
Following are the advantages that XML provides −What are the disadvantages of using XML?
Following are the disadvantages of XML usage −What is XML Parsing?
Parsing XML refers to going through XML documents to access data or to modify data in one or another way.What is XML Parser?
XML Parser provides a way how to access or modify data present in an XML document. Java provides multiple options to parse XML documents.Name some of the parsers which are commonly used to parse XML documents.
Following are various types of parsers that are commonly used to parse XML documents −What does DOM stand for?
DOM stands for Document Object Model.What is DOM?
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.When to use a DOM Parser?
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 onceWhat DOM Parser returns?
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.What are the advantages of DOM Parsing?
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.What are the key components/interfaces of DOM Parsing?
The DOM defines several Java interfaces. Here are the most common interfaces-Name some of the important DOM parsing methods.
When you are working with the DOM, there are several methods you'll use often −Can we create an XML document using DOM parser?
Yes! Using DOM parser, we can parse, modify or create an XML document.What does SAX stand for?
SAX stands for Simple API for XML.What is a SAX Parser?
SAX Parser is an event-based parser for xml documents.How a SAX Parser works?
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.When to use a SAX Parser?
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.What are the disadvantages of SAX Parsing?
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.Name some of the important SAX parsing methods.
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.Name some methods for processing the attributes connected to an element in SAX parsing.
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)Can we create an XML document using the SAX parser?
No! Using the SAX parser, we can only parse or modify an XML document.What is JDOM Parser?
JDOM is an open source, java based library to parse XML documents and it is typically a java developer-friendly API.What are the benefits of the JDOM parser?
It is java optimized, it uses java collections 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.When to use a JDOM Parser?
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.What are the advantages of the JDOM parser?
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 the document is well structured and its structure is known. JDOM gives java developers flexibility and easy maintainability of XML parsing code. It is a lightweight and quick API.Name some of the important JDOM classes.
The JDOM defines several Java classes. Here are the most common classes −Name some of the important JDOM methods.
When you are working with the JDOM, there are several methods you'll use often −Can we create an XML document using JDOM parser?
Yes! Using the JDOM parser, we can parse, modify and create an XML document.What is a StAX Parser?
StAX is a JAVA-based API to parse XML documents in a similar way as the SAX parser does but StAX is a PULL API whereas SAX is a PUSH API. It means in the case of the StAX parser, the client application needs to ask the StAX parser to get information from XML whenever it needs but in the case of the SAX parser, the client application is required to get information when the SAX parser notifies the client application that information is available.Can we create an XML document using StAX parser?
Yes! Using the StAX parser, we can parse, modify and create an XML document.Is StAX parser a PULL API?
Yes! StAX is a PULL API.When you should use a StAX parser?
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.What are the disadvantages of the StAX parser?
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.Explain XMLEventReader Class of StAX parser.
This class provides an iterator of events which can be used to iterate over events as they occur while parsing the XML document.Explain XMLEventWriter Class of StAX parser.
This interface specifies methods for creating an event. add(Event event) − Add event containing elements to XML.Explain XMLStreamReader Class of StAX parser.
This class provides an iterator of events which can be used to iterate over events as they occur while parsing the XML documentExplain XMLStreamWriter Class of StAX parser.
This interface specifies methods for creating an event. −What is XPath?
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 types of expressions that can be used to enquire relevant information from the XML document.What are the key components of XPath?
Following are the key components of XPath −What is the predicate in XPath?
Predicates are used to find a specific node or a node containing a specific value and are defined using.Sr.No. | Expression & Result |
---|---|
1 | /class/student[1] Selects the first student element that is the child of the class element. |
2 | /class/student[last()] Selects the last student element that is the child of the class element. |
3 | /class/student[last()−1] Selects the last but one student element that is the child of the class element. |
4 | //student[@rollno='493'] Selects all the student elements that have an attribute named rollno with a value of '493' |
What is path expression in XPath?
XPath uses a path expression to select nodes or list of nodes from an XML document. Following is the list of useful paths and expressions to select any node/ list of nodes from an XML document.Sr.No. | Expression & Description |
---|---|
1 | node-name Select all nodes with the given name "node name" |
2 | / Selection starts from the root node |
3 | // Selection starts from the current node that match the selection |
4 | . Selects the current node |
5 | .. Selects the parent of the current node |
6 | @ Selects attributes |
7 | student Example - Select all nodes with the name "student" |
8 | class/student Example - Selects all student elements that are children of class |
9 | //student Selects all student elements no matter where they are in the document |
Can we create an XML document using XPath parser?
No! XPath parser is used to navigate XML Documents only. It is better to use a DOM parser for creating XML.What is DOM4J Parser?
DOM4J is an open source, java based library to parse XML documents and it is a highly flexible, high−performance, and memory−efficient API. It is java optimized, it uses java collections like List and Arrays. It works with DOM, SAX, XPath, and XSLT. It can parse large XML documents with very low memory footprint.When to use a DOM4J Parser?
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.What are the benefits of using a DOM4J Parser?
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 the document is well structured and its structure is known. DOM4J uses XPath expression to navigate through the XML document.What are the advantages of using a DOM4J Parser?
DOM4J gives java developers flexibility and easy maintainability of XML parsing code. It is a lightweight and quick API.Name some of the important DOM4J classes.
The DOM4J defines several Java classes. Here are the most common classes −Name some of the important DOM4J methods.
When you are working with the DOM4J, there are several methods you'll use often − SAXReader.read(XML source)() − 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 the given name of the element.Can we create an XML document using the DOM4J parser?
Yes! Using the DOM4J parser, we can parse, modify and create a XML document.