Convert a bean to XML persistence : JavaBeans « Development « Java Tutorial

Java Tutorial
1. Language
2. Data Type
3. Operators
4. Statement Control
5. Class Definition
6. Development
7. Reflection
8. Regular Expressions
9. Collections
10. Thread
11. File
12. Generics
13. I18N
14. Swing
15. Swing Event
16. 2D Graphics
17. SWT
18. SWT 2D Graphics
19. Network
20. Database
21. Hibernate
22. JPA
23. JSP
24. JSTL
25. Servlet
26. Web Services SOA
27. EJB3
28. Spring
29. PDF
30. Email
31. J2ME
32. J2EE Application
33. XML
34. Design Pattern
35. Log
36. Security
37. Apache Common
38. Ant
39. JUnit
Java
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Tutorial » Development » JavaBeans 
6. 56. 6. Convert a bean to XML persistence
import java.beans.XMLEncoder;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;

public class Main {
  public static void main(String[] argsthrows Exception {
    Item bean = new Item();
    bean.setId(new Long(1));
    bean.setItemName("a");
    bean.setItemColour("Red");
    bean.setItemQuantities(new Integer(100));

    XMLEncoder encoder = new XMLEncoder(new BufferedOutputStream(new FileOutputStream("Bean.xml")));

    encoder.writeObject(bean);
    encoder.close();
  }
}

class Item {
  private Long id;
  private String itemName;
  private String itemColour;
  private Integer itemQuantities;
  public Long getId() {
    return id;
  }
  public void setId(Long id) {
    this.id = id;
  }
  public String getItemName() {
    return itemName;
  }
  public void setItemName(String itemName) {
    this.itemName = itemName;
  }
  public String getItemColour() {
    return itemColour;
  }
  public void setItemColour(String itemColour) {
    this.itemColour = itemColour;
  }
  public Integer getItemQuantities() {
    return itemQuantities;
  }
  public void setItemQuantities(Integer itemQuantities) {
    this.itemQuantities = itemQuantities;
  }
}
6. 56. JavaBeans
6. 56. 1. Listening for Changes to the Selected File in a JFileChooser Dialog
6. 56. 2. Get a list of selected files
6. 56. 3. Listening for Changes to the Current Directory in a JFileChooser Dialog
6. 56. 4. Displaying the Current Directory in the Title of a JFileChooser Dialog
6. 56. 5. Setting an Accessory Component in a JFileChooser Dialog
6. 56. 6. Convert a bean to XML persistence
6. 56. 7. Listen for bean's property change event
6. 56. 8. List property names of a Bean
6. 56. 9. Prevent bean's property being serialized to XML
6. 56. 10. Create an instance a Bean
6. 56. 11. Convert an XML persistence to bean
6. 56. 12. Determine bean's property type
6. 56. 13. Listen for a constrained property change
6. 56. 14. Bean has a single property called property.
6. 56. 15. Implementing a Bound Property
6. 56. 16. Implementing a Constrained Property: fires a PropertyChangeEvent whenever its value is about to be changed.
6. 56. 17. Instantiating a Bean
6. 56. 18. Listing the Property Names of a Bean
6. 56. 19. Getting and Setting a Property of a Bean
6. 56. 20. Get and set the value of a property in a bean using Expression and Statement
6. 56. 21. Get and set an Object type property
6. 56. 22. gets and sets a primitive type property
6. 56. 23. gets and sets an array type property
6. 56. 24. Serializing a Bean to XML: XMLEncoder only persists the value of public properties.
6. 56. 25. Deserializing a Bean from XML
6. 56. 26. Preventing a Bean Property from Being Serialized to XML
6. 56. 27. Serializing an Immutable Bean Property to XML
6. 56. 28. Listening for a Property Change Event: A property change event is fired when a bound property is changed.
6. 56. 29. Listening for a Vetoable Property Change Event
6. 56. 30. Read bean's property value
6. 56. 31. extends SimpleBeanInfo
6. 56. 32. Get and set properties on a bean
6. 56. 33. Process bean properties getter by applying the JavaBean naming conventions.
6. 56. 34. Is JavaBean Compliant Setter
6. 56. 35. Constructs a method name from element's bean name for a given prefix
6. 56. 36. Returns attribute's setter method. If the method not found then NoSuchMethodException will be thrown.
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.