List of usage examples for javax.xml.parsers DocumentBuilder parse
public abstract Document parse(InputSource is) throws SAXException, IOException;
From source file:Main.java
/** * Loads the xml file into an xml document and returns the root element. * /* w w w . j a v a 2 s. co m*/ * @param fileName * the fully qualified name of the XML file to load; assumed not * to be <code>null</code>. * * @return root element of the xml document, never <code>null</code>. * * @throws Exception * on any error */ public static Element loadXmlResource(final String fileName) throws Exception { File file = new File(fileName); DocumentBuilder db = getDocumentBuilder(); Document doc = db.parse(file); return doc.getDocumentElement(); }
From source file:Main.java
public static Document readDocument(InputStream is) { try {//from w w w.ja v a2 s. com final DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); final DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder(); final Document doc = docBuilder.parse(is); doc.getDocumentElement().normalize(); return doc; } catch (Exception e) { throw new RuntimeException(e); } }
From source file:Main.java
/** * Parses the given file into an XML {@link Document}. * @param file The file to parse.// w ww . j ava 2 s.com * @return The created XML {@link Document}. */ public static Document parseXml(File file) { if (!file.exists()) { throw new IllegalArgumentException("File " + file + " does not exist."); } try { DocumentBuilder docBuilder = createDocumentBuilder(); return docBuilder.parse(file); } catch (SAXException | IOException e) { throw new IllegalStateException("Unable to parse XML file " + file, e); } }
From source file:Main.java
public static Document parseXMLStream(InputStream stream) { Document doc = null;/* w w w .j a v a 2 s .c o m*/ try { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = dbf.newDocumentBuilder(); doc = docBuilder.parse(stream); } catch (ParserConfigurationException e) { e.printStackTrace(); } catch (SAXException e) { e.printStackTrace(); } catch (java.io.IOException e) { e.printStackTrace(); } return doc; }
From source file:Main.java
public static Object getClassFromXML() throws Exception { //create DOM object DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder(); Document document = docBuilder.parse(new File("property.xml")); //get the node name and get the class name NodeList nodeList = document.getElementsByTagName("className"); Node node = nodeList.item(0).getFirstChild(); String className = node.getNodeValue(); //return an object through the class name Class tempClass = Class.forName(className); Object object = tempClass.newInstance(); return object; }
From source file:Main.java
public static String getDefaultNamespaceUri(URL xmlResource, String rootElementName) throws IOException, ParserConfigurationException, SAXException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(false);/*from w w w .j a v a 2s . c o m*/ DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse(xmlResource.openStream()); NodeList nodes = document.getElementsByTagName(rootElementName); if (nodes == null || nodes.getLength() == 0) { throw new IllegalArgumentException("Root element \"" + rootElementName + "\" not found in xml \"" + xmlResource.toExternalForm() + "\"."); } for (int i = 0; i < nodes.getLength(); i++) { Node node = nodes.item(i); Node xmlns = node.getAttributes().getNamedItem("xmlns"); if (xmlns != null) { String value = xmlns.getNodeValue(); return value.substring(value.indexOf("=") + 1); } } return null; }
From source file:Main.java
/** * Loads a W3C XML document from a file. * //from ww w. j a v a2s.c om * @param filename * The name of the file to be loaded * @return a document object model object representing the XML file * @throws IOException * @throws ParserConfigurationException * @throws SAXException */ public static Document loadXML(String filename) throws IOException, ParserConfigurationException, SAXException { DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); return builder.parse(new File(filename)); }
From source file:Main.java
public static NodeList getDomNodes(String file, String xmlTag) { NodeList nodes = null;/*from ww w . j a v a 2s . c om*/ try { File xml = new File(file); DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); Document doc = dBuilder.parse(xml); doc.getDocumentElement().normalize(); nodes = doc.getElementsByTagName(xmlTag); } catch (Exception e) { e.printStackTrace(); nodes = null; } logger.trace("Returning {} nodes", nodes.getLength()); return nodes; }
From source file:Main.java
public static Document parseXMLFile(String fileName) { Document doc = null;/*from w w w .j a v a2s . c o m*/ try { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = dbf.newDocumentBuilder(); doc = docBuilder.parse(new File(fileName)); } catch (ParserConfigurationException e) { e.printStackTrace(); } catch (SAXException e) { e.printStackTrace(); } catch (java.io.IOException e) { e.printStackTrace(); } return doc; }
From source file:Main.java
public static Document parseToDocument(InputStream is, boolean debugModeEnabled) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse(is); if (debugModeEnabled) System.out.println("Body:\n" + serializeDocument(document) + "\n"); return document; }