Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

XMLEncoder

 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I am using XMLEncoder to save java object to xml file. But there is something wrong! Please help!


import java.beans.XMLDecoder;
import java.beans.XMLEncoder;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;

public class DataObjectHelper {

public DataObjectHelper() {
super();
// TODO Auto-generated constructor stub
}

public static void write(Object f, String filename) throws Exception {
XMLEncoder encoder = new XMLEncoder(new BufferedOutputStream(
new FileOutputStream(filename)));
encoder.writeObject(f);
encoder.close();
}

public static Object read(String filename) throws Exception {
XMLDecoder decoder = new XMLDecoder(new BufferedInputStream(
new FileInputStream(filename)));
Object o = decoder.readObject();
decoder.close();
return o;
}

public static void main(String[] args) {
System.out.println("Start");
try {
DataObject ld =new DataObject( "");
ld.setName("name");
DataObjectHelper.write((DataObject) ld, "c:\\Label.xml");
} catch (Exception e) {
System.out.println("Exception");
e.printStackTrace();
}
}
}



where

public class DataObject {
private String name;

/**
* @param name
*/
public DataObject(String name) {
// TODO Auto-generated constructor stub
this.name = name;
}

/**
* @return Returns the name.
*/
public String getName() {
return name;
}

/**
* @param name The name to set.
*/
public void setName(String name) {
this.name = name;
}

}



and have the output as:


<?xml version="1.0" encoding="UTF-8"?>
<java version="1.4.2_07" class="java.beans.XMLDecoder">
<object class="tycoint.ITransportKPE.DataObject"/>
</java>


Why the field name is not included in the xml result?

Regards
Tyson
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The way I read the JavaDocs for XMLEncoder, only public variables are encoded. This fits the general Java Beans approach philosophy. Your name String is private.
Bill
 
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try having a public no-param constructor for DataObject.
 
Ranch Hand
Posts: 776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Lewin Chan:
Try having a public no-param constructor for DataObject.



I agree.

I would have thought that InstantiationException(s) would be reported.

Guy
 
Guy Allard
Ranch Hand
Posts: 776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by William Brogden:
The way I read the JavaDocs for XMLEncoder, only public variables are encoded. This fits the general Java Beans approach philosophy. Your name String is private.
Bill



I do not understand that.

The instance variables can, and indeed should be private.

Guy
 
Ranch Hand
Posts: 532
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can accomplish this by providing getters & setters.
See this link

Originally posted by Guy Allard:


I do not understand that.

The instance variables can, and indeed should be private.

Guy

 
Seriously Rick? Seriously? You might as well just read this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic