Java XML String Transform getNotFoundXmlForObjectId(final String pid, final String detailCode, final String description)

Here you can find the source of getNotFoundXmlForObjectId(final String pid, final String detailCode, final String description)

Description

get Not Found Xml For Object Id

License

BSD License

Declaration

public static String getNotFoundXmlForObjectId(final String pid, final String detailCode,
            final String description) 

Method Source Code


//package com.java2s;
//License from project: BSD License 

import java.io.StringWriter;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.FactoryConfigurationError;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class Main {
    public static String getNotFoundXmlForObjectId(final String pid, final String detailCode,
            final String description) {

        final String name = "NotFound";
        final String errorCode = "404";

        DocumentBuilder documentBuilder;
        try {//from ww  w.j av a 2 s.c om
            documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        } catch (ParserConfigurationException e) {
            return e.getMessage();
        } catch (FactoryConfigurationError e) {
            return e.getMessage();
        }
        Document dom = documentBuilder.newDocument();
        // create the root node of the dom
        Element errorNode = dom.createElement("error");

        errorNode.setAttribute("name", name);
        errorNode.setAttribute("detailCode", detailCode);
        errorNode.setAttribute("errorCode", errorCode);

        Element desc = dom.createElement("description");
        desc.appendChild(dom.createTextNode(description));

        dom.appendChild(errorNode);
        errorNode.appendChild(desc);

        try {
            return domToString(dom);
        } catch (Exception ex) {
            // log.error(ex.getMessage());
            return ex.getMessage();
        }
    }

    private static String domToString(final Document document) throws Exception {
        String result = null;
        StringWriter strWtr = new StringWriter();
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();

        if (document != null) {
            StreamResult strResult = new StreamResult(strWtr);
            transformer.transform(new DOMSource(document.getDocumentElement()), strResult);
            result = strResult.getWriter().toString();
        }
        return result;
    }
}

Related

  1. getBeanDefinition(String beanId, File file)
  2. getConfigurationAsXML(Map properties)
  3. getHtmlAsString(Node node)
  4. getIntegerAttribute(XMLStreamReader in, String attribute)
  5. getNode(String text)
  6. getOutputMimeType(String xslName)
  7. getOutputSQLPageXML(String inStatement, Map map)
  8. getPropertyAsString(final Properties prop, final String comment)
  9. getSchema(String schemaString)