Jul 10, 2012

XML interview questions and answers

Q. What is XML?
A. XML stands for eXtensible Markup Language. XML is a grammatical system for constructing custom markup languages for describing business data, mathematical data, chemical data etc. XML loosely couples disparate applications or systems utilizing JMS, Web services etc. XML uses the same building blocks that HTML does: elements, attributes and values.

Q. Why is XML important?
  • Scalable: Since XML is not in a binary format you can create and edit files with anything and it’s also easy to debug. XML can be used to efficiently store small amounts of data like configuration files (web.xml, application.xml, struts-config.xml, etc) to large company wide data with the help of XML stored in the database as a CLOB.
  • Fast Access: XML documents benefit from their hierarchical structure. Hierarchical structures are generally faster to access because you can drill down to the section you are interested in.
  • Easy to identify and use: XML not only displays the data but also tells you what kind of data you have. The mark up tags identifies and groups the information so that different information can be identified by different application.
  • Stylability: XML is style-free and whenever different styles of output are required the same XML can be used with different style-sheets (XSL) to produce output in XHTML, PDF, TEXT, another XML format, etc.
  • Linkability, in-line usability, universally accepted standard with free/inexpensive tools.

Q. When would you not use an XML?
A. XML is verbose and it can be 4-6 times larger in size compared to a csv or a tab delimited file. If your network lacked bandwidth and/or your content is too large and network throughput is vital to the application then you may consider using a csv or tab delimited format instead of an XML. JSON might be another alternative.

Q. Which is better to store data as elements or as attributes?
A. A question arising in the mind of XML/DTD designers is whether to model and encode certain information using an element, or alternatively, using an attribute. The answer to the above question is not clear-cut. But the general guideline is:
  • Using an element: <book><title>Lord of the Rings</title>...</book>: If you consider the information in question to be part of the essential material that is being expressed or communicated in the XML, put it in an element
  • Using an attribute: <book title=" Lord of the Rings "/>: If you consider the information to be peripheral or incidental to the main communication, or purely intended to help applications process the main communication, use attributes.
The principle is data goes in elements and metadata goes in attributes.  Elements are also useful when they contain special characters like “<”, “>”, etc which are harder to use in attributes. The most important reason to use element is its extensibility. It is far easier to create child elements to reflect complex content than to break an attribute into pieces. You can use attributes along with elements to refine your understanding of that element with extra information. Attributes are less verbose but using attributes instead of child elements with the view of optimizing document size is a short term strategy, which can have long term consequences. If verbosity and performance are of utmost importance, then XML might be a wrong choice. There are alternatives like JSON, CSV, Tab delimited data, FIX (Financial Information eXchange), etc.

Q. What is a well-formed XML document?
A. A well formed document adheres to the following rules for writing an XML.
  • A root element is required. A root element is an element, which completely contains all the other elements.
  • Closing tags are required. <cust>abc</cust> or <cust/>
  • Elements must be properly nested.
  • XML is case sensitive. <CUSTOMER> and <Customer> elements are considered completely separate.
  • An attribute’s value must always be enclosed in either single or double quotes.
  • Entity references must be declared in a DTD before being used except for the 5 built-in (<, > etc) discussed in the previous question.
Q. What is a valid XML document?
A. For an XML document to be valid, it must conform to the rules of the corresponding DTD (Document Type Definition – internal or external) or XSD (XML Schema Definition).

Q. Why use an XML document as opposed to other types of documents like a text file etc?
  • It is a universally accepted standard.
  • Free and easy to use tools are available. Also can be stored in a database.
  • Fast access due to its hierarchical structure.
  • Easy to identify and use due to its markup tags.

Q. What is your favorite XML framework or a tool?
A.  My favorite XML framework is JiBX, which unmarshals an XML document to graph of Java objects and marshals a graph of Java objects back to  an XML document. It is simple to use, very flexible and fast. It can be used with existing Java classes.

Q. Explain where your project needed XML documents?
A. It is hard to find a project, which does not use XML documents.

  • XML is used to communicate with disparate systems via messaging or Web Services.
  • XML based protocols and standards like SOAP, ebXML, WSDL etc are used in Web Services.
  • XML based deployment descriptors like web.xml, ejb-jar.xml, etc are used to configure the JEE containers. Having said this, with the advent of annotatiions, use of XML for wiring up artefacts has reduced.
  • XML based configuration files are used by open-source frameworks like Hibernate, Spring, Struts, and Tapestry etc.

Q. WHat is JSON (JavaScript Object Notation) and why do you need JSON when there is XML?
A. JSON is a new human readable data format that has become very popular in the last few years, especially in web development.

JSON is very similar to XML. They both try to solve the same problem by creating a simple, human readable format for storing data. JSON was designed with web in mind, so it works really well with JavaScript. The two key benefits of JSON over XML are:
  • Faster to parse
  • Takes up less space
Learn more about JSON with code.

XML format:


JSON format:

JSON is ideal for mobile and web applications.



Post a Comment

Subscribe to Post Comments [Atom]

Links to this post:

Create a Link

<< Home