Re: Difficulties with Java
Consider creating your own utility/helper classes.
Then outputing an xml file to the console could *simply* be:
// DomTest.java
import java.io.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;
public class DomTest
{
public static void main(String[] args)
throws Exception
{
DocumentBuilder db =
DocumentBuilderFactory.newInstance().newDocumentBuilder();
InputStream is = new FileInputStream("test.xml");
Document doc = db.parse(is);
is.close();
System.out.println(DomUtil.toString(doc));
}
}
This is the utility class:
// DomUtil.java
import java.io.*;
import javax.xml.transform.*;
import javax.xml.transform.stream.*;
import javax.xml.transform.dom.*;
import org.w3c.dom.*;
public class DomUtil
{
public static String toString(Document doc)
throws Exception
{
StringWriter sw = new StringWriter();
TransformerFactory tf = TransformerFactory.newInstance();
Transformer t = tf.newTransformer();
t.setOutputProperty(OutputKeys.METHOD, "xml");
if (doc.getXmlEncoding() != null)
t.setOutputProperty(OutputKeys.ENCODING,
doc.getXmlEncoding());
t.setOutputProperty(OutputKeys.INDENT, "yes");
t.transform(new DOMSource(doc), new StreamResult(sw));
return sw.toString();
}
}
Regards