Update of /cvsroot/jaxme/JaxMe2/docs
In directory sc8-pr-cvs1:/tmp/cvs-serv2338

Added Files:
        RunTestApp.html 
Log Message:
Initial version

--- NEW FILE: RunTestApp.html ---
<!DOCTYPE doctype PUBLIC "-//w3c//dtd html 4.0 transitional//en">
<html>
<head>
                
  <meta http-equiv="Content-Type"
 content="text/html; charset=iso-8859-1">
                
  <meta name="Author" content="Thalia Rubio">
  <title>Running test applications</title>
                             
  <style type="text/css">@import url("style.css")</style>
</head>
  <body bgcolor="#ffffff">
        
<h1>Running test applications</h1>
       
<p>After you've <a href="Building.html">downloaded JaxM</a>e and <a
 href="GenerateJava.html">generated the Java code</a>, you're ready to run
your test applications. <br>
</p>
<p>In this document,  you'll see how to do the following: </p>
      
<ol>
          <li> <a href="#4.%20%20Reading%20XML%20data%20into%20Java">Run
a test  application to convert XML data into Java objects</a></li>
     <li> <a href="#5.%20Writing%20XML%20data%20from%20Java">Run another
test  application to convert Java objects into XML data</a></li>
       
</ol>
                 
<h2> <a name="4.  Reading XML data into Java"></a>1.&nbsp; Reading XML data
  into Java objects</h2>
       
<p>This section describes how to integrate the generated code into your Java
application, which will read data from an XML document into Java objects.
</p>
     
<p>In this tutorial, we'll use <a
 href="../examples/misc/AddressPrinter.java">AddressPrinter.java</a> as our 
 test application (shown below). This application will take <tt>Address.xml</tt>
  as input, generate Java objects, and print the data. </p>
     
<p>JaxMe imports the Java source code that it had generated (see <a
 href="GenerateJava.html">Generating Java code</a>), instantiates <tt>Address</tt>,
and uses the <tt>Unmarshaller</tt>  for reading elements. </p>
     
<pre>import java.io.File;<br>import javax.xml.bind.JAXBContext;<br>import 
javax.xml.bind.Marshaller;<br>import javax.xml.bind.Unmarshaller;<br><br>import 
net.sf.jaxme.examples.misc.address.Address;<br><br><br>/**<br> * @author &lt;a 
href="mailto:[EMAIL PROTECTED]"&gt;Jochen Wiedmann&lt;/a&gt;<br> * @version $Id: 
RunTestApp.html,v 1.1 2003/02/15 22:17:27 tr7 Exp $<br> */<br>public class 
AddressPrinter {<br>  public static void main(String[] args) throws Exception {<br>    
JAXBContext context = 
JAXBContext.newInstance("net.sf.jaxme.examples.misc.address");<br>    Unmarshaller 
unmarshaller = context.createUnmarshaller();<br>    Address addr = (Address) 
unmarshaller.unmarshal(new File("Address.xml"));<br>    if (addr.getName() == null) 
{<br>      System.out.println("Loaded address without name.");<br>    } else {<br>     
 System.out.println("Loaded address " + addr.getName().getLast() +<br>                 
        ", " + addr.getName().getFirst() + ".");<br>    }<br>    
System.out.println("Details:");<br>    Marshaller marshaller = 
context.createMarshaller();<br>    marshaller.marshal(addr, System.out);<br>  
}<br>}<br></pre>
         
<h2> 2.&nbsp; Writing XML data  from Java objects</h2>
    As our last step in this tutorial, we run the <a
 href="AddressCreator.java">AddressCreator</a> application to create XML data
from Java objects.    
<blockquote>          
  <pre>import java.io.FileWriter;<br>import javax.xml.bind.JAXBContext;<br>import 
javax.xml.bind.Marshaller;<br><br>import 
net.sf.jaxme.examples.misc.address.*;<br><br><br>public class AddressCreator {<br>  
public static void main(String[] args) throws Exception {<br>    // Create the 
element:<br>    Address addr = new AddressImpl();<br>    addr.setName(new 
NameImpl());<br>    addr.getName().setFirst("Jane");<br>    
addr.getName().setLast("Doe");<br>    addr.setPostal(new PostalImpl());<br>    
addr.getPostal().setStreet("34 Main Street");<br>    
addr.getPostal().setCity("Boston");<br>    addr.getPostal().setState("MA");<br>    
addr.getPostal().setZIP("02215");<br>    addr.setEmailDetails(new 
EmailDetailsImpl());<br><br>    Email email = new EmailImpl();<br>    
email.setType("Private");<br>    email.setEmailAddress("[EMAIL PROTECTED]");<br>    
addr.getEmailDetails().addEmail(email);<br>    email = new EmailImpl();<br>    
email.setType("Private");<br>    email.setEmailAddress("[EMAIL PROTECTED]");<br> 
   addr.getEmailDetails().addEmail(email);<br>    email = new EmailImpl();<br>    
email.setType("Private");<br>    
email.setEmailAddress("[EMAIL PROTECTED]");<br>    
addr.getEmailDetails().addEmail(email);<br><br>    addr.setPhoneDetails(new 
PhoneDetailsImpl());<br>    Phone phone = new PhoneImpl();<br>    
phone.setType("Work");<br>    phone.setPhoneNumber("555.6789");<br>    
addr.getPhoneDetails().addPhone(phone);<br><br>    // And save it into the file 
"Address.xml"<br>    JAXBContext context =<br>      
JAXBContext.newInstance("net.sf.jaxme.examples.misc.address");<br>    Marshaller 
marshaller = context.createMarshaller();<br>    FileWriter fw = new 
FileWriter("Address.xml");<br>    marshaller.marshal(addr, fw);<br>    fw.close();<br> 
 }<br>}<br></pre>
    <br>
    <br>
    <a href="RDBMS.html">NEXT</a>&nbsp; <a href="GenerateJava.html">PREVIOUS</a>&nbsp;
    <a href="index.html">TABLE OF CONTENTS</a></blockquote>
     <br>
   <br>
  <br>
 
</body>
</html>




-------------------------------------------------------
This sf.net email is sponsored by:ThinkGeek
Welcome to geek heaven.
http://thinkgeek.com/sf
_______________________________________________
Jaxme-jaxb-dev mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/jaxme-jaxb-dev

Reply via email to