Java Utililty Methods XML Document Create

List of utility methods to do XML Document Create

Description

The list of methods to do XML Document Create are organized into topic(s).

Method

DocumentcreateDocumentFromXml(String input)
create Document From Xml
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
StringReader stringReader = new StringReader(input);
InputSource inputSource = new InputSource(stringReader);
return documentBuilder.parse(inputSource);
DocumentcreateDocumentFromXMLContent(String docContent)
create Document From XML Content
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
Document doc = docBuilder.parse(new ByteArrayInputStream(docContent.getBytes()));
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory
        .newTransformer(new StreamSource("src/test/resources/strip-spaces.xls"));
DOMSource source = new DOMSource(doc);
OutputStream os = new ByteArrayOutputStream();
...
DocumentcreateDocumentFromXmlFragment(final InputStream inputStreamXmlFragment, final URL urlSchema)
create Document From Xml Fragment
Document ret = null;
if (null == inputStreamXmlFragment) {
    throw new NullPointerException("input stream XML fragment is null");
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
if (null != urlSchema) {
    SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
...
DocumentcreateDocumentFromXmlString(String xmlStr)
create Document From Xml String
try {
    DocumentBuilderFactory docBuilderFactory;
    docBuilderFactory = DocumentBuilderFactory.newInstance();
    docBuilderFactory.setIgnoringComments(true);
    docBuilderFactory.setIgnoringElementContentWhitespace(true);
    docBuilderFactory.setNamespaceAware(false);
    DocumentBuilder builder = docBuilderFactory.newDocumentBuilder();
    ByteArrayInputStream bais = new ByteArrayInputStream(xmlStr.getBytes());
...
DocumentcreateEmptyDocument()
Create empty document
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = dbf.newDocumentBuilder();
return builder.newDocument();
DocumentcreateEmptyDocument()
create Empty Document
DocumentBuilder documentBuilder = createDocumentBuilder();
return documentBuilder.newDocument();
DocumentcreateEmptyDocument()
Creates an empty XML document.
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder;
try {
    builder = factory.newDocumentBuilder();
} catch (ParserConfigurationException e) {
    throw new IllegalStateException("No document builder found, probably Java is misconfigured!", e);
return builder.newDocument();
...
DocumentcreateEmptyDocument()
Purpose: Creates an Empty XML Document object
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
DOMImplementation impl = builder.getDOMImplementation();
Document doc = impl.createDocument(null, null, null);
return doc;
DocumentcreateEmptyDocument(final DocumentBuilder builder)
create Empty Document
return builder.newDocument();
ElementcreateEmptyElement(Document doc, QName qname)
create Empty Element
String prefix = qname.getPrefix();
String name = qname.getLocalPart();
if (prefix != null && prefix.length() > 0)
    name = prefix + ':' + name;
String uri = qname.getNamespaceURI();
return doc.createElementNS(uri, name);