Thursday 29 August 2013

Validate a XML file using XSD schema file (JAVA)

How to validate a XML file using XSD schema file simple example (JAVA)

 
2
 
0
 
Was this helpful?


Here is a sample XML file;
<?xml version=”1.0″ encoding=”UTF-8″?>
<?xml-stylesheet type=”text/xsl” href=”bank.xsl”?>
<bnk:Bank
xmlns:bnk=”http://www.tunatore.wordpress.com/bank
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance
xsi:schemaLocation=”http://www.tunatore.wordpress.com/bank bank.xsd”>
    <bnk:BankName>IT Bank of Turkey</bnk:BankName>
    <bnk:Branch>
        <bnk:branchID>11111</bnk:branchID>
        <bnk:branchName>Istanbul Branch</bnk:branchName>
        <bnk:branchLocation>Istanbul</bnk:branchLocation>
    </bnk:Branch>
    <bnk:Branch>
        <bnk:branchID>12345</bnk:branchID>
        <bnk:branchName>New York Branch</bnk:branchName>
        <bnk:branchLocation>New York</bnk:branchLocation>
    </bnk:Branch>
    <bnk:Branch>
        <bnk:branchID>222222</bnk:branchID>
        <bnk:branchName>Miami Branch</bnk:branchName>
        <bnk:branchLocation>Miami</bnk:branchLocation>
    </bnk:Branch>
    <bnk:Employee>
        <bnk:employeeID>12345678</bnk:employeeID>
        <bnk:name>Tuna</bnk:name>
        <bnk:surname>TORE</bnk:surname>
        <bnk:profession>IOC Advocate, Abstract Developer</bnk:profession>
    </bnk:Employee>
    <bnk:Employee>
        <bnk:employeeID>987654321</bnk:employeeID>
        <bnk:name>James</bnk:name>
        <bnk:surname>Gosling</bnk:surname>
        <bnk:profession>Java GURU</bnk:profession>
    </bnk:Employee>
    <bnk:Employee>
        <bnk:employeeID>11111111</bnk:employeeID>
        <bnk:name>Linus</bnk:name>
        <bnk:surname>Torvalds</bnk:surname>
        <bnk:profession>Linux GURU</bnk:profession>
    </bnk:Employee>
   
</bnk:Bank>
XSD file (bank.xsd) for validating the XML (bank.xml)
<?xml version=”1.0″ encoding=”UTF-8″?>
<schema targetNamespace=”http://www.tunatore.wordpress.com/bank
elementFormDefault=”qualified”
xmlns=”http://www.w3.org/2001/XMLSchema
xmlns:bnk=”http://www.tunatore.wordpress.com/bank“>
    <element name=”Bank” type=”bnk:BankType”></element>
    <complexType name=”EmployeeType”>
     <sequence>
      <element name=”employeeID” type=”string”></element>
      <element name=”name” type=”string”></element>
      <element name=”surname” type=”string”></element>
      <element name=”profession” type=”string”></element>
     </sequence>
    </complexType>
  
    <complexType name=”BranchType”>
     <sequence>
      <element name=”branchID” type=”string”></element>
      <element name=”branchName” type=”string”></element>
      <element name=”branchLocation” type=”string”></element>
                <element name=”employee” type=”bnk:EmployeeType” minOccurs=”0″ maxOccurs=”unbounded”></element>
     </sequence>
    </complexType>
    <complexType name=”BankType”>
     <sequence>
                <element name=”BankName” type=”string” minOccurs=”0″ maxOccurs=”1″></element>
                <element name=”Branch” type=”bnk:BranchType” minOccurs=”0″ maxOccurs=”unbounded”></element>
                <element name=”Employee” type=”bnk:EmployeeType” minOccurs=”0″ maxOccurs=”unbounded”></element>
     </sequence>
    </complexType>
     
</schema>
validate method for validating the XML file defined
public static boolean validate() {
        boolean isValid = false;
      
        try {
            //location of the XML file
            File xmlFile = new File(Transformation.class.getResource(“bank.xml”)
                    .getFile().replace(“%20″, ” “));
            //location of the XSD file
            File xsdFile = new File(Transformation.class.getResource(“bank.xsd”)
                    .getFile().replace(“%20″, ” “));
            //Create source objects for XML and XSD files
            Source xmlSource = new StreamSource(xmlFile);
            Source xsdSource = new StreamSource(xsdFile);
            //Create SchemaFactory using XSD file
            SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
            Schema schema = factory.newSchema(xsdSource);
            //Validator will use xsd  file for validating the XML file
            Validator validator = schema.newValidator();
            // Validate the DOM tree.
            validator.validate(xmlSource);
            isValid = true;
          
        } catch (Exception e) {
            e.printStackTrace(System.out);
        }
      
        if(isValid == true)
        System.out.println(“Document is valid!”);
        else
        System.out.println(“Document is NOT valid!”);
      
        return isValid;
    }
SAMPLE OUTPUT FOR A VALID XML DOCUMENT;
run:
Document is valid!
BUILD SUCCESSFUL (total time: 9 seconds)
SAMPLE OUTPUT FOR AN INVALID XML DOCUMENT;
Use the following invalid XML document
<?xml version=”1.0″ encoding=”UTF-8″?>
<?xml-stylesheet type=”text/xsl” href=”bank.xsl”?>
<bnk:Bank
xmlns:bnk=”http://www.tunatore.wordpress.com/bank
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance
xsi:schemaLocation=”http://www.tunatore.wordpress.com/bank bank.xsd”>
    <bnk:NOTVALIDTAG>THIS ELEMENT IS NOT VALID</bnk:NOTVALIDTAG>
    <bnk:BankName>IT Bank of Turkey</bnk:BankName>
    <bnk:Branch>
        <bnk:branchID>11111</bnk:branchID>
        <bnk:branchName>Istanbul Branch</bnk:branchName>
        <bnk:branchLocation>Istanbul</bnk:branchLocation>
    </bnk:Branch>
    <bnk:Branch>
        <bnk:branchID>12345</bnk:branchID>
        <bnk:branchName>New York Branch</bnk:branchName>
        <bnk:branchLocation>New York</bnk:branchLocation>
    </bnk:Branch>
    <bnk:Branch>
        <bnk:branchID>222222</bnk:branchID>
        <bnk:branchName>Miami Branch</bnk:branchName>
        <bnk:branchLocation>Miami</bnk:branchLocation>
    </bnk:Branch>
    <bnk:Employee>
        <bnk:employeeID>12345678</bnk:employeeID>
        <bnk:name>Tuna</bnk:name>
        <bnk:surname>TORE</bnk:surname>
        <bnk:profession>IOC Advocate, Abstract Developer</bnk:profession>
    </bnk:Employee>
    <bnk:Employee>
        <bnk:employeeID>987654321</bnk:employeeID>
        <bnk:name>James</bnk:name>
        <bnk:surname>Gosling</bnk:surname>
        <bnk:profession>Java GURU</bnk:profession>
    </bnk:Employee>
    <bnk:Employee>
        <bnk:employeeID>11111111</bnk:employeeID>
        <bnk:name>Linus</bnk:name>
        <bnk:surname>Torvalds</bnk:surname>
        <bnk:profession>Linux GURU</bnk:profession>
    </bnk:Employee>
   
</bnk:Bank>
OUTPUT OF THE PROGRAM
run:
org.xml.sax.SAXParseException: cvc-complex-type.2.4.a: Invalid content was found starting with element ‘bnk:NOTVALIDTAG’. One of ‘{“http://www.tunatore.wordpress.com/bank”:BankName, “http://www.tunatore.wordpress.com/bank”:Branch, “http://www.tunatore.wordpress.com/bank”:Employee}’ is expected.
        at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:195)
        at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(ErrorHandlerWrapper.java:131)
        at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:384)
        at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:318)
        at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator$XSIErrorReporter.reportError(XMLSchemaValidator.java:417)
        at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.reportSchemaError(XMLSchemaValidator.java:3182)
        at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.handleStartElement(XMLSchemaValidator.java:1806)
        at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.startElement(XMLSchemaValidator.java:705)
        at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(XMLNSDocumentScannerImpl.java:400)
        at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2755)
        at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:648)
        at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:140)
        at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:511)
        at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:808)
        at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:737)
        at com.sun.org.apache.xerces.internal.jaxp.validation.StreamValidatorHelper.validate(StreamValidatorHelper.java:144)
        at com.sun.org.apache.xerces.internal.jaxp.validation.ValidatorImpl.validate(ValidatorImpl.java:111)
        at javax.xml.validation.Validator.validate(Validator.java:127)
        at xmlxsltexample.Transformation.validate(Transformation.java:75)
        at xmlxsltexample.Transformation.produceHTMLfromXMLwithXSL(Transformation.java:30)
        at xmlxsltexample.Transformation.main(Transformation.java:22)
Document is NOT valid!
BUILD SUCCESSFUL (total time: 2 seconds)

Thursday 11 April 2013

jsp code to download file from server

<%
File file = new File("/ed63/ora510/eld611i2comn/html/eisrs/data/backuploadbalance.xlsx");
String resultXLS="excelfileName1441.xlsx";
response.setHeader("Content-Length", ""+file.length());
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet; charset=UTF-8");
response.setHeader("content-disposition", "attachement; filename=\"" + resultXLS + "\"");
response.setHeader("Content-Transfer-Encoding", "binary");

FileInputStream fileIn = new FileInputStream(file);
OutputStream outExcel = response.getOutputStream();

byte[] outputByte = new byte[8192];
//copy binary contect to output stream
while(fileIn.read(outputByte, 0, 8192) != -1)
{
outExcel.write(outputByte, 0, 8192);
}

fileIn.close();
outExcel.flush();
outExcel.close();

%>

Wednesday 10 April 2013

Stubs and Skeletons

Stubs and Skeletons
RMI uses a standard mechanism (employed in RPC systems) for communicating with remote objects: stubs and skeletons. A stub for a remote object acts as a client's local representative or proxy for the remote object. The caller invokes a method on the local stub which is responsible for carrying out the method call on the remote object. In RMI, a stub for a remote object implements the same set of remote interfaces that a remote object implements.

When a stub's method is invoked, it does the following:

1) initiates a connection with the remote JVM containing the remote object,
2) marshals (writes and transmits) the parameters to the remote JVM,
3) waits for the result of the method invocation,
4) unmarshals (reads) the return value or exception returned, and
5) returns the value to the caller.

The stub hides the serialization of parameters and the network-level communication in order to present a simple invocation mechanism to the caller.

In the remote JVM, each remote object may have a corresponding skeleton (in Java 2 platform-only environments, skeletons are not required).

The skeleton is responsible for dispatching the call to the actual remote object implementation.

When a skeleton receives an incoming method invocation it does the following:

1) unmarshals (reads) the parameters for the remote method,
2) invokes the method on the actual remote object implementation, and
3) marshals (writes and transmits) the result (return value or exception) to the caller.

In the Java 2 SDK, Standard Edition, v1.2 an additional stub protocol was introduced that eliminates the need for skeletons in Java 2 platform-only environments. Instead, generic code is used to carry out the duties performed by skeletons in JDK1.1. Stubs and skeletons are generated by the rmic compiler.

Reference:
http://java.sun.com/j2se/1.5.0/docs/guide/rmi/spec/rmi-arch2.html

Sunday 9 October 2011

Marshalling vs UnMarshalling

Marshalling: The process of converting java object into xml instance 


Unmarsalling: The process of converting XML instance into java objects




Marshal Java object to xml file :-



import java.io.FileOutputStream;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;

public class ObjectToXml {
  public static void main(String[] argsthrows Exception {
    JAXBContext contextObj JAXBContext.newInstance(Employee.class);

    Marshaller marshallerObj contextObj.createMarshaller();
    marshallerObj.setProperty(Marshaller.JAXB_FORMATTED_OUTPUTtrue);

    Student myStudent = new Student();
    myStudent.set
Gender("M");
    myStudent.setName("Amar");
    myStudent.setAge(20);
    marshallerObj .marshal(object, new FileOutputStream("Student.xml")); 
  }


UnMarshal xml file to Java object :-

import java.io.FileOutputStream;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;

public class XmlToObject {
  public static void main(String[] argsthrows Exception {
    JAXBContext jc = JAXBContext.newInstance ();

    Unmarshaller u = jc.createUnmarshaller ();  
      
    File f = new File ("Student.xml");
    JAXBElement element = (JAXBElement) u.unmarshal (f);

    Student myStudent = (Student) element.getValue ();
    System.out.println (myStudent.getGender ());
    System.out.println (myStudent.getName ());
    System.out.println (myStudent.getAge ());
  }




import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
class Student{
  private String gender;

  private String name;

  private int age;

  public String getGender() {
    return gender;
  }

  public void setGender(String gender) {
    this.gendergender;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name name;
  }

  public int getAge() {
    return age;
  }

  public void setAge(int age) {
    this.ageage;
  }



Student.xml



<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<student>
    <gender>M</gender>
    <name>Amar</name>
    <age>20</age>
</student>

Serialization: The process of converting java object into byte stream.

Deserialization: The process of converting the byte stream into java object.