Results 1 to 5 of 5
  1. #1
    buntyindia is offline Member
    Join Date
    May 2011
    Posts
    13
    Rep Power
    0

    Question JAVA Object to XML to JAVA to XML

    In one of my project below is my requirement:-

    -Create input XMLs from Java class..
    -Take input from user then store that data to Java object and submit for processing
    -After processing print response java object again in XML.

    What is the best API to achieve this. Will be helpful if any example is available.
    Last edited by buntyindia; 05-11-2011 at 07:17 PM.

  2. #2
    d3n1s is offline Member
    Join Date
    Apr 2011
    Posts
    69
    Rep Power
    0

    Default

    Quote Originally Posted by buntyindia View Post
    In one of my project below is my requirement:-

    -Create input XMLs from Java class..
    -Take input from user then store that data to Java object and submit for processing
    -After processing print response java object again in XML.

    What is the best API to achieve this. Will be helpful if any example is available.
    XMLSerializer is exactly what you want. I don't have code examples right now but once i get back home i can post a few. A quick search on google should get you in the right direction though.

  3. #3
    cld71 is offline Member
    Join Date
    Feb 2011
    Posts
    4
    Rep Power
    0

    Default

    If you are using Java Beans to hold the data, then you can use java.beans.XMLDecoder and java.beans.XMLEncoder.

  4. #4
    buntyindia is offline Member
    Join Date
    May 2011
    Posts
    13
    Rep Power
    0

    Default Will it handle Iterative ArrayList Classes in XML generation?

    Thanks for the Reply =)

    I have a Java Object that have some fileds like arraylist<Class> will it take care of these fields as well ?

    If that arrayList<Class> have another fields that can be normal fileds or arraylist of another class, will it take care of all fields during XML generation?

    Example:-

    Root Class have below fields
    private int ArCt;
    private ArrayList<ClassChild1> Ar;
    ......
    Setters
    ......
    Getters

    ClassChild1.class
    private int PlnCd;
    private int cArCt;
    private ArrayList cAr<ClassChild2> cAr;

    ClassChild2.class
    private String PlNm

    XML Needed - Example from above object
    <root>
    <ArCt>1</ArCt>
    <Ar>
    <ClassChild1>
    <PlnCd>S</PlnCd>
    <cArCt>2</<cArCt>
    <cAr>
    <ClassChild2>
    <PlNm>Plan1</PlNm>
    </ClassChild2>
    <ClassChild2>
    <PlNm>Plan2</PlNm>
    </ClassChild2>
    </cAr>
    </ClassChild1>
    </Ar>
    </root>
    Last edited by buntyindia; 05-15-2011 at 07:27 PM.

  5. #5
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    Quote Originally Posted by buntyindia View Post
    Thanks for the Reply =)

    I have a Java Object that have some fileds like arraylist<Class> will it take care of these fields as well ?

    i made a test with xstream
    since this is the xml api i know (and also used in the persistence tutorial found in my signature). first i create some instances with:

    Java Code:
    import java.util.ArrayList;
    
    import com.thoughtworks.xstream.XStream;
    import com.thoughtworks.xstream.io.xml.DomDriver;
    
    class Root {
    	private int ArCt;
    	private ArrayList<ClassChild1> Ar;
    
    	public Root() {
    		// create arraylist for Ar
    		Ar = new ArrayList<ClassChild1>();
    		// create one child1
    		ClassChild1 c1 = new ClassChild1();
    		ClassChild2 c2 = new ClassChild2();
    		c1.PlnCd = 10;
    		c1.cArCt = 20;
    		c2.PlNm = "PlNm of class ClassChild2";
    		c1.cAr.add(c2);
    		Ar.add(c1);
    	}
    
    	class ClassChild1 {
    
    		int PlnCd;
    		int cArCt;
    		ArrayList<ClassChild2> cAr;
    
    		public ClassChild1() {
    			cAr = new ArrayList<ClassChild2>();
    		}
    	}
    
    	class ClassChild2 {
    		String PlNm;
    	}
    
    	public static void main(String[] args) {
    		XStream xstream = new XStream(new DomDriver());
    		Root root = new Root();
    		System.out.println(xstream.toXML(root));
    	}
    }

    i removed the access modifiers in the field so i can access them directly without getters and setters (bad practice!) and xstream generate this xml-output:

    <root.Root>
    <ArCt>0</ArCt>
    <Ar>
    <root.Root_-ClassChild1>
    <PlnCd>10</PlnCd>
    <cArCt>20</cArCt>
    <cAr>
    <root.Root_-ClassChild2>
    <PlNm>PlNm of class ClassChild2</PlNm>
    <outer-class reference="../../../../.."/>
    </root.Root_-ClassChild2>
    </cAr>
    <outer-class reference="../../.."/>
    </root.Root_-ClassChild1>
    </Ar>
    </root.Root>

    as you can see, also composition and aggregation is provided by xsteam. please note, you can have absolute class references instead of ../..

Similar Threads

  1. Java add flash object
    By Dennis in forum Advanced Java
    Replies: 0
    Last Post: 01-06-2011, 08:06 PM
  2. Replies: 2
    Last Post: 11-26-2010, 02:12 AM
  3. how to insert a ole object using java
    By ak88 in forum Advanced Java
    Replies: 0
    Last Post: 01-05-2010, 03:26 PM
  4. Java Object Migration
    By vijat in forum Advanced Java
    Replies: 1
    Last Post: 10-23-2009, 01:18 PM
  5. need help about java class and object
    By mrjohn007 in forum New To Java
    Replies: 1
    Last Post: 01-28-2009, 05:09 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •