I have to build an XML document from pieces of existing ones, and then output it as a string. What libraries should I use?

Pramod Hirole

If you have pieces of existing valid documents as nodes then you can import those into the document you want to build by appending those nodes as child elements to the document.

The following code will do this:

To write document object as a string you need to serialize it. You can use the Apache Xerces parser available from Apache. You should use XMLSerializer class to serialise document as String. Here is code sniplet.
// import following package
import org.apache.xml.serialize.*;

// following code serializes
// document to domstr String

OutputFormat of = 
       new OutputFormat("XML","UTF-8",true);
XMLSerializer serializer = 
                       new XMLSerializer();
ByteArrayOutputStream baos = 
                  new ByteArrayOutputstream()
String domstr = 
             new String(baos.toByteArray())

// domstr contains document as a String
0 Comments  (click to add your comment)
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



About | Sitemap | Contact