VIVEK SHAH

011011010010000001100001011001000110010001101001011000110111010001100101011001000010000001110100011011110010000001101010011000010111011001100001 Click Here

Custom Search

        JAXB represents XML documents as Java classes with properties that are specific to the particular XML document


How JAXB works

First of all Design xml file of ur requirement as for sample i have designed my xml file as below as per my requirement




go through this link which will convert your xml file to XSD file converter


copy and paste your xml file in to text area and get the XSD file  as below




1.     Choose File > New Project. Under Categories, select Java. Under Projects, select Java Application and click Next.
2.     Under Project Name, enter JseSimpleClientReport and click Finish. In the Projects window, the JseSimpleClientReport project appears.
3.     In the Projects window, right-click the JseSimpleClientReport node and choose New > Other > XML > JAXB Binding. Then click Next. The New JAXB Binding wizard appears. 




open JAXB wizard







The settings in the wizard above serve the following purposes:

·    Binding Name. Specifies the name of the new JAXB binding, which will be used to identify it.
·    Project. Displays the name of the current project.
·    Schema File. The file that you want to work with can either be available locally or on-line.
·    Schema Type. The following types of XML document are supported:
o    XML Schema
o    Relax NG
o    Relax NG Compact
o    XML DTD
o    WSDL
·    Package Name. Specifies the package to which the Java objects will be generated.
·    Compiler Options. Many compiler options are available, as described here in the Java EE 5 Tutorial. However, in relation to the JAXB Wizard, only the following are relevant and you can set them using checkboxes in the wizard:
o    nv. Do not perform strict validation of the input schema(s). By default, strict validation of the source schema is performed before processing. Note that this does not mean the binding compiler will not perform any validation; it simply means that it will perform less-strict validation.
o    readOnly. Force the compiler to mark the generated Java sources read-only. By default, the compiler does not write-protect the Java source files it generates.
o    npa. Suppress the generation of package level annotations into **/package-info.java. Using this switch causes the generated code to internalize those annotations into the other generated classes.
o    verbose. Produce maximum compiler output, such as progress information and warnings.
o    quiet. Suppress compiler output, such as progress information and warnings.
·         Use Extension. By default, the compiler strictly enforces the rules outlined in the Compatibility chapter of the JAXB Specification. In the default (strict) mode, you are also limited to using only the binding customizations defined in the specification. By using this option, you will be allowed to use the JAXB Vendor Extensions.
·         Use Binding File. Lets you import and edit one or more JAXB binding customization files.
·         Use Catalog File. Lets you import and edit OASIS catalog files.


·         Type employeeBindling in Binding Name and org.reply2vivekshah.blog.employeeBinding in Package Name.
·         Next to Select From Local File System, click Browse and browse to the WSDL file that you downloaded at the start of this tutorial.
·         In the Schema Type drop-down, choose WSDL (unless the IDE chose this automatically). You should now see the following:







Click Finish.

The IDE generates the Java objects from the given XML document. In the next section, we examine the Java objects in the IDE.

As with other artifacts that the IDE regenerates whenever a project is built, the Java objects are generated in the build folder. Open the Files window and then you can browse to the location of the generated Java objects. Starting in NetBeans IDE 6.7, these Java objects are also displayed in the Projects window, under the Generated Sources node.








and class with getter setter method  automatically generated as below



Set some values for the JAXB class, such as the following  (just go through the hilighted part)




follwing code is used for generating xml File

  File f1 = new File("./employeeXML.xml");
        try {
            FileOutputStream fout = new FileOutputStream(f1);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(mainWindow.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            JAXBContext jaxbCtx = JAXBContext.newInstance(e1.getClass().getPackage().getName());
            javax.xml.bind.Marshaller marshaller = jaxbCtx.createMarshaller();
            marshaller.setProperty(javax.xml.bind.Marshaller.JAXB_ENCODING, "UTF-8"); //NOI18N
            marshaller.setProperty(javax.xml.bind.Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
            //it will create the xml file
            marshaller.marshal(e1, f1);
        } catch (javax.xml.bind.JAXBException ex) {
            // XXXTODO Handle exception
            java.util.logging.Logger.getLogger("global").log(java.util.logging.Level.SEVERE, null, ex); //NOI18N
        }



0 comments:

Post a Comment