Example usage for org.w3c.dom Document createTextNode

List of usage examples for org.w3c.dom Document createTextNode

Introduction

In this page you can find the example usage for org.w3c.dom Document createTextNode.

Prototype

public Text createTextNode(String data);

Source Link

Document

Creates a Text node given the specified string.

Usage

From source file:Main.java

public static String convertResultSetToXML(ResultSet rs)
        throws SQLException, ParserConfigurationException, TransformerException {
    ResultSetMetaData rsmd = rs.getMetaData();
    int colCount = rsmd.getColumnCount();

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document doc = builder.newDocument();
    Element results = doc.createElement("Results");
    doc.appendChild(results);//from ww  w  .j  a v a2  s.  c o  m

    while (rs.next()) {
        Element row = doc.createElement("Row");
        results.appendChild(row);
        for (int i = 1; i <= colCount; i++) {
            String columnName = rsmd.getColumnName(i);
            Object value = rs.getObject(i);
            if (value != null) {
                Element node = doc.createElement(columnName.replaceAll("\\(", "_").replaceAll("\\)", "_"));
                node.appendChild(doc.createTextNode(value.toString()));
                row.appendChild(node);
            }
        }
    }

    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    StringWriter writer = new StringWriter();
    transformer.transform(new DOMSource(doc), new StreamResult(writer));
    String output = writer.getBuffer().toString().replaceAll("\n|\r", "");

    return output;
}

From source file:Main.java

public static Document documentify(ResultSet rs) throws ParserConfigurationException, SQLException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document doc = builder.newDocument();
    Element results = doc.createElement("Results");
    doc.appendChild(results);//w  ww  .  j av  a2 s.c o  m
    ResultSetMetaData rsmd = rs.getMetaData();
    int colCount = rsmd.getColumnCount();

    while (rs.next()) {
        Element row = doc.createElement("Row");
        results.appendChild(row);
        for (int i = 1; i <= colCount; i++) {
            String columnName = rsmd.getColumnName(i);
            Object value = rs.getObject(i);
            Element node = doc.createElement(columnName);
            node.appendChild(doc.createTextNode(value.toString()));
            row.appendChild(node);
        }
    }
    return doc;
}

From source file:com.amalto.core.storage.hibernate.DefaultStorageClassLoader.java

private static void addProperty(Document document, Node sessionFactoryElement, String propertyName,
        String propertyValue) {/* w w w  . j  a v  a 2  s .  c  o  m*/
    Element property = document.createElement("property"); //$NON-NLS-1$
    Attr name = document.createAttribute("name"); //$NON-NLS-1$
    name.setValue(propertyName);
    property.getAttributes().setNamedItem(name);
    property.appendChild(document.createTextNode(propertyValue));
    sessionFactoryElement.appendChild(property);
}

From source file:Main.java

public static boolean creteEntity(Object entity, String fileName) {
    boolean flag = false;
    try {/* w  w w .j a va2  s  .c o m*/
        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
        Document document = documentBuilder.parse(fileName);

        Class clazz = entity.getClass();
        Field[] fields = clazz.getDeclaredFields();
        Node EntityElement = document.getElementsByTagName(clazz.getSimpleName() + "s").item(0);

        Element newEntity = document.createElement(clazz.getSimpleName());
        EntityElement.appendChild(newEntity);

        for (Field field : fields) {
            field.setAccessible(true);
            Element element = document.createElement(field.getName());
            element.appendChild(document.createTextNode(field.get(entity).toString()));
            newEntity.appendChild(element);
        }

        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        DOMSource domSource = new DOMSource(document);
        StreamResult streamResult = new StreamResult(new File(fileName));
        transformer.transform(domSource, streamResult);
        flag = true;
    } catch (ParserConfigurationException pce) {
        System.out.println(pce.getLocalizedMessage());
        pce.printStackTrace();
    } catch (TransformerException te) {
        System.out.println(te.getLocalizedMessage());
        te.printStackTrace();
    } catch (IOException ioe) {
        System.out.println(ioe.getLocalizedMessage());
        ioe.printStackTrace();
    } catch (SAXException sae) {
        System.out.println(sae.getLocalizedMessage());
        sae.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }
    return flag;
}

From source file:com.hphoto.server.ApiServlet.java

private static Element addNode(Document doc, Node parent, String ns, String name, String text) {
    Element child = doc.createElementNS((String) NS_MAP.get(ns), ns + ":" + name);
    child.appendChild(doc.createTextNode(getLegalXml(text)));
    parent.appendChild(child);//from w  ww .j av  a  2s  . c om
    return child;
}

From source file:Main.java

public static void nodeSubUpdate(Element root, String table, String queryType, String queryValue) {
    Node find = getTag(root, table);
    Document doc = find.getOwnerDocument();

    NodeList nl = find.getChildNodes();
    int numnodes = nl.getLength();

    System.out.println("length : " + numnodes);

    Node replNode = null;//ww w .j a  va  2s. c o m

    for (int i = 0; i < numnodes; i++) {
        Node n = nl.item(i);
        String name = n.getNodeName();

        if (name.equals(queryType)) {
            replNode = n;
            System.out.println("Finding Node : " + replNode.getNodeName());

            break;
        }
    }

    Element type = doc.createElement(queryType);
    Text value = doc.createTextNode(queryValue);

    type.appendChild(value);

    find.replaceChild(type, replNode);

    print(doc);
}

From source file:at.gv.egovernment.moa.id.util.client.mis.simple.MISSimpleClient.java

public static List<MISMandate> sendGetMandatesRequest(String webServiceURL, String sessionId,
        SSLSocketFactory sSLSocketFactory) throws MISSimpleClientException {
    if (webServiceURL == null) {
        throw new NullPointerException("Argument webServiceURL must not be null.");
    }/*from  w w w  . j a  v a  2s .c o m*/
    if (sessionId == null) {
        throw new NullPointerException("Argument sessionId must not be null.");
    }

    // ssl settings
    if (sSLSocketFactory != null) {
        SZRGWSecureSocketFactory fac = new SZRGWSecureSocketFactory(sSLSocketFactory);
        Protocol.registerProtocol("https", new Protocol("https", fac, 443));
    }

    try {
        Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
        Element mirElement = doc.createElementNS(MIS_NS, "MandateIssueRequest");
        Element sessionIdElement = doc.createElementNS(MIS_NS, "SessionID");
        sessionIdElement.appendChild(doc.createTextNode(sessionId));
        mirElement.appendChild(sessionIdElement);

        // send soap request
        Element mandateIssueResponseElement = sendSOAPRequest(webServiceURL, mirElement);

        // check for error
        checkForError(mandateIssueResponseElement);

        // check for session id
        NodeList mandateElements = XPathAPI.selectNodeList(mandateIssueResponseElement,
                "//mis:MandateIssueResponse/mis:Mandates/mis:Mandate", NS_NODE);

        if (mandateElements == null || mandateElements.getLength() == 0) {
            throw new MISSimpleClientException("No mandates found in response.");
        }

        ArrayList<MISMandate> foundMandates = new ArrayList<MISMandate>();
        for (int i = 0; i < mandateElements.getLength(); i++) {
            Element mandate = (Element) mandateElements.item(i);

            MISMandate misMandate = new MISMandate();
            if (mandate.hasAttribute("ProfessionalRepresentative")) {
                //               System.out.println("OID: " + mandate.getAttribute("ProfessionalRepresentative"));
                misMandate.setProfRep(mandate.getAttribute("ProfessionalRepresentative"));
            }
            if (mandate.hasAttribute("OWbPK")) {
                misMandate.setOWbPK(mandate.getAttribute("OWbPK"));
                //               System.out.println("OWBPK: " + mandate.getAttribute("OWbPK"));
            }

            //misMandate.setMandate(Base64.decodeBase64(DOMUtils.getText(mandate)));
            misMandate.setMandate(Base64.decodeBase64(DOMUtils.getText(mandate).getBytes()));
            misMandate.setFullMandateIncluded(true);

            foundMandates.add(misMandate);
        }
        return foundMandates;
    } catch (ParserConfigurationException e) {
        throw new MISSimpleClientException("service.06", e);
    } catch (DOMException e) {
        throw new MISSimpleClientException("service.06", e);
    } catch (TransformerException e) {
        throw new MISSimpleClientException("service.06", e);
    }
}

From source file:fr.ece.epp.tools.Utils.java

public static void updatePom(String path, String[] repo, boolean outOrno) {
    Document document = null;
    try {/*from   w  w  w . jav a 2  s  .  c  om*/
        document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(path);
        Element root = document.getDocumentElement();
        Node repositories = root.getElementsByTagName("repositories").item(0);
        for (int i = 0; i < repo.length; i++) {
            Element repository = document.createElement("repository");

            Element id = document.createElement("id");
            id.appendChild(document.createTextNode("repository" + i));
            repository.appendChild(id);

            Element layout = document.createElement("layout");
            layout.appendChild(document.createTextNode("p2"));
            repository.appendChild(layout);

            Element url = document.createElement("url");
            url.appendChild(document.createTextNode(repo[i]));

            repository.appendChild(url);
            repositories.appendChild(repository);
        }
        output(root, path);
        if (outOrno) {
            output(root, null);
        }

    } catch (SAXException e) {
    } catch (IOException e) {
    } catch (ParserConfigurationException e) {
    }
}

From source file:com.msopentech.odatajclient.engine.data.json.GeospatialJSONHandler.java

private static void appendPoses(final Element parent, final Document document, final Iterator<JsonNode> itor) {
    while (itor.hasNext()) {
        final Iterator<JsonNode> lineItor = itor.next().elements();
        final Element pos = document.createElementNS(ODataConstants.NS_GML, ODataConstants.ELEM_POS);
        parent.appendChild(pos);//  w w w.  j av a  2s  .co m
        pos.appendChild(document.createTextNode(lineItor.next().asText() + " " + lineItor.next().asText()));
    }
}

From source file:at.gv.egovernment.moa.id.util.client.mis.simple.MISSimpleClient.java

public static MISSessionId sendSessionIdRequest(String webServiceURL, byte[] idl, byte[] cert,
        String oaFriendlyName, String redirectURL, String referenceValue, List<String> mandateIdentifier,
        String targetType, byte[] authBlock, SSLSocketFactory sSLSocketFactory)
        throws MISSimpleClientException {
    if (webServiceURL == null) {
        throw new MISSimpleClientException("service.04");
    }// w  w  w . j  a v a 2 s . c  om
    if (idl == null) {
        throw new NullPointerException("Argument idl must not be null.");
    }
    if (redirectURL == null) {
        throw new NullPointerException("Argument redirectURL must not be null.");
    }

    // ssl settings
    if (sSLSocketFactory != null) {
        SZRGWSecureSocketFactory fac = new SZRGWSecureSocketFactory(sSLSocketFactory);
        Protocol.registerProtocol("https", new Protocol("https", fac, 443));
    }

    try {
        Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
        Element mirElement = doc.createElementNS(MIS_NS, "MandateIssueRequest");
        Element idlElement = doc.createElementNS(MIS_NS, "IdentityLink");

        idlElement.appendChild(doc.createTextNode(new String(Base64.encodeBase64(idl))));
        mirElement.appendChild(idlElement);

        if (cert != null && cert.length > 0) {
            Element certElement = doc.createElementNS(MIS_NS, "X509SignatureCertificate");
            certElement.appendChild(doc.createTextNode(new String(Base64.encodeBase64(cert))));
            //certElement.appendChild(doc.createTextNode(Base64.encodeBase64(cert)));
            //          certElement.appendChild(doc.createTextNode(new String(Base64.encodeBase64(cert))));
            mirElement.appendChild(certElement);
        }

        if (!StringUtils.isEmpty(oaFriendlyName)) {
            Element oaFriendlyNameElement = doc.createElementNS(MIS_NS, "OAFriendlyName");
            oaFriendlyNameElement.appendChild(doc.createTextNode(oaFriendlyName));
            mirElement.appendChild(oaFriendlyNameElement);
        }

        Element redirectElement = doc.createElementNS(MIS_NS, "RedirectURL");
        redirectElement.appendChild(doc.createTextNode(redirectURL));
        mirElement.appendChild(redirectElement);

        Element referenceValueElement = doc.createElementNS(MIS_NS, "ReferenceValue");
        referenceValueElement.appendChild(doc.createTextNode(referenceValue));
        mirElement.appendChild(referenceValueElement);

        if (mandateIdentifier != null && mandateIdentifier.size() > 0) {
            Element filtersElement = doc.createElementNS(MIS_NS, "Filters");
            Element mandateIdentifiersElement = doc.createElementNS(MIS_NS, "MandateIdentifiers");
            for (int i = 0; i < mandateIdentifier.size(); i++) {
                Element mandateIdentifierElement = doc.createElementNS(MIS_NS, "MandateIdentifier");
                mandateIdentifierElement.appendChild(doc.createTextNode(mandateIdentifier.get(i)));
                mandateIdentifiersElement.appendChild(mandateIdentifierElement);
            }
            filtersElement.appendChild(mandateIdentifiersElement);
            mirElement.appendChild(filtersElement);
        }

        //add Target element
        Element targetElement = doc.createElementNS(MIS_NS, "Target");
        Element targetTypeElement = doc.createElementNS(MIS_NS, "Type");
        targetTypeElement.appendChild(doc.createTextNode(targetType));
        targetElement.appendChild(targetTypeElement);
        mirElement.appendChild(targetElement);

        //add AuthBlock element
        Element authBlockElement = doc.createElementNS(MIS_NS, "authBlock");
        authBlockElement.appendChild(doc.createTextNode(new String(Base64.encodeBase64(authBlock))));
        mirElement.appendChild(authBlockElement);

        // send soap request
        Element mandateIssueResponseElement = sendSOAPRequest(webServiceURL, mirElement);

        // check for error
        checkForError(mandateIssueResponseElement);

        // check for session id
        //String sessionId = ((Node) XPathAPI.selectSingleNode(mandateIssueResponseElement, "/mis:MandateIssueResponse/mis:SessionID/text()", NS_NODE)).getNodeValue();
        Node sessionIdNode = ((Node) XPathAPI.selectSingleNode(mandateIssueResponseElement,
                "//mis:MandateIssueResponse/mis:SessionID/text()", NS_NODE));
        if (sessionIdNode == null) {
            throw new MISSimpleClientException("SessionId not found in response.");
        }
        String sessionId = sessionIdNode.getNodeValue();

        Node guiRedirectURLNode = ((Node) XPathAPI.selectSingleNode(mandateIssueResponseElement,
                "//mis:MandateIssueResponse/mis:GuiRedirectURL/text()", NS_NODE));
        if (guiRedirectURLNode == null) {
            throw new MISSimpleClientException("GuiRedirectURL not found in response.");
        }
        String guiRedirectURL = guiRedirectURLNode.getNodeValue();

        // create return object
        MISSessionId msid = new MISSessionId();
        msid.setSessiondId(sessionId);
        msid.setRedirectURL(guiRedirectURL);

        return msid;
    } catch (ParserConfigurationException e) {
        throw new MISSimpleClientException("service.06", e);
    } catch (DOMException e) {
        throw new MISSimpleClientException("service.06", e);
    } catch (TransformerException e) {
        throw new MISSimpleClientException("service.06", e);
    }

}