Here I am pasing how to get the doc from the xml file.
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
/**
* A utility class for loading and processing XML documents.
*/
public final class XMLUtilsOfMine {
/**
* Builds a DOM
Document
. If abs is true, the path provided* is assumed to be absolute. Otherwise the file is loaded using the
*
ClassLoader
, so the file should be located in classpath.*
* @param file name of the file to load
* @param abs true if file path is absolute
* @return the XML document,
null
if not found.* @throws
XMLProcessingException
if any error occurred* while loading/parsing the document.
* @throws
DOMException
*/
public static Document loadDocument(String file, boolean abs)
throws XMLProcessingException {
if (!abs) {
return loadDocument(file);
}
InputStream stream = null;
try {
stream = new FileInputStream(file);
} catch (FileNotFoundException e1) {
throw new XMLProcessingException("Error occurred while reading:" + file,
e1);
}
if (stream == null) {
return null;
}
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
try {
return factory.newDocumentBuilder().parse(stream);
} catch (ParserConfigurationException e) {
/** @todo log message */
throw new XMLProcessingException(
"Error occurred while creating DocumentBuilder", e);
} catch (SAXException e) {
throw new XMLProcessingException("Error occurred while parsing " + file, e);
} catch (IOException e) {
throw new XMLProcessingException("Error occurred while reading " + file, e);
}
}
}
No comments:
Post a Comment