Java tutorial
//package com.java2s; /* * The following methods come from a library written by Tom Fennelly. * Here was the header of the licence. */ import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.StringReader; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import org.w3c.dom.*; import org.xml.sax.InputSource; import org.xml.sax.SAXException; public class Main { /** * Return a Node corresponding to the input XML string representation. * @param xmlString XML string representation. * @return An instance of Node corresponding to the input XML string representation. * @throws ParserConfigurationException * @throws SAXException * @throws IOException */ public static Node getXMLNode(String xmlString) throws ParserConfigurationException, SAXException, IOException { InputSource inputSource = new InputSource(new StringReader(xmlString)); return (getXMLNode(inputSource)); } /** * Return a Node corresponding to the input XML string representation. * @param xmlFile XML file. * @return An instance of Node corresponding to the input XML string representation. * @throws ParserConfigurationException * @throws SAXException * @throws IOException */ public static Node getXMLNode(File xmlFile) throws ParserConfigurationException, SAXException, IOException { InputSource inputSource = new InputSource(new BufferedInputStream(new FileInputStream(xmlFile))); return (getXMLNode(inputSource)); } /** * Return a Node corresponding to the input XML string representation. * @param inputSource source. * @return An instance of Node corresponding to the input XML string representation. * @throws ParserConfigurationException * @throws SAXException * @throws IOException */ public static Node getXMLNode(InputSource inputSource) throws ParserConfigurationException, SAXException, IOException { DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); docFactory.setNamespaceAware(true); DocumentBuilder docBuilder = docFactory.newDocumentBuilder(); Document doc = docBuilder.parse(inputSource); return (doc); } }