Click to See Complete Forum and Search --> : Loading org.w3c.dom.Document


jsiii
February 27th, 2006, 07:38 AM
Hello, I am bit new to JAVA. I am using org.w3c.dom.* to parse XML database. The XML document is stored in Document class and my question is pretty simple:

How do I load this Document from regular text file?

Thank you for any help ;-)

jompe71
February 28th, 2006, 02:51 AM
Since nobody posted a reply, I pasted together some code snippets of mine into a class that MIGHT help you out. It uses JDOM as the XML Document wrapper and SAX as parser. At the bottom there is a method for retrieving the W3C Xml Document interface from a JDOM Xml Doc instance.

import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import org.jdom.output.DOMOutputter;
import org.jdom.output.XMLOutputter;
import org.jdom.transform.JDOMSource;

public class XmlUtils
{
private XmlUtils()
{}

/**
* Loads an XML from file.
*
* @param xml
* @return
*/
public static org.jdom.Document loadXmlFromFile( File xml )
{
SAXBuilder saxBuilder = new SAXBuilder();
org.jdom.Document xmlDoc = null;

try
{
xmlDoc = saxBuilder.build( xml );
}
catch( Exception e )
{
System.out.println( "Error loading XML file into JDOM Document: " + e.getMessage() );
}

return xmlDoc;
}

/**
* Converts XML in String format to JDOM Doc.
*
* @param xml
* @return
*/
public static org.jdom.Document convertFromString( String xml )
{
SAXBuilder saxBuilder = new SAXBuilder();
org.jdom.Document xmlDoc = null;

try
{
xmlDoc = saxBuilder.build( new StringReader( xml ) );
}
catch( Exception e )
{
System.out.println( "Error converting String XML to JDOM Document: " + e.getMessage() );
}

return xmlDoc;
}

/**
* JDOM Doc to String.
*
* @param xmlDoc
* @return
*/
public static String convertToString( org.jdom.Document xmlDoc )
{
XMLOutputter xmlOut = new XMLOutputter();
StringWriter sw = new StringWriter();

try
{
xmlOut.setIndent( "\t" );
xmlOut.output( xmlDoc, sw );
sw.flush();
}
catch( Exception e )
{
System.out.println( "Error converting JDOM Document to String: " + e.getMessage() );
}
finally
{
try{ sw.close(); } catch( Exception e ){}
}
return sw.getBuffer().toString();
}

/**
* Converts the JDOM XML Doc into the W3C interface.
*
* @param xmlDoc
* @return
*/
public static org.w3c.Document jdomToW3C( org.jdom.Document xmlDoc )
{
// JDOM -> W3C.
DOMOutputter domOut = new DOMOutputter();
return = domOut.output( xmlDoc );
}
}

jsiii
February 28th, 2006, 03:41 AM
Thank you, it looks nice and it seemed to be a way to go. Unfortunately I don't have org.jdom package for some reason (I am using NetBeans 5.0).

I however found this. I will give it a try.


/**
* Converts String containing the XML representation to Document.
*
* @param xmlString String that contains XML representation.
* @return Document loaded from String
*/
public Document convertXmlToDocument(String xmlString) {
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder parser = factory.newDocumentBuilder();
Document document = parser.parse(xmlString);
return document;
}
catch (Exception ex) {
ex.printStackTrace();
return null;
}
}