jaxb - Using SAX with JAXBContext -


i trying deserialize xml data newly created java content trees:

i using sax, have java class under src\main\java\summaries.java , trying print document extracted:

string xmlpath = "c:\\workspace-sts-2.8.0.release\\restclient\\src\\main\\resources\\dailysummary.xml"; string xmlpath2 = "/dailysummary.xml"; string xmlpath3 = "/src/main/resources/dailysummary.xml";  inputstream inputstream = null; inputsource inputsource = null; unmarshaller unmarshaller = null; jaxbcontext jc = null; try  {  // read file // or try xmlpath1 or try xmlpath3 inputstream = resttestclient.class.getclassloader().getresourceasstream(xmlpath2);  inputsource = new inputsource(inputstream); saxparserfactory sax = saxparserfactory.newinstance(); sax.setnamespaceaware(true); xmlreader reader = sax.newsaxparser().getxmlreader(); saxsource saxsource= new saxsource(reader, inputsource);  // use jaxb class[] classes = new class[1]; classes[0]= summaries.class; jc = jaxbcontext.newinstance(classes); unmarshaller = jc.createunmarshaller();  @suppresswarnings("unchecked") **jaxbelement<object> doc = (jaxbelement<object>) unmarshaller.unmarshal(saxsource);**  // errors out here sysout(doc); } 

i error :

javax.xml.bind.unmarshalexception  - linked exception:  [org.xml.sax.saxparseexception: file "null" not found.]      @ javax.xml.bind.helpers.abstractunmarshallerimpl.createunmarshalexception(abstractunmarshallerimpl.java:315)     @ com.sun.xml.internal.bind.v2.runtime.unmarshaller.unmarshallerimpl.createunmarshalexception(unmarshallerimpl.java:505)      @ com.sun.xml.internal.bind.v2.runtime.unmarshaller.unmarshallerimpl.unmarshal0(unmarshallerimpl.java:206)     @ com.sun.xml.internal.bind.v2.runtime.unmarshaller.unmarshallerimpl.unmarshal(unmarshallerimpl.java:173)      @ javax.xml.bind.helpers.abstractunmarshallerimpl.unmarshal(abstractunmarshallerimpl.java:120)     @ javax.xml.bind.helpers.abstractunmarshallerimpl.unmarshal(abstractunmarshallerimpl.java:103)      @ main.java.resttestclient.marshalljaxb(resttestclient.java:346)     @ main.java.resttestclient.main(resttestclient.java:82)  caused by: org.xml.sax.saxparseexception: file "null" not found.      @ org.apache.xerces.framework.xmlparser.reporterror(xmlparser.java:1156)     @ org.apache.xerces.readers.defaultentityhandler.startreadingfromdocument(defaultentityhandler.java:499)      @ org.apache.xerces.framework.xmlparser.parsesomesetup(xmlparser.java:310)     @ org.apache.xerces.framework.xmlparser.parse(xmlparser.java:1034)      @ com.sun.xml.internal.bind.v2.runtime.unmarshaller.unmarshallerimpl.unmarshal0(unmarshallerimpl.java:200) 

any ideas might point me in right direction ?

you try following approach using jaxb's unmarshallerhandler:

package forum10890323;  import java.io.inputstream; import javax.xml.bind.*; import javax.xml.parsers.*; import org.xml.sax.*;  public class demo {      public static void main(string[] args) throws exception {         jaxbcontext jc = jaxbcontext.newinstance(root.class);         unmarshallerhandler unmarshallerhandler = jc.createunmarshaller().getunmarshallerhandler();          saxparserfactory spf = saxparserfactory.newinstance();         saxparser sp = spf.newsaxparser();         xmlreader xr = sp.getxmlreader();         xr.setcontenthandler(unmarshallerhandler);          inputstream inputstream = root.class.getclassloader().getresourceasstream("forum10890323/input.xml");         inputsource inputsource = new inputsource(inputstream);         xr.parse(inputsource);         inputstream.close();          root root = (root) unmarshallerhandler.getresult();          marshaller marshaller = jc.createmarshaller();         marshaller.setproperty(marshaller.jaxb_formatted_output, true);         marshaller.marshal(root, system.out);     }  } 

Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -