View RSS Feed


Extensible Markup Language (XML) is a set of rules for encoding documents in machine-readable form.

  1. Using the JAXP validation framework

    by , 11-28-2011 at 07:29 PM (My Java Tips)
    While working with XML documents, you need to validate the documents. You may use setValidating() method on a SAX or DOM factory. But Java 5.0 (JAXP 1.3) introduces JAXP validation framework which can also be used for validating XML documents.

    Using the JAXP validation framework is fairly simple and efficient. In JAXP 1.3, the validation is broken out into several classes within the new javax.xml.validation package. Lets go through the steps:

    1. Load the model in to ...
    Tags: dom, java 5.0, jaxp, sax Add / Edit Tags
    Java EE , XML
  2. XSLT processing in Java

    by , 11-28-2011 at 07:25 PM (My Java Tips)
    XSLT (Extensible Stylesheet Language Transformations) is used to transform XML files into other formats like HTML format. There are many XSLT processors (libraries) available to be used in Java for XSLT transformation. These libraries can be used from a Java application like JSP/Servlet to read a XML file and to transform it into a HTML.

    An XSLT processor takes two inputs: an XML file and an XSLT stylesheet.

    For this post, I have chosen Xalan-Java library for the transformation. ...
    Tags: html, xalan-java, xml, xslt Add / Edit Tags
  3. XML Pull Parsing (Demo)

    by , 11-22-2011 at 06:58 PM (My Java Tips)
    XML Pull Parsing makes parsing XML documents easier and efficient. This post introduces this API.

    You may get the required API from
    Java docs are available at:

    Let me present an example of parsing XML using XML Pull Parsing.

    Java Code:
    public class SimpleXmlPullApp
         public static void main (String args[])
  4. Parsing XML documents using DOM

    by , 11-09-2011 at 06:48 PM (My Java Tips)
    Parsing XML documents using DOM is easy and simple. Let do this with example.
    DocumentBuilder is used to obtain Document from XML file. It is an abstract class. Once we have reference of DocumentBuilder, we will create Document instance. Document class instance is created using parse method of DocumentBuilder class which takes xml file as parameter. By doing so, we will have XML document in the memory.

    We have following XML file to parse:
    XML Code:
     <?xml version="1.0"?>

    Updated 11-09-2011 at 07:06 PM by Java Tip

  5. Sync4J

    by , 11-07-2011 at 07:49 PM (My Java Tips)
    To create an implementation of SyncML protocol, you may use sync4j. I will introduce you to sync4j in this post.

    You may download it from:

    Sync4j possesses layered architecture and the layers are:

    Core layer - The actual SyncML handling is done by the core layer. Its responsibilities include XML parsing and conversion of the SyncML markup to an internal object representation. Sync4j can also convert internal ...
    Java ME , XML
  6. Transformations API for XML

    by , 11-07-2011 at 07:36 PM (My Java Tips)
    In the next few posts, I will write about how to perform an XSLT transformation using StAX APIs: Cursor API and Event Iterator API.

    TrAX (Transformations API for XML) is a Java API for performing XSLT transformations. In J2SE 1.5, there are three different ways to represent the source and the result of an XSLT transformation and they are: by XML stream source/result, by SAX events source/result, and by DOM tree source/result. In J2SE 1.6 an XSLT transformation is performed using a ...
    Java SE , XML
  7. ANT's best practices

    by , 11-07-2011 at 07:28 PM (My Java Tips)
    ANT makes building complex Java applications easier. But many people argue that managing ANT scripts is itself a cumbersome task and involves a lot of effort. In the next few posts, I will talk about how one can adopt best practices for creating and managing ANT scripts that will make the life of developers and configuration managers easier.

    First thing is, one should follow a consistent style while writing XML for ANT files. If you format your ANT script, it will be visually appealing ...
    Ant , XML
  8. Pom

    by , 11-07-2011 at 07:01 PM (My Java Tips)
    Maven uses an XML project descriptor to generate the object model for project. This file is POM.XML and should be located at the top level directory of the project.

    The XML file contains all the needed information for the project. Apart from core information, it also contains groupid, version, name and all the libraries needed for the project.

    <artifactid>junit</artifactid> ...
    Maven , XML
  9. Loading properties from XML file

    by , 11-07-2011 at 06:58 PM (My Java Tips)
    In this post, I will present how to read properties from XML files.

    We have following XML file with the required properties:

    <!--?xml version="1.0" encoding="UTF-8"?-->
    <entry key="mode">alpha</entry>
    <entry key="entertaionment">movie</entry>
    DTD for the XML file ...
  10. ANT (Example)

    by , 11-07-2011 at 06:55 PM (My Java Tips)
    This post is all about an ANT example. I will present an ANT build script that will compile a project, and will generate a JAR file.

    I will name the project as Project X and will provide some useful description.

    <!--?xml version="1.0"?-->
    <project default="dist" name="Project X">
    <description>Test project</description>

    Now is the time to define the properties. ...
    Ant , XML
  11. POM (groupId:artifactId:versi on)

    by , 11-07-2011 at 06:22 PM (My Java Tips)
    A project object model (POM.xml) contains all necessary information about a project like configurations of plugins involved. You might be thinking about Ant build file. You may embed ant tasks inside the POM. A build.xml tells Ant precisely what to do when it is run, a POM states its configuration.

    A POM in simplest form is given below:

    xsi:schemaLocation=" ...
    Maven , XML
  12. Ant exec task

    by , 11-07-2011 at 06:19 PM (My Java Tips)
    To execute a system command from Ant, exec is used. Since different operating systems have different commands, therefore OS commands (if used), will restrict the build script to a particular operating system.

    One cannot interact with the forked program and if any attempt to read input in the forked program is made, an EOF (-1) will be thrown.

    The task delegates to Runtime.exec which in turn apparently calls ::CreateProcess. Generally, batch files (.bat) cannot by executed ...
    Ant , XML
  13. Using a custom task in Ant

    by , 11-06-2011 at 06:53 PM (My Java Tips)
    It is assumed that you have already defined a custom Ant task and wish to use it. In this post, I will show you how to do so.

    You first need to define the task by giving it a name and telling Ant the classfile that implements it and any classpath setting required to locate that classfile. This is done using the taskdef task, like so:

    <taskdef name="fileparser">classname="FileParser"
    classpath="."/&gt; ...
    Ant , XML
  14. Creating a Hibernate archive

    by , 11-06-2011 at 06:20 PM (My Java Tips)
    Consider a simple POJO with no direct coupling to Hibernate. The details of the Hibernate mapping will be specified in the Player.hbm.xml file.

    Player.hbm.xml is shown below.

    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    <class name="org.jboss.roster.Player" table="PlayerBean">
    <id ...
    XML , Hibernate
  15. JBoss - Logging Service

    by , 11-06-2011 at 06:13 PM (My Java Tips)
    Jboss uses Apache logging application called log4j for logging. You can find log4j.jar under lib director. The logging service is enabled from jboss.service.xml.

    The file will read like this:

    <!-- ================================================== ================== -->
    <!-- Log4j Initialization -->
    <!-- ================================================== ================== --> ...
    JBoss , XML
  16. Managing dependencies using Maven

    by , 11-05-2011 at 07:42 PM (My Java Tips)
    Maven can manage all jar file dependencies for the build machines. Let me explain how to do this.

    We use dependency element to refer to a jar file which reside in a remote repository. It is said that in near future, the dependencies will extend beyond jar files. Maven places the jar files (fetched from remote repository) on the local repository. The element has three child elements, , , and . Maven looks in the remote repository for a directory with the same name as ...
    Maven , XML
  17. XPath VS DOM

    by , 11-03-2011 at 12:55 PM (My Java Tips)
    We use Structured Query Language (SQL) for querying certain kinds of relational databases. Other less familiar query languages include Object Query Language (OQL) and XQuery. XPath, a query language used for querying XML documents.

    Following XPath query finds the titles of all the books in a document whose author is Neal Stephenson:

    //book[author="Neal Stephenson"]/title

    Following example uses DOM to find all the title elements of books by Neal ...
  18. JAXP/XPath example

    by , 11-03-2011 at 12:50 PM (My Java Tips)
    I will present an example that will search a XML file and then prints out the titles of all the books it finds.

    I will use following books.xml in the example.

    <book year="2000">

    <author>Neal Stephenson</author>
  19. file

    by , 11-03-2011 at 12:34 PM (My Java Tips) file is a configuration file (not in XML format). If you have a stand alone application, then should be in the directory where you issued the java command. In case of web application (JSP/Servlet), place at /WEB-INF/classes/.

    A sample properties file is given below:

    log4j.appender.stdout=org.apache.log4j.ConsoleAppe nder
    log4j.appender.stdout.layout=org.apache.log4j.Patt ...
    Java SE , XML
  20. Quering an XML document with a fixed XPath expression

    by , 11-01-2011 at 07:22 PM (My Java Tips)
    I will present a complete program to query an XML document with a fixed XPath expression.

    The method can throw several exceptions that must be declared in try- catch blocks. Do proper exception handling.

    Java Code:
    import org.w3c.dom.*;
    import org.xml.sax.SAXException;
    import javax.xml.parsers.*;
    import javax.xml.xpath.*;
    public class XPathExample {
      public static void main(String[]
  21. Binding a single namespace

    by , 11-01-2011 at 07:16 PM (My Java Tips)
    The XPath expression that finds the titles of all of Laiq's books will be //pre:book[pre:author="Laiq"]/pre:title/text(). However, you have to map the prefix pre to the URI.

    It's a little strange that the Namespace Context interface doesn't have a default implementation in the Java software development kit (JDK) or JAXP, but it doesn't. However, it's not hard to implement. Following simple implementation is for one namespace. You should map the xml prefix as well. ...
  22. Adding MIME types to JBOSS

    by , 11-01-2011 at 06:51 PM (My Java Tips)
    Consider that you have an application deployed on JBOSS application server. You offer different files in your application that can be opened or downloaded. But someone complains that the web application shows code of the file imbedded in the web page instead of opening or downloading it. What problem can there be?

    Actually you have to tell your JBOSS application server of the supported files. For this, you need to add an entry in web.xml. For instance:

    <mime-mapping> ...

    Updated 11-01-2011 at 06:54 PM by Java Tip

    JBoss , XML
  23. xsl output tag

    by , 11-01-2011 at 06:21 PM (My Java Tips)
    As you know, you may apply XSLT transformers and filters on XML documents in Java. To do so, you should know some basic XSLT tags. I will present xsl:output tag in this post.

    It is a top-level element, and must appear as a child node of or . The element defines the format of the output document.

    Example follows:

    omit-xml-declaration="yes|no" ...
  24. XML Prolog

    by , 11-01-2011 at 06:15 PM (My Java Tips)
    Developer who are working with XML, should know about XML prolog. This post will brief you about the said topic.

    Its an optional component of the XML document. If you wish to include it, then it must appear before the root element. A prolog consists of two parts:

    Part 1 comprises of XML declaration
    Part 2 comprises of Document Type Declaration (DTD)

    You can include both depending on your needs.


    <!--?xml ...
  25. SAX is underneath most XML processing

    by , 10-31-2011 at 06:08 PM (My Java Tips)
    SAX is a simple API for XML and is largely used because it is very popular because almost all XML processing involves SAX at some level.

    SAX is blazing fast and has been around for a while. XML is fairly easy to work with, but it's not an intuitive language in lots of ways. Most of the XML parsers these days use SAX since it already works pretty well.
    SAX is great at parsing XML. And if you know how to handle errors in SAX, then you know how to handle errors in almost any XML ...
  26. JAXP exposes SAX

    by , 10-31-2011 at 06:04 PM (My Java Tips)
    Many of you must be using JAXP API, the Java API for XML Processing for XML processing.

    The code below shows a fragment that uses JAXP for some SAX parsing.

    Java Code:
    SAXParserFactory factory = SAXParserFactory.newInstance();
    SAXParser parser = factory.newSAXParser();
    parser.parse(new File(args[0]), new MyHandler());
    This surely looks similar ...
  27. Java to JSON

    by , 10-30-2011 at 06:43 PM (My Java Tips)
    Creating JSON or XML from Java is simple. I'll show some short code sample.

    Java Code:
    Person p = new Person();
    JSONSerializer serializer = new JSONSerializer();
    String json = serializer.serialize( p );
    Created JSON String:

    Java Code:
       "class": "Person",
       "name": "William Shakespeare",
       "birthday": -12802392000000,
       "nickname": "Bill"
  28. Log method entry and exit

    by , 10-30-2011 at 06:39 PM (My Java Tips)
    The typical approach we use in Java is given below:

    Java Code:
    public String myMethod(String param)
      if (LOG.isDebugEnabled())
        LOG.debug("myMethod(" + param + ")");
    String result = // . . .
    if (LOG.isDebugEnabled())
      LOG.debug("myMethod() returns " + result);
    return result;

    This will work ...
    HiveMind , XML
  29. Repositories

    by , 10-30-2011 at 05:48 PM (My Java Tips)
    In Maven, projects can share dependencies which means that different project can use common jar files.

    These jar files will be updated over time and Maven keeps each project current with the latest jar file. These jar files will be placed in a repository on a remote server and are accessed by the projects. These files will be mirrored in a local repository under the Maven installation. The remote repository location is given in the file which is located in your Maven ...
    Maven , XML
  30. Hibernate Filters

    by , 10-21-2011 at 08:40 PM (My Java Tips)
    A Hibernate filter is a global, named, parameterized filter that may be enabled or disabled for a particular Hibernate session. Filters can be used like database views.

    Filters were introduced in Hibernate3. Using filters, you can define filter criteria and attach those filters at class and a collection level. A filter criteria is actually defining a restriction clause. This is same as "where" attribute available on the class and various collection elements. ...
    Hibernate , XML
Page 1 of 2 12 LastLast