List of usage examples for javax.xml.parsers DocumentBuilderFactory newDocumentBuilder
public abstract DocumentBuilder newDocumentBuilder() throws ParserConfigurationException;
From source file:Main.java
public static Object getBean() { try {//from ww w . j av a 2 s .co m DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = dFactory.newDocumentBuilder(); Document document; document = builder.parse(new File("config.xml")); NodeList nl = document.getElementsByTagName("TemplateClassName"); Node classNode = nl.item(0).getFirstChild(); String cName = classNode.getNodeValue(); System.out.println(cName); Class c = Class.forName("com.seven.templatemethod.one." + cName); Object object = c.newInstance(); return object; } catch (Exception e) { e.printStackTrace(); return null; } }
From source file:Main.java
/** * Parses a string into an XMLDocument//from w w w.jav a 2 s . com * * @param xmlString * @return * @throws IOException * @throws ParserConfigurationException * @throws SAXException */ public static Document loadXML(String xmlString) throws IOException, ParserConfigurationException, SAXException { InputStream inputStream = new ByteArrayInputStream(xmlString.getBytes()); DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); Document doc = dBuilder.parse(inputStream); // optional, but recommended // read this - // http://stackoverflow.com/questions/13786607/normalization-in-dom-parsing-with-java-how-does-it-work doc.getDocumentElement().normalize(); return doc; }
From source file:Main.java
public static Document deserialize(String sXML) throws ParserConfigurationException, SAXException, IOException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder;/* ww w.j a v a2s .com*/ builder = factory.newDocumentBuilder(); Document doc = builder.parse(new InputSource(new StringReader(sXML))); return doc; }
From source file:Main.java
public static Document loadDocument(InputStream is) throws ParserConfigurationException, SAXException, IOException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); //skip DTD validation builder.setEntityResolver(new EntityResolver() { public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException { return new InputSource(new StringReader("")); }//from ww w . j a v a 2s. c o m }); return builder.parse(is); }
From source file:Main.java
private static Element constructDocument() throws Exception { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); Document doc = db.newDocument(); Element rootElement = doc.createElement("root"); doc.appendChild(rootElement);// w w w . j a va 2 s . c o m Element c1Element = doc.createElement("c1"); Element c2Element = doc.createElement("c2"); Element c3Element = doc.createElement("c3"); Element gc1Element = doc.createElement("gc1_1"); Element gc2Element = doc.createElement("gc1_2"); Text textNode = doc.createTextNode("uncle_freddie"); Element gc3Element = doc.createElement("gc2_1"); rootElement.appendChild(c1Element); rootElement.appendChild(c2Element); rootElement.appendChild(c3Element); c1Element.appendChild(gc1Element); c1Element.appendChild(gc2Element); c2Element.appendChild(gc3Element); gc3Element.appendChild(textNode); return rootElement; }
From source file:Main.java
public static Node loadURL(java.net.URL url) { Document document = null;//from ww w . j a v a2s. com if (url != null) { try { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); document = builder.parse(url.openStream()); } catch (java.io.IOException ioe) { document = null; ioe.printStackTrace(); } catch (ParserConfigurationException pce) { document = null; pce.printStackTrace(); } catch (org.xml.sax.SAXException se) { document = null; se.printStackTrace(); } return document; } return null; }
From source file:Main.java
/** * Converts a String representing an XML snippet into an {@link org.w3c.dom.Element}. * * @param xml snippet as a string/*from w ww . j a v a2 s . c om*/ * * @return a DOM Element * * @throws Exception if unable to parse the String or if it doesn't contain valid XML. */ public static Element elementFromString(String xml) throws Exception { ByteArrayInputStream bais = new ByteArrayInputStream(xml.getBytes("UTF-8")); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse(bais); bais.close(); return document.getDocumentElement(); }
From source file:Main.java
private static Element getRootNodeFromXmlStr(String xmlStr) { Element element = null;//from ww w .j a v a 2s . co m try { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); InputStream is = new ByteArrayInputStream(xmlStr.getBytes("UTF-8")); Document document = db.parse(is); element = document.getDocumentElement(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (ParserConfigurationException e) { e.printStackTrace(); } catch (SAXException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return element; }
From source file:Main.java
public static NodeList getXMLNodes(final String xmlFileName, final String tagName) { NodeList nList = null;/*from ww w .j a v a 2 s. c om*/ try { File xmlFile = new File(xmlFileName); DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); Document doc = dBuilder.parse(xmlFile); doc.getDocumentElement().normalize(); nList = doc.getElementsByTagName(tagName); } catch (Exception e) { e.printStackTrace(); } return nList; }