Hi all,
Im new to all this XML stuff
below I have some code I am just starting to write and I want to view the output as I build it.so I have a function documentToString that converts the doc to a String . However when I run it I dont get any output- just a white box appears.
I would be grateful for any feedback
also another question I have is how does one input data between the 2 elements
<element> how do I put data in here <\element>
thanks
import java.util.Enumeration;
import java.util.*;
import java.net.*;
import java.io.*;
import java.rmi.RemoteException;
import java.util.*;
import javax.naming.NamingException;
import com.kensingtonspace.api.*;
import com.kensingtonspace.naming.InitialContext;
import javax.swing.*;
import java.awt.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.sax.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;
import org.w3c.dom.*;
import org.xml.sax.*;
public final class ToXML
{
public static void genDMML(Vector v)
{
// step 1 - create empty document
Document doc= null;
try
{
DocumentBuilderFactory factory= DocumentBuilderFactory.newInstance();
factory.setValidating(false);
factory.setNamespaceAware(false);
DocumentBuilder db= factory.newDocumentBuilder();
doc= db.newDocument();
}
catch( Exception e )
{
System.err.println("Problem creating DMML document"
}
// step 2 - add nodes
Element dmml= doc.createElement("DMML"
dmml.setAttribute ("version", "1.0" );
dmml.setAttribute ("encoding", "UTF-8" );
doc.appendChild(dmml);
String dmml_string=null;
dmml_string = documentToString(doc);
System.out.println(dmml_string);
}
/**
* Method for transforming an XML document object to a string.
*
* @param d document to transform into a string
* @return string representation of d
*/
public static String documentToString(Document d)
{
try
{
ByteArrayOutputStream output= new ByteArrayOutputStream();
// This creates a transformer that does a simple identity transform,
// and thus can be used for all intents and purposes as a serializer.
TransformerFactory tfactory = TransformerFactory.newInstance();
Transformer serializer = tfactory.newTransformer();
Properties props = new Properties();
props.put("method", "xml"
props.put("indent-amount", "2"
serializer.setOutputProperties(props);
serializer.transform(new DOMSource(d), new StreamResult(output));
String s = output.toString("UTF-8"
return s;
}
catch(Exception e)
{
e.printStackTrace();
}
return null;
}
}
Im new to all this XML stuff
below I have some code I am just starting to write and I want to view the output as I build it.so I have a function documentToString that converts the doc to a String . However when I run it I dont get any output- just a white box appears.
I would be grateful for any feedback
also another question I have is how does one input data between the 2 elements
<element> how do I put data in here <\element>
thanks
import java.util.Enumeration;
import java.util.*;
import java.net.*;
import java.io.*;
import java.rmi.RemoteException;
import java.util.*;
import javax.naming.NamingException;
import com.kensingtonspace.api.*;
import com.kensingtonspace.naming.InitialContext;
import javax.swing.*;
import java.awt.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.sax.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;
import org.w3c.dom.*;
import org.xml.sax.*;
public final class ToXML
{
public static void genDMML(Vector v)
{
// step 1 - create empty document
Document doc= null;
try
{
DocumentBuilderFactory factory= DocumentBuilderFactory.newInstance();
factory.setValidating(false);
factory.setNamespaceAware(false);
DocumentBuilder db= factory.newDocumentBuilder();
doc= db.newDocument();
}
catch( Exception e )
{
System.err.println("Problem creating DMML document"
}
// step 2 - add nodes
Element dmml= doc.createElement("DMML"
dmml.setAttribute ("version", "1.0" );
dmml.setAttribute ("encoding", "UTF-8" );
doc.appendChild(dmml);
String dmml_string=null;
dmml_string = documentToString(doc);
System.out.println(dmml_string);
}
/**
* Method for transforming an XML document object to a string.
*
* @param d document to transform into a string
* @return string representation of d
*/
public static String documentToString(Document d)
{
try
{
ByteArrayOutputStream output= new ByteArrayOutputStream();
// This creates a transformer that does a simple identity transform,
// and thus can be used for all intents and purposes as a serializer.
TransformerFactory tfactory = TransformerFactory.newInstance();
Transformer serializer = tfactory.newTransformer();
Properties props = new Properties();
props.put("method", "xml"
props.put("indent-amount", "2"
serializer.setOutputProperties(props);
serializer.transform(new DOMSource(d), new StreamResult(output));
String s = output.toString("UTF-8"
return s;
}
catch(Exception e)
{
e.printStackTrace();
}
return null;
}
}