List of usage examples for javax.xml.parsers DocumentBuilderFactory setIgnoringElementContentWhitespace
public void setIgnoringElementContentWhitespace(boolean whitespace)
From source file:Main.java
public static void main(String[] args) throws Exception { File CFile = new File("data.xml"); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setIgnoringComments(true);/*from ww w . java 2 s . c om*/ factory.setIgnoringElementContentWhitespace(true); factory.setValidating(false); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse(CFile); NodeList pizzas = document.getElementsByTagName("Pizza"); for (int i = 0; i < pizzas.getLength(); i++) { Element pizzaSize = (Element) pizzas.item(i); String pSize = pizzaSize.getAttribute("Size"); if (pSize.equalsIgnoreCase("Large")) System.out.println(10.0); if (pSize.equalsIgnoreCase("Medium")) System.out.println(7.0); if (pSize.equalsIgnoreCase("Small")) System.out.println(5.0); } }
From source file:Main.java
static public void main(String[] arg) throws Exception { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setValidating(true);/*from w w w . ja v a 2 s .c om*/ dbf.setNamespaceAware(true); dbf.setIgnoringElementContentWhitespace(true); DocumentBuilder builder = dbf.newDocumentBuilder(); StringReader sr = new StringReader("<tag>java2s.com</tag>"); Document document = builder.parse(new InputSource(sr)); deleteFirstElement(document); TransformerFactory tf = TransformerFactory.newInstance(); Transformer trans = tf.newTransformer(); StringWriter sw = new StringWriter(); trans.transform(new DOMSource(document), new StreamResult(sw)); System.out.println(sw.toString()); }
From source file:ListMoviesXML.java
public static void main(String[] args) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setIgnoringComments(true);/*from w w w .j ava 2s . c o m*/ factory.setIgnoringElementContentWhitespace(true); factory.setValidating(true); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse(new InputSource("y.xml")); Element root = doc.getDocumentElement(); Element movieElement = (Element) root.getFirstChild(); Movie m; while (movieElement != null) { m = getMovie(movieElement); String msg = Integer.toString(m.year); msg += ": " + m.title; msg += " (" + m.price + ")"; System.out.println(msg); movieElement = (Element) movieElement.getNextSibling(); } }
From source file:Main.java
static public void main(String[] arg) throws Exception { String filename = "input.xml"; boolean validate = true; DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setValidating(validate);/*from w w w. jav a 2 s. c om*/ dbf.setNamespaceAware(true); dbf.setIgnoringElementContentWhitespace(true); DocumentBuilder builder = dbf.newDocumentBuilder(); builder.setErrorHandler(new MyErrorHandler()); InputSource is = new InputSource(filename); Document doc = builder.parse(is); TreeDumper td = new TreeDumper(); td.dump(doc); }
From source file:Main.java
public static void main(String args[]) throws Exception { DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); builderFactory.setNamespaceAware(true); // Set namespace aware builderFactory.setValidating(true); // and validating parser feaures builderFactory.setIgnoringElementContentWhitespace(true); DocumentBuilder builder = builderFactory.newDocumentBuilder(); // Create the parser Document xmlDoc = builder.parse(new InputSource(new StringReader(xmlString))); System.out.println(xmlDoc.getDocumentURI()); }
From source file:Main.java
static public void main(String[] arg) { boolean validate = true; DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setValidating(validate);/* w w w . j av a 2 s .c o m*/ dbf.setNamespaceAware(true); dbf.setIgnoringElementContentWhitespace(true); // Parse the input to produce a parse tree with its root // in the form of a Document object Document doc = null; try { DocumentBuilder builder = dbf.newDocumentBuilder(); InputSource is = new InputSource(new StringReader(getXMLData())); doc = builder.parse(is); } catch (SAXException e) { System.exit(1); } catch (ParserConfigurationException e) { System.err.println(e); System.exit(1); } catch (IOException e) { System.err.println(e); System.exit(1); } dump(doc); }
From source file:Main.java
public static void main(String args[]) throws Exception { DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); builderFactory.setNamespaceAware(true); // Set namespace aware builderFactory.setValidating(true); // and validating parser feaures builderFactory.setIgnoringElementContentWhitespace(true); DocumentBuilder builder = builderFactory.newDocumentBuilder(); // Create the parser Document xmlDoc = builder.parse(new InputSource(new StringReader(xmlString))); DOMConfiguration config = xmlDoc.getDomConfig(); }
From source file:MainClass.java
static public void main(String[] arg) { boolean validate = false; DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setValidating(validate);/* w w w.ja v a 2 s .c o m*/ dbf.setNamespaceAware(true); dbf.setIgnoringElementContentWhitespace(true); Document doc = null; try { DocumentBuilder builder = dbf.newDocumentBuilder(); doc = builder.parse(new InputSource(new StringReader(xmlString))); } catch (SAXException e) { System.exit(1); } catch (ParserConfigurationException e) { System.err.println(e); System.exit(1); } catch (IOException e) { System.err.println(e); System.exit(1); } TreeDumper td = new TreeDumper(); td.dump(doc); }
From source file:Main.java
static public void main(String[] arg) { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setValidating(true);/*from w w w . ja va 2 s. com*/ dbf.setNamespaceAware(true); dbf.setIgnoringElementContentWhitespace(true); Document doc = null; try { DocumentBuilder builder = dbf.newDocumentBuilder(); InputSource is = new InputSource(new StringReader(getXMLData())); doc = builder.parse(is); write(doc); } catch (Exception e) { System.err.println(e); } }
From source file:DOMDump.java
static public void main(String[] arg) { boolean validate = true; DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setValidating(validate);/*from w w w . j a v a2 s. c o m*/ dbf.setNamespaceAware(true); dbf.setIgnoringElementContentWhitespace(true); // Parse the input to produce a parse tree with its root // in the form of a Document object Document doc = null; try { DocumentBuilder builder = dbf.newDocumentBuilder(); builder.setErrorHandler(new MyErrorHandler()); InputSource is = new InputSource("personWithDTD.xml"); doc = builder.parse(is); } catch (SAXException e) { System.exit(1); } catch (ParserConfigurationException e) { System.err.println(e); System.exit(1); } catch (IOException e) { System.err.println(e); System.exit(1); } dump(doc); }