List of usage examples for javax.xml.parsers DocumentBuilder parse
public abstract Document parse(InputSource is) throws SAXException, IOException;
From source file:Main.java
public static Document documentify(InputStream in) throws ParserConfigurationException, SAXException, IOException { DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder(); return docBuilder.parse(in); }
From source file:Main.java
/** * Return he dom root element of an xml file * @param filePathInClassPath the file path relative to the classpath * @return the root element//from ww w . j a v a2 s . c o m * @throws ParserConfigurationException * @throws IOException * @throws SAXException */ public static Element getRootElementFromFileInClasspath(String filePathInClassPath) throws ParserConfigurationException, IOException, SAXException { DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = docFactory.newDocumentBuilder(); // root elements Document doc = docBuilder.parse(new ClassPathResource(filePathInClassPath).getInputStream()); return doc.getDocumentElement(); }
From source file:Main.java
public static Document loadXMLFromString(String theXML_URL) { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db; try {/*from w ww . ja v a2 s . c o m*/ db = dbf.newDocumentBuilder(); Document doc = db.parse(new URL(theXML_URL).openStream()); return doc; } catch (ParserConfigurationException e) { return null; } catch (MalformedURLException e) { return null; } catch (SAXException e) { return null; } catch (IOException e) { return null; } }
From source file:Main.java
public static void getXMLContent(String filePath) { try {//from ww w . java 2 s .c o m File f = new File(filePath); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); org.w3c.dom.Document doc = builder.parse(f); NodeList nodes_1 = doc.getChildNodes(); for (int i = 0; i < nodes_1.getLength(); i++) { org.w3c.dom.Node nodes_2 = nodes_1.item(i); NodeList nodes_3 = nodes_2.getChildNodes(); for (int j = 0; j < nodes_3.getLength(); j++) { org.w3c.dom.Node node = nodes_3.item(j); NodeList xmlMeta = node.getChildNodes(); for (int k = 0; k < xmlMeta.getLength(); k++) { // value = xmlMeta.item(k).getNodeValue(); System.out.println(xmlMeta.item(k).getNodeName() + ":" + xmlMeta.item(k).getNodeValue()); } } } } catch (Exception e) { e.printStackTrace(); } }
From source file:Main.java
/** * Load the given {@link InputStream} as an XML document. * * @param in/* ww w .j av a2s . c o m*/ * @return the loaded XML document * @throws ParserConfigurationException * @throws IOException * @throws SAXException */ @Nonnull public static Document loadXmlDocumentFromStream(@Nonnull InputStream in) throws ParserConfigurationException, IOException, SAXException { Preconditions.checkNotNull(in, "Inputstream can not be null"); DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); return documentBuilder.parse(in); }
From source file:Main.java
/** * Method to create a document based on file path. * /*from ww w . j a va 2 s.co m*/ * @param path xml file path * @return Document for given xml file * @throws ParserConfigurationException * @throws SAXException * @throws IOException */ public static Document createDocumentPath(String path) throws ParserConfigurationException, SAXException, IOException { File fXmlFile = new File(path); DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); Document doc = dBuilder.parse(fXmlFile); return doc; }
From source file:Main.java
public static Object getBean(String relativePath, String nodeName) { try {/* w w w . j a v a 2s . com*/ DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = dFactory.newDocumentBuilder(); Document doc; doc = builder.parse(new File(path + relativePath)); NodeList nl = doc.getElementsByTagName(nodeName); Node classNode = nl.item(0).getFirstChild(); String className = classNode.getNodeValue().trim(); Class clazz = Class.forName(className); Object obj = clazz.newInstance(); return obj; } catch (Exception e) { e.printStackTrace(); return null; } }
From source file:Main.java
/** * Parse a DOM Document from the given XML file. * // w w w . j a v a2 s.c o m * @param f File to parse as Document * @return Document * @throws IOException */ public static Document getDocument(File f) throws IOException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); // never forget this! try { DocumentBuilder builder = factory.newDocumentBuilder(); return builder.parse(f); } catch (ParserConfigurationException e) { IOException ioe = new IOException(); ioe.initCause(e); throw ioe; } catch (SAXException e) { IOException ioe = new IOException(); ioe.initCause(e); throw ioe; } }
From source file:Main.java
public static String toNormalizedXML(InputStream is) throws ParserConfigurationException, SAXException, IOException { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true);/*from w w w.j a v a 2 s . c om*/ dbf.setCoalescing(true); dbf.setIgnoringElementContentWhitespace(true); dbf.setIgnoringComments(true); DocumentBuilder db = dbf.newDocumentBuilder(); Document document = db.parse(is); document.normalizeDocument(); document.getDocumentElement().setAttributeNS("http://www.w3.org/2001/XMLSchema-instance", //$NON-NLS-1$ "xsi:schemaLocation", //$NON-NLS-1$ "http://abc4trust.eu/wp2/abcschemav1.0 ../../../../../../../../../abc4trust-xml/src/main/resources/xsd/schema.xsd"); //$NON-NLS-1$ DOMImplementationLS domImplLS = (DOMImplementationLS) document.getImplementation(); LSSerializer serializer = domImplLS.createLSSerializer(); String xml = serializer.writeToString(document); return trim(xml); }
From source file:Main.java
public static Document parse(File iFile) throws SAXException, IOException, ParserConfigurationException { DocumentBuilder builder = javax.xml.parsers.DocumentBuilderFactory.newInstance().newDocumentBuilder(); return builder.parse(iFile); }