List of usage examples for javax.xml.parsers DocumentBuilderFactory setCoalescing
public void setCoalescing(boolean coalescing)
From source file:Main.java
public static void main(String[] argv) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setCoalescing(true); Document doc = factory.newDocumentBuilder().parse(new File("infilename.xml")); // doc will not contain any CDATA nodes }
From source file:MainClass.java
public static void main(String[] args) throws IOException, ParserConfigurationException, org.xml.sax.SAXException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setIgnoringComments(true);//from www . j a va2 s .co m factory.setCoalescing(true); // Convert CDATA to Text nodes factory.setNamespaceAware(false); // No namespaces: this is default factory.setValidating(false); // Don't validate DTD: also default DocumentBuilder parser = factory.newDocumentBuilder(); Document document = parser.parse(new File(args[0])); NodeList sections = document.getElementsByTagName("sect1"); int numSections = sections.getLength(); for (int i = 0; i < numSections; i++) { Element section = (Element) sections.item(i); // A <sect1> Node title = section.getFirstChild(); while (title != null && title.getNodeType() != Node.ELEMENT_NODE) title = title.getNextSibling(); if (title != null) System.out.println(title.getFirstChild().getNodeValue()); } }
From source file:Main.java
private static DocumentBuilder getDocumentBuilder() throws ParserConfigurationException { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setIgnoringComments(true);/*from w ww .j a v a2 s . co m*/ dbf.setCoalescing(true); dbf.setIgnoringElementContentWhitespace(true); dbf.setValidating(false); return dbf.newDocumentBuilder(); }
From source file:Main.java
public static Document createDocument(Reader reader) throws IllegalArgumentException { // init DOM builder DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setCoalescing(true); factory.setIgnoringComments(true);/*from w w w . ja v a 2 s . co m*/ factory.setIgnoringElementContentWhitespace(true); factory.setValidating(false); try { DocumentBuilder builder = factory.newDocumentBuilder(); InputSource inputSource = new InputSource(reader); Document doc = builder.parse(inputSource); return doc; } catch (Exception ex) { IllegalArgumentException iae = new IllegalArgumentException(ex.getMessage()); iae.initCause(ex); throw iae; } }
From source file:Main.java
public static Document parse(File file) throws IOException { try {/*from w w w .j a va 2s. co m*/ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setCoalescing(true); factory.setIgnoringComments(true); factory.setIgnoringElementContentWhitespace(true); factory.setNamespaceAware(true); DocumentBuilder parser = factory.newDocumentBuilder(); return parser.parse(file); } catch (ParserConfigurationException e) { throw new IOException(e); } catch (SAXException e) { throw new IOException(e); } }
From source file:Main.java
public static Document parse(InputStream is) throws IOException { try {// ww w. j a va 2s .co m DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setCoalescing(true); factory.setIgnoringComments(true); factory.setIgnoringElementContentWhitespace(true); factory.setNamespaceAware(true); DocumentBuilder parser = factory.newDocumentBuilder(); InputSource in = new InputSource(is); return parser.parse(in); } catch (ParserConfigurationException e) { throw new IOException(e); } catch (SAXException e) { throw new IOException(e); } }
From source file:Main.java
private static DocumentBuilderFactory createDocumentBuilderFactory() { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true);/*from ww w. ja v a 2 s .c o m*/ factory.setCoalescing(true); factory.setIgnoringComments(true); return factory; }
From source file:Main.java
public static synchronized DocumentBuilder getSyncDocumentBuilder(boolean namespaceAware, boolean coalescing, boolean ignoringElementContentWhitespace) throws ParserConfigurationException { DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance(); domFactory.setNamespaceAware(namespaceAware); domFactory.setCoalescing(coalescing); domFactory.setIgnoringElementContentWhitespace(ignoringElementContentWhitespace); return domFactory.newDocumentBuilder(); }
From source file:Main.java
/** Read an XML document from a String */ public static Document readXmlDocumentFromString(String xml) throws Exception { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setIgnoringElementContentWhitespace(true); dbf.setCoalescing(true); DocumentBuilder db = dbf.newDocumentBuilder(); Document doc = db.parse(new ByteArrayInputStream(xml.getBytes())); return doc;/*from w ww . j ava 2 s . c o m*/ }
From source file:Main.java
/** Read an XML document from an InputStream */ public static Document readXmlDocumentFromStream(InputStream stream) throws Exception { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setIgnoringElementContentWhitespace(true); dbf.setCoalescing(true); DocumentBuilder db = dbf.newDocumentBuilder(); Document doc = db.parse(stream); return doc;//from ww w. j a v a 2s.c o m }