Java Utililty Methods XML String Transform

List of utility methods to do XML String Transform

Description

The list of methods to do XML String Transform are organized into topic(s).

Method

StringgetHtmlAsString(Node node)
get Html As String
String s = "";
try {
    s = getXmlAsString(node);
} catch (TransformerException e) {
return s.toLowerCase().replaceAll(" xmlns=\"http://www.w3.org/1999/xhtml\"", "");
IntegergetIntegerAttribute(XMLStreamReader in, String attribute)
get Integer Attribute
return parseInt(getAttribute(in, attribute));
NodegetNode(String text)
get Node
Node ret = null;
DocumentBuilderFactory fact = DocumentBuilderFactory.newInstance();
fact.setNamespaceAware(true);
java.io.InputStream xmlstr = new java.io.ByteArrayInputStream(text.getBytes());
DocumentBuilder builder = fact.newDocumentBuilder();
org.w3c.dom.Document doc = builder.parse(xmlstr);
xmlstr.close();
ret = doc.getDocumentElement();
...
StringgetNotFoundXmlForObjectId(final String pid, final String detailCode, final String description)
get Not Found Xml For Object Id
final String name = "NotFound";
final String errorCode = "404";
DocumentBuilder documentBuilder;
try {
    documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
} catch (ParserConfigurationException e) {
    return e.getMessage();
} catch (FactoryConfigurationError e) {
...
StringgetOutputMimeType(String xslName)
Returns the MIME type of the XSLT transformation output taken from the "media-type" attribute of the xsl:output element.
String mimeType = "";
String mediaType = "";
String method = "";
getDocument(xslName);
NodeList nList = xsl.getElementsByTagName("xsl:output");
for (int i = 0; i < nList.getLength(); i++) {
    Node outputNode = nList.item(i);
    if (outputNode.getNodeType() == Node.ELEMENT_NODE) {
...
StringgetOutputSQLPageXML(String inStatement, Map map)
get Output SQL Page XML
org.w3c.dom.Document doc = null;
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = null;
try {
    builder = factory.newDocumentBuilder();
} catch (ParserConfigurationException e1) {
    e1.printStackTrace();
DOMImplementation impl = builder.getDOMImplementation();
doc = impl.createDocument(null, "SQLPage", null);
Element root = doc.getDocumentElement();
for (Iterator it = map.entrySet().iterator(); it.hasNext();) {
    Object element = it.next();
    if (element instanceof Map.Entry) {
        String key = (String) ((Map.Entry) element).getKey();
        String value = (String) ((Map.Entry) element).getValue();
        Attr attr = doc.createAttribute(key);
        attr.setValue(value);
        root.setAttributeNode(attr);
Element item = doc.createElement("Statement"); 
if (inStatement == null)
    inStatement = "";
item.appendChild(doc.createCDATASection(inStatement));
root.appendChild(item);
StringWriter stringOut = new StringWriter(); 
DOMSource domSource = new DOMSource(doc);
StreamResult streamResult = new StreamResult(stringOut);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer serializer = null;
try {
    serializer = tf.newTransformer();
} catch (TransformerConfigurationException e) {
    e.printStackTrace();
serializer.setOutputProperty(OutputKeys.INDENT, "yes");
try {
    serializer.transform(domSource, streamResult);
} catch (TransformerException e2) {
    e2.printStackTrace();
return stringOut.toString();
byte[]getPropertyAsString(final Properties prop, final String comment)
get Property As String
final ByteArrayOutputStream out = new ByteArrayOutputStream();
prop.store(out, comment);
return out.toByteArray();
SchemagetSchema(String schemaString)
get Schema
Schema schema = null;
try {
    if (schemaString != null) {
        SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        StreamSource ss = new StreamSource();
        ss.setReader(new StringReader(schemaString));
        schema = sf.newSchema(ss);
} catch (Exception e) {
    throw new RuntimeException("Failed to create schemma from string: " + schemaString, e);
return schema;
SchemagetSchemaFromResource(String... files)
get Schema From Resource
InputStream[] inputStreams = new InputStream[files.length];
Schema schema = null;
try {
    for (int i = 0; i < files.length; i++) {
        inputStreams[i] = loadResourceInputStream(files[i]);
    schema = getSchema(inputStreams);
} finally {
...
StringgetSourceAsString(Source source)
get Source As String
Transformer transformer = TransformerFactory.newInstance().newTransformer();
OutputStream outputStream = new ByteArrayOutputStream();
StreamResult streamResult = new StreamResult();
streamResult.setOutputStream(outputStream);
transformer.transform(source, streamResult);
return streamResult.getOutputStream().toString();