1 Reply Latest reply: May 11, 2010 7:16 PM by EJP RSS

    XMLEncoder does not create xml tags for null values

    843790
      Hi Experts,

      Below mentioned is my problem.

      I have a class like this.
      package com;
      
      
      public class Person {
           
           private String name;
           private int age;
           private String lastName;
      
                    public Person(){
                name="Nish";
                age=10;
                lastName="C";
           }
           public String getName() {
                return name;
           }
           public void setName(String name) {
                this.name = name;
           }
           public int getAge() {
                return age;
           }
           public void setAge(int age) {
                this.age = age;
           }
           public String getLastName() {
                return lastName;
           }
           public void setLastName(String lastName) {
                this.lastName = lastName;
           }
                
      }
      And i have a test class to create XML of the class
      package com;
      
      import java.beans.XMLEncoder;
      import java.io.FileNotFoundException;
      import java.io.FileOutputStream;
      import java.io.OutputStream;
      import java.util.ArrayList;
      import java.util.List;
      
      public class TestClass {
      
           public static void main(String[] args) {
                TestClass testClass=new TestClass();
                testClass.check();
           }
           
           
      
      
      public void check(){
           OutputStream out;
           try {
                out = new FileOutputStream("d:\\check.xml");
                XMLEncoder xmEncoder = new XMLEncoder(out);
                Person person=new Person();
                System.out.println("TestClass.check()"+person.getName());
                xmEncoder.writeObject(person);
                xmEncoder.close();
                System.out.println("TestClass.check()-->completed");
           } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
           }
           
           
      }
      }
      And this is the Output i get in the check.xml..
      <?xml version="1.0" encoding="UTF-8"?> 
      <java version="1.5.0_02" class="java.beans.XMLDecoder"> 
       <object class="com.Person"/> 
      </java>
      Now this is my question...
      1. Why do the default values not coming in the generated XML.
      2. Even if i remove the constructor in my bean class i am getting the same XML. So how do i get the XML tags for Null values of my beans.
      For instance my bean has lastName as Null value and how do i get this in XML like
      <void property="lastName"> 
         <string>null</string> 
        </void>
      or utleast like
      <void property="lastName"> 
         <string>null</string> 
        </void>
      Pls let me know if this is possible..
        • 1. Re: XMLEncoder does not create xml tags for null values
          EJP
          Below mentioned is my problem.
          Are you sure it's a problem?
          1. Why do the default values not coming in the generated XML.
          Because it's a waste of time and space. The value will appear anyway, why encode and transport and decode it?
          2. Even if i remove the constructor in my bean class i am getting the same XML. So how do i get the XML tags for Null values of my beans.
          Why do you think you want them?