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 parseXmlDocument(String xml, boolean namespaceAware) throws ParserConfigurationException, SAXException, IOException { DocumentBuilder docBuilder = buildDocumentBuilder(namespaceAware); Document doc = docBuilder.parse(new InputSource(new StringReader(xml))); // normalize text representation doc.getDocumentElement().normalize(); return doc;//from w w w . j a v a 2s .c om }
From source file:Main.java
public static Document parseXmlDocument(File file, boolean namespaceAware) throws ParserConfigurationException, SAXException, IOException { DocumentBuilder docBuilder = buildDocumentBuilder(namespaceAware); Document doc = docBuilder.parse(file); // normalize text representation doc.getDocumentElement().normalize(); return doc;//from ww w. ja v a 2s . c o m }
From source file:Main.java
/** * Creates W3C DOM Document object from XML file * @param filePath path to xml file including file name and extension *//*w w w . jav a 2s .co m*/ public static Document parseXml(String filePath) { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); try { DocumentBuilder db = dbf.newDocumentBuilder(); Document doc = db.parse(filePath); doc.getDocumentElement().normalize(); return doc; } catch (ParserConfigurationException pce) { pce.printStackTrace(); return null; } catch (SAXException se) { se.printStackTrace(); return null; } catch (IOException ioe) { //ioe.printStackTrace(); String toRemove = File.separator + "WebApp" + File.separator; Document doc; try { DocumentBuilder db = dbf.newDocumentBuilder(); doc = db.parse(filePath.replace(toRemove, File.separator)); doc.getDocumentElement().normalize(); return doc; } catch (SAXException saxex) { saxex.printStackTrace(); return null; } catch (ParserConfigurationException pcex) { pcex.printStackTrace(); return null; } catch (IOException ioex) { ioex.printStackTrace(); return null; } } }
From source file:Main.java
/** * This will parse an XML stream and create a DOM document. * * @param is The stream to get the XML from. * @return The DOM document.//from ww w . j ava2 s .c o m * @throws IOException It there is an error creating the dom. */ public static Document parse(InputStream is) throws IOException { try { DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = builderFactory.newDocumentBuilder(); return builder.parse(is); } catch (Exception e) { IOException thrown = new IOException(e.getMessage()); throw thrown; } }
From source file:Main.java
/** * This will parse an InputSource and create a DOM document. * * @param is The stream to get the XML from. * @return The DOM document.//from w w w . ja va 2 s .c om * @throws IOException It there is an error creating the dom. */ public static Document parse(InputSource is) throws IOException { try { DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = builderFactory.newDocumentBuilder(); return builder.parse(is); } catch (Exception e) { IOException thrown = new IOException(e.getMessage()); throw thrown; } }
From source file:Main.java
/** * This will parse an XML stream and create a DOM document. * * @param fileName The file to get the XML from. * @return The DOM document.//from w w w .j a v a 2 s .c om * @throws IOException It there is an error creating the dom. */ public static Document parse(String fileName) throws IOException { try { DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = builderFactory.newDocumentBuilder(); return builder.parse(fileName); } catch (Exception e) { IOException thrown = new IOException(e.getMessage()); throw thrown; } }
From source file:Main.java
/** * src:http://www.journaldev.com/1237/java-convert-string-to-xml-document-and-xml-document-to-string * @param xmlStr//from ww w. j a va 2 s. co m * @return */ public static Document convertStringToDocument(String xmlStr) { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder; try { builder = factory.newDocumentBuilder(); Document doc = builder.parse(new InputSource(new StringReader(xmlStr))); return doc; } catch (Exception e) { e.printStackTrace(); } return null; }
From source file:Main.java
/** Parse a XML document from string */ public static Document parse(String xml) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); InputSource is = new InputSource(new StringReader(xml)); return builder.parse(is); }
From source file:Main.java
public static Document loadXMLFromString(String xml) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); InputSource is = new InputSource(new StringReader(xml)); return builder.parse(is); }
From source file:Main.java
/** * Parses the given {@link InputSource} into a {@link Document}. * * @param is the input to parse// www .ja va 2s .c o m * @return the document * @throws SAXParseException on parse exception * @throws RuntimeException on IOException or non-parse-related SAXException */ public static Document parseDocument(InputSource is) throws SAXParseException { DocumentBuilder builder = newDocumentBuilder(); try { return builder.parse(is); } catch (IOException e) { throw new RuntimeException(e); } catch (SAXException e) { if (e instanceof SAXParseException) { throw (SAXParseException) e; } throw new RuntimeException(e); } }