You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jaxme-dev@ws.apache.org by Vi...@aol.com on 2004/07/08 05:16:59 UTC

About Unmarshalling

 
Hello,
 
I am a newbee to JAXME and I need assistance on the unmarshalling routine.  I 
have encountered a dead-end with regards to unmarshalling an XML String to an 
 Object. Here's the scenario...
 
This is the XML stream, which I've stored in a String variable as  such:
 
   String subXML = "<?xml  version=\"1.0\" encoding=\"UTF-8\"?>\n"  +
"<mstns:Data Id=\"184AAA2Y364\"  
xmlns:mstns=\"_http://tempuri.org/Data.xsd\">\n_ (http://tempuri.org/Data.xsd/">/n) "  +
" <mstns:Id1>09854029</mstns:Id1>\n"  +
" <mstns:Id2>7592934</mstns:Id2>\n"  +
" <mstns:Id3>3775234</mstns:Id3>\n"  +
" <mstns:Id4>9472432</mstns:Id4>\n"  +
" <mstns:Name1>Sample1</mstns:Name1>\n"  +
" <mstns:Name2>Sample2</mstns:Name2>\n"  +
" <mstns:Name3>TestRun</mstns:Name3>\n"  +
" <mstns:Date>1976-10-27</mstns:Date>\n"  +
" <mstns:DateTime>1973-12-07T00:00:000Z</mstns:DateTime>\n"  +
" <mstns:Type>Task</mstns:Type>\n"  +
" <mstns:Subject>String</mstns:Subject>\n"  +
"</mstns:Data>\n";


This is a code snippet I've used for unmarshalling (the commented-lines are  
tried out statements to produce a "source" for unmarshalling and parsing);  
unfortunately, it still produces the same exception:
 
   javax.xml.transform.stream.StreamSource  xmlSource = new 
javax.xml.transform.stream.StreamSource(new  StringReader(subXML));
//java.io.ByteArrayInputStream xmlSource = new  
java.io.ByteArrayInputStream(subXML.getBytes());  
//org.xml.sax.InputSource xmlSource =  new org.xml.sax.InputSource(new  
StringReader(subXML));
org.tempuri.data_xsd.Data  data;
JAXBContext jaxCtx =  JAXBContext.newInstance("org.tempuri.data_xsd");
Unmarshaller  unmarsh = jaxCtx.createUnmarshaller();
data =  (org.tempuri.data_xsd.Data)unmarsh.unmarshal(xmlSource);

 
 
The following is a section of the error I'm getting.
 
15:31:39,468 INFO   [STDOUT] javax.xml.bind.UnmarshalException
15:31:39,468 INFO   [STDOUT]     at  org.apache.ws.jaxme.impl.JMUnmarshallerIm
pl.unmarshal(JMUnmarshallerImpl.java:135)
15:31:39,468  INFO  [STDOUT]     at  
org.apache.ws.jaxme.impl.JMUnmarshallerImpl.unmarshal(JMUnmarshallerImpl.java:202)
 
 
I've traced the throw from here:
 
  public Object  unmarshal(InputSource pSource) throws JAXBException {
UnmarshallerHandler uh = getUnmarshallerHandler();
try  {
SAXParser sp =  spf.newSAXParser();
XMLReader xr =  sp.getXMLReader();
xr.setContentHandler(uh);
xr.parse(pSource);  <---------------------------- Possible  thrower
} catch (SAXException e)  {
if (e.getException() != null)  {
throw new  UnmarshalException(e.getException());
} else  {
throw new UnmarshalException(e);  <------------ Throw  location
}
}  catch (IOException e) {
throw new  UnmarshalException(e);
} catch  (ParserConfigurationException e) {
throw new  UnmarshalException(e);
}
return  uh.getResult();
}


I have been successful in the "marshalling" process. I hope someone could  
help me out with the unmarshalling part. Is this an XML issue or am I missing  
something else?
 
Best Regards,
Carl De Guzman

Re: About Unmarshalling

Posted by Jochen Wiedmann <jo...@freenet.de>.
Hi, Vicencio,

Vicencio73@aol.com wrote:

>    *javax.xml.transform.stream.StreamSource* xmlSource = new 
> javax.xml.transform.stream.StreamSource(new StringReader(subXML));
>    //java.io.ByteArrayInputStream xmlSource = new 
> java.io.ByteArrayInputStream(subXML.getBytes());
>    //org.xml.sax.InputSource xmlSource = new org.xml.sax.InputSource(new 
> StringReader(subXML));
>    org.tempuri.data_xsd.Data data;
>    JAXBContext jaxCtx = JAXBContext.newInstance("org.tempuri.data_xsd");
>    Unmarshaller unmarsh = jaxCtx.createUnmarshaller();
>    data = (org.tempuri.data_xsd.Data)unmarsh.unmarshal(xmlSource);

I have added a test case for unmarshalling from a StreamSource to the Unit 
test "MarshallerTest", but was unable to reproduce your Exception.

Anyways, the recommended way to unmarshal a String is:

     StringReader sr = new StringReader(s);
     InputSource is = new InputSource(is);
     is.setSystemId("something");  // Optional, only used in case of error
                                   // messages or if your XML document
                                   // uses external entities with
                                   // relative addressing.
     unmarshaller.unmarshal(is);


Jochen

---------------------------------------------------------------------
To unsubscribe, e-mail: jaxme-dev-unsubscribe@ws.apache.org
For additional commands, e-mail: jaxme-dev-help@ws.apache.org