Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

new to XML and am very stuck!

Status
Not open for further replies.

hotpot

Technical User
Mar 25, 2009
1
US
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(&quot;Problem creating DMML document&quot;);
}

// step 2 - add nodes
Element dmml= doc.createElement(&quot;DMML&quot;);
dmml.setAttribute (&quot;version&quot;, &quot;1.0&quot; );
dmml.setAttribute (&quot;encoding&quot;, &quot;UTF-8&quot; );
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(&quot;method&quot;, &quot;xml&quot;);
props.put(&quot;indent-amount&quot;, &quot;2&quot;);
serializer.setOutputProperties(props);
serializer.transform(new DOMSource(d), new StreamResult(output));

String s = output.toString(&quot;UTF-8&quot;);
return s;
}
catch(Exception e)
{
e.printStackTrace();
}

return null;
}
}




 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top