title: Convert Java object to XML string
body: <p>Yes, yes I know that lots of questions were asked about this topic. But I still cannot find the solution to my problem. I have a property annotated Java object. For example Customer, <a href="http://www.mkyong.com/java/jaxb-hello-world-example/" rel="noreferrer">like in this example</a>. And I want a String representation of it. Google reccomends using JAXB for such purposes. But in all examples created XML file is printed to file or console, like this:</p>
  
  <pre><code>File file = new File("C:\\file.xml");
  JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class);
  Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
  
  // output pretty printed
  jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
  
  jaxbMarshaller.marshal(customer, file);
  jaxbMarshaller.marshal(customer, System.out);
  </code></pre>
  
  <p>But I have to use this object and send over network in XML format. So I want to get a String which represents XML.</p>
  
  <pre><code>String xmlString = ...
  sendOverNetwork(xmlString);
  </code></pre>
  
  <p>How can I do this?</p>