This week's book giveaway is in the Java in General forum.
We're giving away four copies of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 and have ishori Sharan & Adam L Davis on-line!
See this thread for details.
Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

XMLEncoder

 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try having a public no-param constructor for DataObject.
 
Ranch Hand
Posts: 776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic