Interview Questions

XML

JAXB - Avoid converting < into &lt; and > into &gt; during Marshalling

In this article, let us see how to avoid converting < to &lt ; and > to &gt ; and & to &amp ; during JAXB Marshalling operation.

1. CharacterEscapeHandler creation
Create a custom Escape Handler by implementing the CharacterEscapeHandler interface as given below.

import com.sun.xml.bind.marshaller.CharacterEscapeHandler;

public class JaxbCharacterEscapeHandler implements CharacterEscapeHandler {

        public void escape(char[] buf, int start, int len, boolean isAttValue,
                        Writer out) throws IOException {

JAXB - Generate Stubs from XSD

This article describes the method of generating the JAXB Stubs from the XML schema using ANT build.

Consider the following ANT build xml which reads the XML schema from "C:\Sample.xsd" and generates the JAXB stubs in the "C:\jaxbSrc" location.

<?xml version="1.0" encoding="UTF-8" ?>

<project basedir="." default="build" name="Jaxb-StubGenerationDemo">

        <taskdef name="xjc" classname="com.sun.tools.xjc.XJCTask">
          <classpath>
            <fileset dir="C:\lib\" includes="jaxb-xjc.jar" />
          </classpath>
        </taskdef>

    <target name="build">

XML Parsing using JDOM

JDOM is simply a Java representation of an XML document. It provides a way to represent that document for efficient parsing, manipulation, and writing. It has a straightforward API, which is optimized for the Java programmer. It's an alternative to DOM and SAX, although it integrates well with both DOM and SAX. It also integrates with other xml parsers like xerces.

The following class has methods for creating and parsing an xml document using JDom.

XMLJDomParser.java

package in.techdive.java;

import java.io.BufferedOutputStream;

Java Bean to XML conversion

In this article lets discuss about how to convert a Java bean to XML document.

Have look at the following Java bean.

Device.java

public class Device
{

        public Device(long deviceId, String deviceName, String productId, String serialNo)
        {
                super();
                this.deviceId = deviceId;
                this.deviceName = deviceName;
                this.productId = productId;
                this.serialNo = serialNo;
        }

        public Device()
        {

        }

        private long    deviceId;
        private String  deviceName;
        private String  productId;
        private String  serialNo;

        /**

Java - XML Validation with Schema

In this article, let us see how to validate XML file against the XML Schema.

Let us take a simple XML Schema file as follows,

XML Schema

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
        <xs:element name="Person">
                <xs:complexType>
                        <xs:attribute name="Name" type="xs:string"/>
                        <xs:attribute name="Country" type="xs:string"/>
                </xs:complexType>
        </xs:element>
</xs:schema>

Lets us construct the XML file which we will use to validate with the Schema.

Sample XML