List of utility methods to do XML Document Create
Document | createEmptyDocument() create Empty Document final DocumentBuilderFactory factory = DocumentBuilderFactory .newInstance(); try { return factory.newDocumentBuilder().newDocument(); } catch (ParserConfigurationException e) { throw new RuntimeException("Should not reach this point"); |
Document | createNewDocument() create New Document final Document document; DocumentBuilder builder = createDocumentBuilder(false); document = builder.newDocument(); return document; |
Document | createDocument() create Document DocumentBuilder builder = DocumentBuilderFactory.newInstance()
.newDocumentBuilder();
return builder.newDocument();
|
Document | newDocument(String rootName) new Document DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); Document xml = db.newDocument(); Node root = xml.createElement(rootName); Attr a = xml.createAttribute("version"); a.setValue("0.6"); root.getAttributes().setNamedItem(a); a = xml.createAttribute("encoding"); ... |
Document | stringToDocument(String xmlString) Converts String containing XML code to Document if (xmlString == null) return null; DocumentBuilder documentBuilder = getDocumentBuilder(); InputSource inputSource = new InputSource(new StringReader( xmlString)); try { return documentBuilder.parse(inputSource); } catch (SAXException e) { ... |
Document | stringToDocument(String xmlString) Converts String containing XML code to Document if (xmlString == null) return null; DocumentBuilder documentBuilder = getDocumentBuilder(); InputSource inputSource = new InputSource(new StringReader( xmlString)); try { return documentBuilder.parse(inputSource); } catch (SAXException e) { ... |
Document | createDocument(final String source, boolean isNamespaceAware) create Document final Document document; DocumentBuilder builder = createDocumentBuilder(isNamespaceAware); try { document = builder.parse(new ByteArrayInputStream(source .getBytes())); } catch (SAXException e) { String errMsg = "Failed to parse XML source(" + e.getMessage() + ") [" + source + "]"; ... |
String | createXml(Document document) Creates XML from W3C Document from the xml. TransformerFactory transformerFactory = TransformerFactory .newInstance(); Transformer transformer = transformerFactory.newTransformer(); StringWriter stringWriter = new StringWriter(); StreamResult result = new StreamResult(stringWriter); DOMSource source = new DOMSource(document); transformer.transform(source, result); return stringWriter.toString(); ... |
Element | getOrCreateElement(Document doc, Element manifestElement, String elementName) get Or Create Element NodeList nodeList = manifestElement .getElementsByTagName(elementName); Element element = null; if (nodeList.getLength() == 0) { element = doc.createElement(elementName); manifestElement.appendChild(element); } else { element = (Element) nodeList.item(0); ... |
Document | getDomElement(File xmlFile) get Dom Element Document document = null; DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory .newInstance(); DocumentBuilder documentBuilder = documentBuilderFactory .newDocumentBuilder(); InputSource inputSource = new InputSource(); inputSource.setCharacterStream(new FileReader(xmlFile)); document = documentBuilder.parse(inputSource); ... |