List of utility methods to do XML Document from String
Document | getDocumentFromBytes(byte[] data) Creates an XML document object from a byte array. DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); documentBuilderFactory.setNamespaceAware(true); DocumentBuilder docBuilder = documentBuilderFactory.newDocumentBuilder(); Document doc = docBuilder.parse(new ByteArrayInputStream(data)); return doc; |
Document | getDocumentFromString(String text) Returns a new document built from the input stream. Document document; try { DocumentBuilder docBuilder = getDocumentBuilder(); InputStream is = new ByteArrayInputStream(text.getBytes("UTF-8")); document = docBuilder.parse(is); } catch (Exception e) { e.printStackTrace(); return null; ... |
Document | getDocumentFromString(String xml) get Document From String Document retVal = null; try { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); retVal = db.parse(getInputSourceFromString(xml)); } catch (Exception ex) { ex.printStackTrace(); return retVal; |
Document | getDocumentFromString(String xml) get Document From String Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder() .parse(new ByteArrayInputStream(xml.getBytes())); return document; |
Document | getDocumentFromString(String xml) get Document From String InputSource source = new InputSource(new StringReader(xml)); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setIgnoringElementContentWhitespace(true); DocumentBuilder db = dbf.newDocumentBuilder(); Document document = db.parse(source); document.normalize(); return document; |
Document | getDocumentFromString(String xmlString) Parses an XML string. StringReader sr = new StringReader(xmlString); DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder(); return db.parse(new InputSource(sr)); |
Document | getDocumentFromString(String xmlString) get Document From String DocumentBuilderFactory oDocumentBuilderFactory = DocumentBuilderFactory.newInstance(); try { oDocumentBuilderFactory.setAttribute("http://apache.org/xml/features/nonvalidating/load-external-dtd", Boolean.FALSE); } catch (IllegalArgumentException e) { DocumentBuilder oDocumentBuilder = oDocumentBuilderFactory.newDocumentBuilder(); InputSource inputSource = new InputSource(new StringReader(xmlString)); ... |
Document | getXmlDocument(final String container) get Xml Document final InputStream inputStream = new ByteArrayInputStream(container.getBytes()); final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); final DocumentBuilder docBuilder = factory.newDocumentBuilder(); return docBuilder.parse(inputStream); |
Document | getXMLDocument(final String xml) Parse string with XML content to org.w3c.dom.Document . try { return getNewDocumentBuilder().parse(new InputSource(new StringReader(xml))); } catch (IOException e) { throw new SAXException(e); } catch (ParserConfigurationException e) { throw new SAXException(e); |
Document | loadDocument(String documentContent) Method should be invoked only from unit tests Class<?> documentClass = Class.forName("org.netbeans.editor.BaseDocument"); Constructor<?> documentConstructor = documentClass.getConstructor(boolean.class, String.class); Document document = (Document) documentConstructor.newInstance(false, "text/xml"); document.insertString(0, documentContent, null); return document; |