users@jaxb.java.net

Re: Problem generating a XML document......help...

From: bmadhuri <bmadhuri_at_myway.com>
Date: Mon, 09 Jun 2003 09:38:21 -0400

Hi! I am a beginner of JAXB too. I have tried changing your program and got results. I am printing the program changes and corresponding output below. Hope this helps you. (Bold text in the program indicates changes)1) First I made the changes as below./////////////////////////////////////JAVA PROGRAMimport java.io.FileInputStream;import java.io.FileOutputStream;import java.io.File;import java.io.IOException;import java.util.Iterator;import java.util.List;import java.math.*;import javax.xml.bind.JAXBContext;import javax.xml.bind.JAXBException;import javax.xml.bind.Unmarshaller;import javax.xml.bind.Marshaller;import test.jaxb.*;public class New{public static void main( String[] args ){try{JAXBContext jc = JAXBContext.newInstance( "test.jaxb" );ObjectFactory objFactory = new ObjectFactory();Marshaller mar = jc.createMarshaller();mar.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT ,new Boolean(true));CollectionType ctype = objFactory.createCollectionType();Collection cc = objFacto
 ry.createCollection();List lst1 = ctype.getChoice();ChoiceType chtype = objFactory.createChoiceType();Choice ch1 = objFactory.createChoice();ch1.setSnumber(1);ch1.setAnswerchoice("Old");lst1.add(ch1);mar.marshal(ctype, System.out);}catch( JAXBException je ){je.printStackTrace();}catch( Exception ioe ){ioe.printStackTrace();}}}OUTPUT:&lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?&gt;&lt;Choice snumber="1"&gt;&lt;answerchoice&gt;Old&lt;/answerchoice&gt;&lt;/Choice&gt;2) Then, I have tried this./////////////////////////////////////JAVA PROGRAMimport java.io.FileInputStream;import java.io.FileOutputStream;import java.io.File;import java.io.IOException;import java.util.Iterator;import java.util.List;import java.math.*;import javax.xml.bind.JAXBContext;import javax.xml.bind.JAXBException;import javax.xml.bind.Unmarshaller;import javax.xml.bind.Marshaller;import test.jaxb.*;public class New{public static void main( String[] args ){try{JAXBContext jc = JAXBContext.newInst
 ance( "test.jaxb" );ObjectFactory objFactory = new ObjectFactory();Marshaller mar = jc.createMarshaller();mar.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT ,new Boolean(true));CollectionType ctype = objFactory.createCollectionType();Collection cc = objFactory.createCollection();List lst1 = cc.getChoice();ChoiceType chtype = objFactory.createChoiceType();Choice ch1 = objFactory.createChoice();ch1.setSnumber(1);ch1.setAnswerchoice("Old");lst1.add(ch1);mar.marshal(cc, System.out);}catch( JAXBException je ){je.printStackTrace();}catch( Exception ioe ){ioe.printStackTrace();}}}OUTPUT:&lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?&gt;&lt;Collection&gt;&lt;Choice snumber="1"&gt;&lt;answerchoice&gt;Old&lt;/answerchoice&gt;&lt;/Choice&gt;&lt;/Collection&gt;3) But, still this does not display the schema location. Added a property to marshaller.
import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.File;import java.io.IOException;import java.util.Iterator;import java.util.List;import java.math.*;
import javax.xml.bind.JAXBContext;import javax.xml.bind.JAXBException;import javax.xml.bind.Unmarshaller;import javax.xml.bind.Marshaller;
import test.jaxb.*;
public class New{public static void main( String[] args ){try{JAXBContext jc = JAXBContext.newInstance( "test.jaxb" );ObjectFactory objFactory = new ObjectFactory();
Marshaller mar = jc.createMarshaller();mar.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT ,new Boolean(true));
CollectionType ctype = objFactory.createCollectionType();Collection cc = objFactory.createCollection();
List lst1 = cc.getChoice();
ChoiceType chtype = objFactory.createChoiceType();Choice ch1 = objFactory.createChoice();ch1.setSnumber(1);ch1.setAnswerchoice("Old");
lst1.add(ch1);mar.setProperty(Marshaller.JAXB_NO_NAMESPACE_SCHEMA_LOCATION , "c:jaxb1.0binTest.xsd");mar.marshal(cc, System.out);
}catch( JAXBException je ){je.printStackTrace();}catch( Exception ioe ){ioe.printStackTrace();}}}
OUTPUT:
&lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?&gt;&lt;Collection xsi:noNamespaceSchemaLocation="c:\jaxb1.0\bin\Test.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&gt;&lt;Choice snumber="1"&gt;&lt;answerchoice&gt;Old&lt;/answerchoice&gt;&lt;/Choice&gt;&lt;/Collection&gt;--- On Sun 06/08, Lakhamraju, Sarada (UMC-Student) &lt; slh3m@mizzou.edu &gt; wrote:
From: Lakhamraju, Sarada (UMC-Student) [mailto: slh3m_at_mizzou.edu]To: JAXB-INTEREST_at_JAVA.SUN.COMDate: Sun, 8 Jun 2003 19:03:40 -0500Subject: Problem generating a XML document......help...Hello All, I have attached the Schema which I am using. I am trying to create a content tree by using the following java code below. Before creating the content tree, I have parsed the XML Schema and got the JAXB classes. I have to genetate the following content tree, but my program below doesnot do that. Please check this piece of code and tell me where I am making a mistake? Is the Schema wrong or the Java Program I have written is wrong. Please help me out.Thanks,Sarada.String/////////////////////////////////////JAVA PROGRAMimport java.io.FileInputStream;import java.io.FileOutputStream;import java.io.File;import java.io.IOException;import java.util.Iterator;import java.util.List;import java.math.*;import javax.xml.bind.JAXBContext;import javax.xml.bind.JAXBException;import javax.xml.bind.Un
 marshaller;import javax.xml.bind.Marshaller;import test.jaxb.*;public class New{public static void main( String[] args ){try{JAXBContext jc = JAXBContext.newInstance( "test.jaxb" );ObjectFactory objFactory = new ObjectFactory();Marshaller mar = jc.createMarshaller();mar.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT ,new Boolean(true));CollectionType ctype = objFactory.createCollectionType();Collection cc = objFactory.createCollection();List lst1 = ctype.getChoice();ChoiceType chtype = objFactory.createChoiceType();Choice ch1 = objFactory.createChoice();chtype.setSnumber(1);chtype.setAnswerchoice("Old");lst1.add(chtype);mar.marshal(ctype, System.out);}catch( JAXBException je ){je.printStackTrace();}catch( Exception ioe ){ioe.printStackTrace();}}}Attachment: Test.xsd (0.89KB)



_______________________________________________
No banners. No pop-ups. No kidding.
Introducing My Way - http://www.myway.com