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 Object getBean() { try {//from w w w.ja va 2 s . c o m DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = dFactory.newDocumentBuilder(); Document document; document = builder.parse(new File("config.xml")); NodeList nl = document.getElementsByTagName("StrategyClassName"); Node classNode = nl.item(0).getFirstChild(); String cName = classNode.getNodeValue(); System.out.println(cName); Class c = Class.forName("com.seven.strategy.sort." + cName); Object object = c.newInstance(); return object; } catch (Exception e) { e.printStackTrace(); return null; } }
From source file:Main.java
public static Document buildDocumentFromSource(InputSource is, boolean namespaceaware) throws ParserConfigurationException, SAXException, IOException { DocumentBuilder builder = getSyncDocumentBuilder(namespaceaware); //Parse the document Document doc = builder.parse(is); builder.reset();//from w ww . j av a 2s . c om return doc; }
From source file:Main.java
public static boolean compareXmls(InputStream xml1, InputStream xml2) throws ParserConfigurationException, SAXException, IOException { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true);/*from w ww . j av a2s. c o m*/ dbf.setCoalescing(true); dbf.setIgnoringElementContentWhitespace(true); dbf.setIgnoringComments(true); DocumentBuilder db = dbf.newDocumentBuilder(); Document doc1 = db.parse(xml1); doc1.normalizeDocument(); Document doc2 = db.parse(xml2); doc2.normalizeDocument(); return doc2.isEqualNode(doc1); }
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 av a 2 s .c o m * @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) { System.out.println(e.getMessage()); IOException thrown = new IOException(e.getMessage()); throw thrown; } }
From source file:Main.java
/** * Loads an XML document from an InputStream *//* w w w .ja va2 s.c om*/ public static Document loadXmlDoc(final InputStream stream) { Document result = null; try { DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance(); domFactory.setExpandEntityReferences(false); domFactory.setIgnoringComments(true);//strips comments // domFactory.setIgnoringElementContentWhitespace(true);//would be nice if it worked domFactory.setNamespaceAware(true); DocumentBuilder builder = domFactory.newDocumentBuilder(); result = builder.parse(stream); } catch (ParserConfigurationException e) { e.printStackTrace(); } catch (SAXException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return result; }
From source file:Main.java
public static Document parseXML(String xml) throws ParserConfigurationException, IOException, SAXException { DocumentBuilder builder; synchronized (factory) { builder = factory.newDocumentBuilder(); }//from ww w . ja v a 2 s .c o m return builder.parse(new InputSource(new StringReader(xml))); }
From source file:Main.java
public static Object getBean() { try {// ww w.j a va2 s .c o 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
/** * Return a Node corresponding to the input XML string representation. * @param inputSource source.//from w w w . j a va 2s . co m * @return An instance of Node corresponding to the input XML string representation. * @throws ParserConfigurationException * @throws SAXException * @throws IOException */ public static Node getXMLNode(InputSource inputSource) throws ParserConfigurationException, SAXException, IOException { DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); docFactory.setNamespaceAware(true); DocumentBuilder docBuilder = docFactory.newDocumentBuilder(); Document doc = docBuilder.parse(inputSource); return (doc); }
From source file:Main.java
public static String getRequestNameFull(String xml) throws ParserConfigurationException, SAXException, IOException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse(new InputSource(new StringReader(xml))); NodeList nodeList = document.getDocumentElement().getChildNodes(); Node reqName = document.getElementsByTagName("request-name").item(0); System.out.println(reqName.getTextContent()); for (int i = 0; i < nodeList.getLength(); i++) { Node node = nodeList.item(i); System.out.println(i + "--" + node); if (node instanceof Element) { NodeList childNodes = node.getChildNodes(); for (int j = 0; j < childNodes.getLength(); j++) { Node cNode = childNodes.item(j); System.out.println(i + "--" + j + "--" + cNode); if (cNode instanceof Element) { String content = cNode.getLastChild().getTextContent().trim(); System.out.println(i + "--" + j + "--" + content); }//from ww w. j a v a 2 s. co m } } } /* * Do the parsing for reqname */ return reqName.getTextContent(); }
From source file:Main.java
public static Document createDocument(byte[] data) throws RuntimeException { DocumentBuilderFactory f = DocumentBuilderFactory.newInstance(); InputStream input = null;// ww w. ja v a2 s. c o m try { DocumentBuilder p = f.newDocumentBuilder(); input = new ByteArrayInputStream(data); return p.parse(input); } catch (ParserConfigurationException e) { throw new RuntimeException(e); } catch (SAXException e) { throw new RuntimeException(e); } catch (IOException e) { throw new RuntimeException(e); } }