Example usage for org.w3c.dom Element getTagName

List of usage examples for org.w3c.dom Element getTagName

Introduction

In this page you can find the example usage for org.w3c.dom Element getTagName.

Prototype

public String getTagName();

Source Link

Document

The name of the element.

Usage

From source file:com.twinsoft.convertigo.engine.util.XMLUtils.java

private static Object getValue(Element elt, boolean ignoreStepIds, boolean useType) throws JSONException {
    Object value = null;/*from w  w w . j a  v a 2  s  .com*/

    try {
        if (elt.hasAttribute("type")) {
            String type = elt.getAttribute("type");

            if (type.equals("object")) {
                JSONObject jsonObject = new JSONObject();

                NodeList nl = elt.getChildNodes();
                for (int i = 0; i < nl.getLength(); i++) {
                    Node node = nl.item(i);

                    if (node instanceof Element) {
                        Element child = (Element) node;
                        String childName = child.hasAttribute("originalKeyName")
                                ? child.getAttribute("originalKeyName")
                                : child.getTagName();
                        Object childValue = getValue(child, ignoreStepIds, useType);

                        if (childValue != null) {
                            jsonObject.put(childName, childValue);
                        } else {
                            handleElement(child, jsonObject, ignoreStepIds, useType);
                        }
                    }
                }
                value = jsonObject;
            } else if (type.equals("array")) {
                JSONArray array = new JSONArray();

                NodeList nl = elt.getChildNodes();
                for (int i = 0; i < nl.getLength(); i++) {
                    Node node = nl.item(i);

                    if (node instanceof Element) {
                        Element child = (Element) node;
                        Object childValue = getValue(child, ignoreStepIds, useType);

                        if (childValue != null) {
                            array.put(childValue);
                        } else {
                            JSONObject obj = new JSONObject();
                            array.put(obj);
                            handleElement(child, obj, ignoreStepIds, useType);
                        }
                    }
                }

                value = array;
            } else if (type.equals("string")) {
                value = elt.getTextContent();
            } else if (type.equals("boolean")) {
                value = Boolean.parseBoolean(elt.getTextContent());
            } else if (type.equals("null")) {
                value = JSONObject.NULL;
            } else if (type.equals("integer")) {
                value = Integer.parseInt(elt.getTextContent());
            } else if (type.equals("long")) {
                value = Long.parseLong(elt.getTextContent());
            } else if (type.equals("double")) {
                value = Double.parseDouble(elt.getTextContent());
            } else if (type.equals("float")) {
                value = Float.parseFloat(elt.getTextContent());
            }

            if (value != null) {
                elt.removeAttribute(type);
            }
        }
    } catch (Throwable t) {
        Engine.logEngine.debug("failed to convert the element " + elt.getTagName(), t);
    }

    return value;
}

From source file:edu.lternet.pasta.client.EventSubscriptionClient.java

public String subscriptionTableHTML() throws PastaEventException {
    String html = "";

    if (this.uid != null && !this.uid.equals("public")) {
        StringBuilder sb = new StringBuilder("");
        String xmlString = readByFilter("");

        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        try {//from  w ww .j av a  2s. co  m
            DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
            InputStream inputStream = IOUtils.toInputStream(xmlString, "UTF-8");
            Document document = documentBuilder.parse(inputStream);
            Element documentElement = document.getDocumentElement();
            NodeList subscriptionList = documentElement.getElementsByTagName("subscription");
            int nSubscriptions = subscriptionList.getLength();

            for (int i = 0; i < nSubscriptions; i++) {
                Node subscriptionNode = subscriptionList.item(i);
                NodeList subscriptionChildren = subscriptionNode.getChildNodes();
                String subscriptionId = "";
                String packageId = "";
                String url = "";
                for (int j = 0; j < subscriptionChildren.getLength(); j++) {
                    Node childNode = subscriptionChildren.item(j);
                    if (childNode instanceof Element) {
                        Element subscriptionElement = (Element) childNode;

                        if (subscriptionElement.getTagName().equals("id")) {
                            Text text = (Text) subscriptionElement.getFirstChild();
                            if (text != null) {
                                subscriptionId = text.getData().trim();
                            }
                        } else if (subscriptionElement.getTagName().equals("packageId")) {
                            Text text = (Text) subscriptionElement.getFirstChild();
                            if (text != null) {
                                packageId = text.getData().trim();
                            }
                        } else if (subscriptionElement.getTagName().equals("url")) {
                            Text text = (Text) subscriptionElement.getFirstChild();
                            if (text != null) {
                                url = text.getData().trim();
                            }
                        }
                    }
                }

                sb.append("<tr>\n");

                sb.append("<td class='nis' align='center'>");
                sb.append(subscriptionId);
                sb.append("</td>\n");

                sb.append("<td class='nis' align='center'>");
                sb.append(packageId);
                sb.append("</td>\n");

                sb.append("<td class='nis'>");
                sb.append(url);
                sb.append("</td>\n");

                sb.append("</tr>\n");
            }

            html = sb.toString();
        } catch (Exception e) {
            logger.error("Exception:\n" + e.getMessage());
            e.printStackTrace();
            throw new PastaEventException(e.getMessage());
        }
    }

    return html;
}

From source file:org.opentraces.metatracker.net.OpenTracesClient.java

/**
 * Do XML over HTTP request and retun response.
 *///w  w w . j a v  a2  s  .c  o  m
private Element doRequest(Element anElement) throws ClientException {

    // Create URL to use
    String url = protocolURL;
    if (agentKey != null) {
        url = url + "?agentkey=" + agentKey;
    } else {
        // Must be login
        url = url + "?timeout=" + timeout;
    }
    p("doRequest: " + url + " req=" + anElement.getTagName());

    // Perform request/response
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httpPost = new HttpPost(url);

    Element replyElement = null;

    try {
        // Make sure the server knows what kind of a response we will accept
        httpPost.addHeader("Accept", "text/xml");

        // Also be sure to tell the server what kind of content we are sending
        httpPost.addHeader("Content-Type", "application/xml");

        String xmlString = DOMUtil.dom2String(anElement.getOwnerDocument());

        StringEntity entity = new StringEntity(xmlString, "UTF-8");
        entity.setContentType("application/xml");
        httpPost.setEntity(entity);

        // Execute HTTP Post Request
        HttpResponse response = httpclient.execute(httpPost);

        // Parse response
        Document replyDoc = DOMUtil.parse(response.getEntity().getContent());
        replyElement = replyDoc.getDocumentElement();
        p("doRequest: rsp=" + replyElement.getTagName());
    } catch (Throwable t) {
        throw new ClientException("Error in doRequest: " + t);
    } finally {

    }

    return replyElement;

}

From source file:com.enonic.vertical.adminweb.handlers.xmlbuilders.SimpleContentXMLBuilder.java

public String getContentTitle(Element contentDataElem, int contentTypeKey) {
    Document ctDoc = XMLTool.domparse(admin.getContentType(contentTypeKey));
    Element ctElem = XMLTool.getElement(ctDoc.getDocumentElement(), "contenttype");
    Element moduleDataElem = XMLTool.getElement(ctElem, "moduledata");
    Element moduleElem = XMLTool.getElement(moduleDataElem, "config");

    // find title xpath
    Element formElem = XMLTool.getElement(moduleElem, "form");
    Element titleElem = XMLTool.getElement(formElem, "title");
    String titleFieldName = titleElem.getAttribute("name");
    String titleXPath = null;//  w ww .j a  va  2 s  . c  o m

    Node[] nodes = XMLTool.filterNodes(formElem.getChildNodes(), Node.ELEMENT_NODE);
    for (int i = 0; i < nodes.length && titleXPath == null; ++i) {
        Element elem = (Element) nodes[i];
        if (elem.getTagName().equals("block")) {
            Node[] inputNodes = XMLTool.filterNodes(elem.getChildNodes(), Node.ELEMENT_NODE);
            for (Node inputNode : inputNodes) {
                if (titleFieldName.equals(((Element) inputNode).getAttribute("name"))) {
                    titleXPath = XMLTool.getElementText(XMLTool.getElement((Element) inputNode, "xpath"));
                    break;
                }
            }
        }
    }

    return XMLTool.getElementText((Element) contentDataElem.getParentNode(), titleXPath);
}

From source file:com.bstek.dorado.data.config.xml.DataParseContext.java

/**
 * ?????//  ww  w .java  2s .  c om
 */
public String getPrivateNameSection(Node node) {
    String section = null;
    if (node instanceof Element) {
        Element element = (Element) node;
        String keyValue = element.getAttribute(XmlConstants.ATTRIBUTE_ID);
        if (StringUtils.isEmpty(keyValue)) {
            keyValue = element.getAttribute(XmlConstants.ATTRIBUTE_NAME);
        }

        if (StringUtils.isNotEmpty(keyValue)) {
            section = DataXmlConstants.PATH_SUB_OBJECT_PREFIX + keyValue;
        } else {
            section = DataXmlConstants.PATH_PROPERTY_PREFIX + element.getTagName();
        }
    } else {
        section = DataXmlConstants.PATH_PROPERTY_PREFIX + node.getNodeName();
    }
    return section;
}

From source file:com.icesoft.faces.context.DOMResponseWriter.java

private void enhanceAndFixDocument() {
    Element html = (Element) document.getDocumentElement();
    enhanceHtml(html = "html".equals(html.getTagName()) ? html : fixHtml());

    Element head = (Element) document.getElementsByTagName("head").item(0);
    enhanceHead(head == null ? fixHead() : head);

    Element body = (Element) document.getElementsByTagName("body").item(0);
    enhanceBody(body == null ? fixBody() : body);
}

From source file:betullam.xmlmodifier.XMLmodifier.java

private boolean hasDuplicate(Element parentElement, String elementName, String attrName, String attrValue,
        String textContent) {//from  ww  w  .  j  a  v  a  2 s.  c  om
    boolean hasDuplicate = false;
    NodeList childNodes = parentElement.getChildNodes();
    if (childNodes.getLength() > 0) {
        for (int i = 0; i < childNodes.getLength(); i++) {
            if (childNodes.item(i).getNodeType() == org.w3c.dom.Node.ELEMENT_NODE) {
                Element childElement = (Element) childNodes.item(i);

                // Get name of child element:
                String childElementName = childElement.getTagName();

                // Check if the element with the given element name exists
                if (childElementName.equals(elementName)) {
                    boolean elementExists = true;
                    if (elementExists) { // The given element exists

                        // Check if given text content exists
                        String childElementTextContent = childElement.getTextContent().trim();
                        boolean textContentExists = (childElementTextContent.equals(textContent)) ? true
                                : false;

                        // If attribute value and name are null, we don't check for them (in this case, only the text content is relevant).
                        if (attrName.equals("null") && attrValue.equals("null")) {
                            if (textContentExists) { // Element exists with the given text content. 
                                hasDuplicate = true; // The new element would be a duplicate
                            } else { // Element exists but not with the given text content.
                                hasDuplicate = false; // The new element wouldn't be a duplicate
                            }
                        } else { // If attribute value and name are not null, check if they are the same as the given value.

                            // Check if child element has the given attribute
                            boolean elementHasAttr = childElement.hasAttribute(attrName);

                            if (elementHasAttr) { // The given element has the given attribute
                                // Check if the attribute has the given value
                                String childElementAttrValue = childElement.getAttribute(attrName);
                                if (childElementAttrValue.equals(attrValue)) {
                                    if (textContentExists) { // Element exists with the given text content. 
                                        hasDuplicate = true; // The attribute contains the given attribute value, so the new element would be a duplicate.
                                    } else { // Element exists but not with the given text content.
                                        hasDuplicate = false; // The new element wouldn't be a duplicate
                                    }
                                } else {
                                    hasDuplicate = false; // The attribute does not contain the given attribute value, so the new element would not be a duplicate.
                                }
                            } else {
                                hasDuplicate = false; // The attribute does not exist, so the new element would not be a duplicate.
                            }
                        }
                    }
                }
            }
        }
    }

    return hasDuplicate;
}

From source file:marytts.tools.analysis.CopySynthesis.java

private void updateDurationAndEndTime(Element e, Label label, double prevEndTime) {
    String PHONE = "ph";
    String A_PHONE_DURATION = "d";
    String A_PHONE_SYMBOL = "p";
    String A_PHONE_END = "end";
    String BOUNDARY = "boundary";
    String A_BOUNDARY_DURATION = "duration";

    assert label.time > prevEndTime;
    double durationSeconds = label.time - prevEndTime;
    String durationMillisString = String.valueOf((int) Math.round(1000 * durationSeconds));
    // System.out.println("For label "+label+", setting duration "+durationSeconds+" -> "+durationMillisString);
    if (e.getTagName().equals(PHONE)) {
        assert label.phn.equals(e.getAttribute(A_PHONE_SYMBOL));
        e.setAttribute(A_PHONE_DURATION, durationMillisString);
        e.setAttribute(A_PHONE_END, String.valueOf(label.time));
    } else {//from  w ww  .  j  av  a2 s .  co m
        assert e.getTagName().equals(BOUNDARY);
        e.setAttribute(A_BOUNDARY_DURATION, durationMillisString);
    }

}

From source file:fr.gouv.finances.dgfip.xemelios.utils.TextWriter.java

private void element(Writer writer, Element elem) throws IOException {
    if (prettyPrint) {
        checkTextBuffer(writer);/*  ww w  .j av  a2 s .  co m*/
        indent(writer);
    }

    String n = elem.getTagName();
    writer.write('<');
    writeText(writer, n);

    NamedNodeMap a = elem.getAttributes();
    int size = a.getLength();
    for (int i = 0; i < size; i++) {
        Attr att = (Attr) a.item(i);
        writer.write(' ');
        writeNode(writer, att);
    }

    if (elem.hasChildNodes()) {
        writer.write('>');

        if (prettyPrint) {
            String text = getChildText(elem);
            if (text != null)
                writeEscapedText(writer, normalizeString(text), false);
            else {
                writer.write('\n');
                indentLevel++;
                writeChildren(writer, elem);
                checkTextBuffer(writer);
                indentLevel--;
                indent(writer);
            }
        } else
            writeChildren(writer, elem);

        writer.write("</");
        writeText(writer, n);
        writer.write('>');
    } else {
        if (produceHTML)
            writer.write(">");
        else
            writer.write("/>");
    }

    if (prettyPrint)
        writer.write('\n');
}

From source file:com.twinsoft.convertigo.engine.util.XMLUtils.java

public static String XmlToJson(Element elt, boolean ignoreStepIds, boolean useType, JsonRoot jsonRoot)
        throws JSONException {
    JSONObject json = new JSONObject();
    handleElement(elt, json, ignoreStepIds, useType);
    String jsonString = json.toString(1);
    if (jsonRoot != null && !jsonRoot.equals(JsonRoot.docNode)) {
        JSONObject jso = new JSONObject(jsonString).getJSONObject(elt.getTagName());
        if (jsonRoot.equals(JsonRoot.docChildNodes)) {
            jso.remove("attr");
        }//  www . jav  a2  s .  c  om
        jsonString = jso.toString(1);
    }
    return jsonString;
}