Easiest way to read xml


What is XMLBeans?


XMLBeans is a technology for accessing XML by binding it to Java types. XMLBeans provides several ways to get at the XML, including:


As the complexity of enterprise applications increases, constraints and rules on the structure of XML documents become more and more stringent. Also, with the rapid adoption of Web services across the industry, XML now plays an undeniably important role across many platforms. All of this means it is imperative that applications have an easy and powerful mechanism for handling XML.

XMLBeans provides such a mechanism. XMLBeans is used for XML data binding. It is immensely powerful in that it supports the full W3C XML Schema specification, unlike other data-binding techniques that support only a subset of it. It is also surprisingly easy to use for developers who are accustomed to object-oriented manipulations.

  • Through XML schema that has been compiled to generate Java types that represent schema types. In this way, you can access instances of the schema through JavaBeans-style accessors after the fashion of "getFoo" and "setFoo".
    The XMLBeans API also allows you to reflect into the XML schema itself through an XML Schema Object model.
  • A cursor model through which you can traverse the full XML infoset.
  • Support for XML DOM.
For a more complete introduction, see the XMLBeans Overview or Getting Started With XMLBeans.
For more details on XMLBeans see the XMLBeans Wiki pages or the XMLBeans documentation (the Documentation tab on this website).

Installing XMLBeans


Requirements

Before installing XMLBeans, you will need to have JDK 1.4 installed and Ant. Once you have those, continue with the steps below.

Download XMLBeans

To start out, you'll need the XMLBeans binaries. You can get these in one of two ways:
  • To get started more quickly, you can Download the XMLBeans release. For full XQuery and XPath support, be sure to download the Saxon XQuery processor as follows:
    • For XmlBeans 2.0.0 and XMLBeans 2.1.0 you need Saxon 8.1.1 ONLY.
    • For XmlBeans 2.2.0 you need Saxon 8.6.1 (may work with later revisions, but not tested)
  • If you're interested, you can also build from the source.

Windows Setup

On Windows, do the following:
  1. Right-click My Computer, then click Properties.
  2. On the Advanced tab, click Environment Variables.
  3. Add an XMLBEANS_HOME variable whose value is the XMLBeans location.
    For example, if you downloaded the XMLBeans release to the root of your C drive, this might be c:\xmlbeans-1.0.2.
  4. Edit the PATH variable so that it includes the bin directory of your XMLBeans installation. For the XMLBeans release, you could add%XMLBEANS_HOME%\bin.
  5. Edit the CLASSPATH variable to include the xbean.jar included with XMLBeans.
    This is located in the lib directory. If you built XMLBeans from source, you can also use the JAR file in the build/ar or build/lib directories.
  6. For XMLBeans V2, also include the jsr173_1.0_api.jar (or jsr173_api.jar, whichever one came in your download) to the CLASSPATH variable.
    This is found in the lib (or build/lib if you built XMLBeans from source) directory.
Note that you may also need to edit your PATH variable so that the JDK is earlier in the path. For more information, see the XMLBeans FAQ.

UNIX/Linux Setup

On UNIX/Linux, update your environment as follows:
  1. export XMLBEANS_HOME=<location of XMLBeans root>
  2. export PATH=$PATH:$XMLBEANS_HOME/bin
  3. export CLASSPATH=$XMLBEANS_HOME/lib/xbean.jar:$CLASSPATH
    (or, if you built XMLBeans from source, export CLASSPATH=$XMLBEANS_HOME/build/lib/xbean.jar:$CLASSPATH)
  4. For XMLBeans V2, export CLASSPATH=$CLASSPATH:$XMLBEANS_HOME/lib/jsr173[_1.0]_api.jar
    (or, if you built XMLBeans from source, export CLASSPATH=$CLASSPATH:$XMLBEANS_HOME/build/lib/jsr173[_1.0]_api.jar)
Your updates might look like this:
export XMLBEANS_HOME=/home/me/xmlbeans-2.1.0
export PATH=$PATH:$XMLBEANS_HOME/bin
export CLASSPATH=$XMLBEANS_HOME/lib/xbean.jar:$XMLBEANS_HOME/lib/jsr173_1.0_api.jar:$CLASSPATH
Once you've updated your environment paths, you should be set up to use the tools that come with XMLBeans. These include a schema compiler, an instance validator, and more.
To get started understanding and using XMLBeans, see Getting Started with XMLBeans.

Start off with your own stuff.


  1. Install XMLBEANS.
  2. create XSD file using your XML file (you can use this site for creating XSD using your XML)
  3. Compile your schema in command prompt. Use scomp to compile the schema, generating and jarring Java types. For example, to create a employeeschema.jar from an employeesschema.xsd file:

    scomp -out employeeschema.jar employeeschema.xsd
    (or)
    scomp -out employeeschema.jar employeeschema.xsd -compiler "C:\Program Files\Java\jdk1.7.0\bin\javac.exe"

  4. Write code. With the generated JAR on your classpath, write code to bind an XML instance to the Java types representing your schema. Here's an example that would use types generated from an employees schema:
    File xmlFile = new File("c:\employees.xml"); 
    
    // Bind the instance to the generated XMLBeans types.
    EmployeesDocument empDoc = 
     EmployeesDocument.Factory.parse(xmlFile); 
    
    // Get and print pieces of the XML instance.
    Employees emps = empDoc.getEmployees(); 
    Employee[] empArray = emps.getEmployeeArray(); 
    for (int i = 0; i < empArray.length; i++) 
    { 
     System.out.println(empArray[i]); 
    }
    
    

Exapmle Java File.


package log.client.client.logviewer.util.anand;

import java.io.File;
import java.io.ObjectInputStream.GetField;
import java.util.ArrayList;
import java.util.List;

import org.apache.xmlbeans.XmlCursor;


import noNamespace.LogRegistryDocument;
import noNamespace.LogRegistryDocument.LogRegistry.Application;
import noNamespace.LogRegistryDocument.LogRegistry.Application.LogCollector;
import noNamespace.LogRegistryDocument.LogRegistry.Application.LogCollector.Hosts.Host;

public class NavigatorUtil {
 public static List getValueObject(String filePath) {
  //XmlCursor cursor = null;
  List MenuLoader= null;
  try {
   File inputXMLFile = new File(filePath);
   LogRegistryDocument persGrpDoc = LogRegistryDocument.Factory.parse(inputXMLFile);
   LogRegistryDocument.LogRegistry adminGrp = persGrpDoc.getLogRegistry();
   // for printing whole object
   /*cursor = adminGrp.newCursor();
   System.out.println(cursor.getTextValue());*/
   
   System.out.println("Xml Admin size====="+adminGrp.sizeOfApplicationArray());
   MenuLoader=new ArrayList();
   for(int i=0;i< adminGrp.sizeOfApplicationArray();i++){
    TreeMenuItemBean menu=new TreeMenuItemBean();
    Application menuTemp= adminGrp.getApplicationArray(i);
    menu.setApplicationID(menuTemp.getId());
    menu.setApplicationName(menuTemp.getName());
    menu.setApplicationEn_type(menuTemp.getEnvType());
    List LogCollector=new ArrayList();
    for(int j=0;j< adminGrp.getApplicationArray(i).sizeOfLogCollectorArray();j++){
     LogCollectorBean log=new LogCollectorBean();
     LogCollector logTemp = adminGrp.getApplicationArray(i).getLogCollectorArray(j);
     log.setLogId(logTemp.getId());
     log.setLogName(logTemp.getName());
     List HostCollector=new ArrayList();
     for(int k=0;k< adminGrp.getApplicationArray(i).getLogCollectorArray(j).getHosts().sizeOfHostArray();k++){
      HostBean host=new HostBean();
      Host hostTemp = adminGrp.getApplicationArray(i).getLogCollectorArray(j).getHosts().getHostArray(k);
      host.setHostName(hostTemp.getName());
      host.setSourceFolder(hostTemp.getSourceFolder());
      host.setFileNamePattern(hostTemp.getFileNamePattern());
      HostCollector.add(host);
     }
     log.setHost(HostCollector);
     LogCollector.add(log);
    }
    menu.setLogCollector(LogCollector);
    MenuLoader.add(menu);
   }
  } catch (Exception e) {
   e.printStackTrace();
  } finally {
   //cursor.dispose();
  }
  System.out.println("list size = "+MenuLoader.size());
  return MenuLoader;
  
 }
 
 public static void main(String args[]){
  String filePath = "D:\\LogRegistry.xml";
  List MenuLoader=(List)getValueObject(filePath);
  int z=0;
  for(TreeMenuItemBean as:MenuLoader){
   System.out.println("id = "+as.getApplicationID());
   System.out.println("name = "+as.getApplicationName());
   System.out.println("en_type = "+as.getApplicationEn_type());
   List log=(List) as.getLogCollector();
   
   System.out.println("log size = "+log.size());
   for(int x=0;x< log.size();x++){
    LogCollectorBean logTemp=(LogCollectorBean) log.get(x);
    System.out.println("Log id = "+logTemp.getLogId());
    System.out.println("Log name = "+logTemp.getLogName());
    List host=(List) logTemp.getHost();
    for(int y=0;y< log.size();y++){
     HostBean hostTemp=(HostBean) host.get(y);
     System.out.println("Host name = "+hostTemp.getHostName());
     System.out.println("Host folder = "+hostTemp.getSourceFolder());
     System.out.println("Host pattern = "+hostTemp.getFileNamePattern());
    }
   }
   System.out.println("_____________________________");
   z++;
  }
 }
}

Read a tutorial.

Read apche tutorial to get a sense of XMLBeans basics.

Read documentation and other information.

On apche documentation page, you'll find links to several topics that describe XMLBeans features and how to use them. You'll also find links to Javadoc reference on the XMLBeans API.
You can also check out the FAQ, which is updated with new answers as they're needed.
Don't forget the XMLBeans Wiki, which collects lots of valuable information.

Check out the samples.

Many of the XMLBeans features are illustrated in apche samples.

Comments

Popular posts from this blog

SinglePass Terms of Service

Jasper Report Viruatization

JasperReports Tutorial