Example usage for org.w3c.dom Element getNodeName

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

Introduction

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

Prototype

public String getNodeName();

Source Link

Document

The name of this node, depending on its type; see the table above.

Usage

From source file:com.msopentech.odatajclient.engine.data.atom.AtomDeserializer.java

public static AtomEntry entry(final Element input) {
    if (!ODataConstants.ATOM_ELEM_ENTRY.equals(input.getNodeName())) {
        return null;
    }/*w  w  w  . j  a va  2 s  . c  o  m*/

    final AtomEntry entry = new AtomEntry();

    common(input, entry);

    final String etag = input.getAttribute(ODataConstants.ATOM_ATTR_ETAG);
    if (StringUtils.isNotBlank(etag)) {
        entry.setETag(etag);
    }

    final List<Element> categories = XMLUtils.getChildElements(input, ODataConstants.ATOM_ELEM_CATEGORY);
    if (!categories.isEmpty()) {
        entry.setType(categories.get(0).getAttribute(ODataConstants.ATOM_ATTR_TERM));
    }

    final List<Element> links = XMLUtils.getChildElements(input, ODataConstants.ATOM_ELEM_LINK);
    for (Element linkElem : links) {
        final AtomLink link = new AtomLink();
        link.setRel(linkElem.getAttribute(ODataConstants.ATTR_REL));
        link.setTitle(linkElem.getAttribute(ODataConstants.ATTR_TITLE));
        link.setHref(linkElem.getAttribute(ODataConstants.ATTR_HREF));

        if (ODataConstants.SELF_LINK_REL.equals(link.getRel())) {
            entry.setSelfLink(link);
        } else if (ODataConstants.EDIT_LINK_REL.equals(link.getRel())) {
            entry.setEditLink(link);
        } else if (link.getRel().startsWith(ODataConstants.NAVIGATION_LINK_REL)) {
            link.setType(linkElem.getAttribute(ODataConstants.ATTR_TYPE));
            entry.addNavigationLink(link);

            final List<Element> inlines = XMLUtils.getChildElements(linkElem, ODataConstants.ATOM_ELEM_INLINE);
            if (!inlines.isEmpty()) {
                final List<Element> entries = XMLUtils.getChildElements(inlines.get(0),
                        ODataConstants.ATOM_ELEM_ENTRY);
                if (!entries.isEmpty()) {
                    link.setInlineEntry(entry(entries.get(0)));
                }

                final List<Element> feeds = XMLUtils.getChildElements(inlines.get(0),
                        ODataConstants.ATOM_ELEM_FEED);
                if (!feeds.isEmpty()) {
                    link.setInlineFeed(feed(feeds.get(0)));
                }
            }
        } else if (link.getRel().startsWith(ODataConstants.ASSOCIATION_LINK_REL)) {
            entry.addAssociationLink(link);
        } else if (link.getRel().startsWith(ODataConstants.MEDIA_EDIT_LINK_REL)) {
            entry.addMediaEditLink(link);
        }
    }

    final List<Element> authors = XMLUtils.getChildElements(input, ODataConstants.ATOM_ELEM_AUTHOR);
    if (!authors.isEmpty()) {
        final AtomEntry.Author author = new AtomEntry.Author();
        for (Node child : XMLUtils.getChildNodes(input, Node.ELEMENT_NODE)) {
            if (ODataConstants.ATOM_ELEM_AUTHOR_NAME.equals(XMLUtils.getSimpleName(child))) {
                author.setName(child.getTextContent());
            } else if (ODataConstants.ATOM_ELEM_AUTHOR_URI.equals(XMLUtils.getSimpleName(child))) {
                author.setUri(child.getTextContent());
            } else if (ODataConstants.ATOM_ELEM_AUTHOR_EMAIL.equals(XMLUtils.getSimpleName(child))) {
                author.setEmail(child.getTextContent());
            }
        }
        if (!author.isEmpty()) {
            entry.setAuthor(author);
        }
    }

    final List<Element> actions = XMLUtils.getChildElements(input, ODataConstants.ATOM_ELEM_ACTION);
    for (Element action : actions) {
        final ODataOperation operation = new ODataOperation();
        operation.setMetadataAnchor(action.getAttribute(ODataConstants.ATTR_METADATA));
        operation.setTitle(action.getAttribute(ODataConstants.ATTR_TITLE));
        operation.setTarget(URI.create(action.getAttribute(ODataConstants.ATTR_TARGET)));

        entry.addOperation(operation);
    }

    final List<Element> contents = XMLUtils.getChildElements(input, ODataConstants.ATOM_ELEM_CONTENT);
    if (!contents.isEmpty()) {
        final Element content = contents.get(0);

        List<Element> props = XMLUtils.getChildElements(content, ODataConstants.ELEM_PROPERTIES);
        if (props.isEmpty()) {
            entry.setMediaContentSource(content.getAttribute(ODataConstants.ATOM_ATTR_SRC));
            entry.setMediaContentType(content.getAttribute(ODataConstants.ATTR_TYPE));

            props = XMLUtils.getChildElements(input, ODataConstants.ELEM_PROPERTIES);
            if (!props.isEmpty()) {
                entry.setMediaEntryProperties(props.get(0));
            }
        } else {
            entry.setContent(props.get(0));
        }
    }

    return entry;
}

From source file:uk.ac.kcl.Transform23.java

public static void cleanFieldNames(Node node) {
    // do something with the current node instead of System.out
    //System.out.println(node.getNodeName());

    NodeList nodeList = node.getChildNodes();
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node currentNode = nodeList.item(i);
        if (currentNode.getNodeType() == Node.ELEMENT_NODE) {
            //calls this method for all the children which is Element
            Element el = (Element) nodeList.item(i);
            //System.out.println(el.getNodeName());
            //System.out.println(nodeList.getLength());
            if (el.getNodeName().contains(".")) {
                el.getOwnerDocument().renameNode(nodeList.item(i), null, el.getNodeName().replace(".", "_"));
            }/*from ww  w  . j  av  a  2  s .  c  o m*/
            cleanFieldNames(currentNode);
        }
    }
}

From source file:Main.java

public static String logElement(Element El, int level) {

    String Es = "";
    String indentText = "  ";
    String addIndT = "";

    // add indent
    int ind = 0;// w  w  w.  j a  v a 2s  .c om
    while (ind < level) {
        addIndT = addIndT + indentText;
        ind++;

    }
    String name = El.getNodeName();
    Es = "\n" + addIndT + "<" + name + " ";
    // Attribs
    NamedNodeMap namedNodeMap = El.getAttributes();
    StringBuilder sb = new StringBuilder(Es);
    for (int i = 0; i < namedNodeMap.getLength(); i++) {
        Attr att = (Attr) namedNodeMap.item(i);
        sb.append(" " + Es + att.getName() + "=\"" + att.getNodeValue() + "\" ");
        // Es = " " + Es + att.getName() + "=\"" + att.getNodeValue() +
        // "\" ";
    }
    sb.append(">");
    // Es = Es + ">";
    Es = sb.toString();
    NodeList pl = El.getChildNodes();
    int index = pl.getLength();
    // text nodes
    if (index > 0) {
        int i = 0;
        while (i < index) {
            Node DomNode = pl.item(i);
            if ((DomNode.getNodeType()) == org.w3c.dom.Node.TEXT_NODE) {
                String Etext = DomNode.getNodeValue();
                Es = Es + "\n " + addIndT + addIndT + Etext;
            }
            i++;
        }
    }
    // Child Elements
    if (index > 0) {
        level++;
        int i = 0;
        while (i < index) {
            Node DomNode = pl.item(i);
            if ((DomNode.getNodeType()) == org.w3c.dom.Node.ELEMENT_NODE) {
                Element el = (Element) DomNode;
                Es = Es + logElement(el, level);
            }
            i++;
        }
    }
    Es = Es + "\n" + addIndT + "</" + name + ">";

    return Es;
}

From source file:net.vexelon.bgrates.Utils.java

/**
 * Serialize an XML element recursively/*w  ww  . ja  v a2 s .c o  m*/
 * @param node
 * @param serializer
 * @throws IOException
 */
private static void serializeXmlElement(Node node, XmlSerializer serializer) throws IOException {

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

        if (current.getNodeType() == Node.ELEMENT_NODE) {
            Element child = (Element) current;
            serializer.startTag("", child.getNodeName());
            serializeXmlElement(child, serializer);
            serializer.endTag("", child.getNodeName());
        } else if (current.getNodeType() == Node.TEXT_NODE) {
            Text child = (Text) current;
            serializer.text(child.getData());
        } else if (current.getNodeType() == Node.CDATA_SECTION_NODE) {
            CDATASection child = (CDATASection) current;
            serializer.cdsect(child.getData());
        } else if (current.getNodeType() == Node.COMMENT_NODE) {
            Comment child = (Comment) current;
            serializer.comment(child.getData());
        }
    }
}

From source file:Main.java

public static String logElement(final Element elem, final int level) {

    String estr = "";
    String indentText = "  ";
    String addIndT = "";

    // add indent
    int ind = 0;//from www.  j a  va  2 s. c o  m
    while (ind < level) {
        addIndT = addIndT + indentText;
        ind++;

    }
    String name = elem.getNodeName();
    estr = "\n" + addIndT + "<" + name + " ";
    // Attribs
    NamedNodeMap namedNodeMap = elem.getAttributes();
    StringBuilder sb = new StringBuilder(estr);
    for (int i = 0; i < namedNodeMap.getLength(); i++) {
        Attr att = (Attr) namedNodeMap.item(i);
        sb.append(" " + estr + att.getName() + "=\"" + att.getNodeValue() + "\" ");
    }
    sb.append(">");
    estr = sb.toString();
    NodeList pl = elem.getChildNodes();
    int index = pl.getLength();
    // text nodes
    if (index > 0) {
        int i = 0;
        while (i < index) {
            Node domNode = pl.item(i);
            if ((domNode.getNodeType()) == org.w3c.dom.Node.TEXT_NODE) {
                String Etext = domNode.getNodeValue();
                estr = estr + "\n " + addIndT + addIndT + Etext;
            }
            i++;
        }
    }
    // Child Elements
    if (index > 0) {
        int i = 0;
        while (i < index) {
            Node domNode = pl.item(i);
            if ((domNode.getNodeType()) == org.w3c.dom.Node.ELEMENT_NODE) {
                Element el = (Element) domNode;
                estr = estr + logElement(el, level + 1);
            }
            i++;
        }
    }
    estr = estr + "\n" + addIndT + "</" + name + ">";

    return estr;
}

From source file:Main.java

public static Element getSingleOptionalChildElement(Element parent, Enum<?> child) {
    final String elName = getXmlName(child);
    final NodeList list = parent.getElementsByTagName(elName);
    if (list.getLength() == 1) {
        return (Element) list.item(0);
    }//  w w  w  . j a v a  2s.c om
    if (list.getLength() == 0) {
        return null;
    }
    throw new IllegalArgumentException("expected a single " + elName + " child element of XML element "
            + parent.getNodeName() + ", but found " + list.getLength());
}

From source file:fr.aliasource.webmail.server.proxy.client.http.DOMUtils.java

public static String getElementText(Element root, String elementName) {
    NodeList list = root.getElementsByTagName(elementName);
    if (list.getLength() == 0) {
        if (logger.isDebugEnabled()) {
            logger.debug("No element named '" + elementName + "' under '" + root.getNodeName() + "'");
        }//ww w  .  j  a  va  2  s.  co m
        return null;
    }
    return getElementText((Element) list.item(0));
}

From source file:Main.java

/**
 * Returns the element name of the given element.
 * @param element//from   w ww . j  a v a2  s  .co  m
 * @return String
 */
public static String getElementName(Element element) {
    // When loading from disk the local name will be setup correctly, but if the local name
    // is fetched directly after calling Document.createElement() then the value will be null.
    // See the JavaDoc for more info.  To workaround this, use the complete node name.
    String elemName = element.getLocalName();
    if (elemName == null) {
        elemName = element.getNodeName();
    }
    return elemName;
}

From source file:org.dataone.proto.trove.mn.http.client.HttpExceptionHandler.java

private static ErrorElements deserializeXml(HttpResponse response) throws IllegalStateException, IOException //    throws NotFound, InvalidToken, ServiceFailure, NotAuthorized,
//    NotFound, IdentifierNotUnique, UnsupportedType,
//    InsufficientResources, InvalidSystemMetadata, NotImplemented,
//    InvalidCredentials, InvalidRequest, IOException {
{
    ErrorElements ee = new ErrorElements();

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    Document doc;/*from  w  w  w. ja v a2  s . co m*/

    int httpCode = response.getStatusLine().getStatusCode();
    if (response.getEntity() != null) {
        BufferedInputStream bErrorStream = new BufferedInputStream(response.getEntity().getContent());
        bErrorStream.mark(5000); // good for resetting up to 5000 bytes

        String detailCode = null;
        String description = null;
        String name = null;
        int errorCode = -1;
        try {
            DocumentBuilder db = dbf.newDocumentBuilder();
            doc = db.parse(bErrorStream);
            Element root = doc.getDocumentElement();
            root.normalize();
            if (root.getNodeName().equalsIgnoreCase("error")) {
                if (root.hasAttribute("errorCode")) {
                    try {
                        errorCode = Integer.getInteger(root.getAttribute("errorCode"));
                    } catch (NumberFormatException nfe) {
                        System.out.println("errorCode unexpectedly not able to parse to int,"
                                + " using http status for creating exception");
                        errorCode = httpCode;
                    }
                }
                if (errorCode != httpCode) //            throw new ServiceFailure("1000","errorCode in message body doesn't match httpStatus");
                {
                    System.out.println("errorCode in message body doesn't match httpStatus,"
                            + " using errorCode for creating exception");
                }
                if (root.hasAttribute("detailCode")) {
                    detailCode = root.getAttribute("detailCode");
                } else {
                    detailCode = "detail code is Not Set!";
                }
                if (root.hasAttribute("name")) {
                    name = root.getAttribute("name");
                } else {
                    name = "Exception";
                }
                Node child = root.getFirstChild();
                do {
                    if (child.getNodeType() == Node.ELEMENT_NODE) {
                        if (child.getNodeName().equalsIgnoreCase("description")) {
                            Element element = (Element) child;
                            description = element.getTextContent();
                            break;
                        }
                    }
                } while ((child = child.getNextSibling()) != null);
            } else {
                description = domToString(doc);
                detailCode = "detail code was never Set!";
            }
        } catch (TransformerException e) {
            description = deserializeNonXMLErrorStream(bErrorStream, e);
        } catch (SAXException e) {
            description = deserializeNonXMLErrorStream(bErrorStream, e);
        } catch (IOException e) {
            description = deserializeNonXMLErrorStream(bErrorStream, e);
        } catch (ParserConfigurationException e) {
            description = deserializeNonXMLErrorStream(bErrorStream, e);
        }

        ee.setCode(errorCode);
        ee.setName(name);
        ee.setDetailCode(detailCode);
        ee.setDescription(description);
    }
    return ee;
}

From source file:com.netsteadfast.greenstep.util.BusinessProcessManagementUtils.java

public static String getResourceProcessId(File activitiBpmnFile) throws Exception {
    if (null == activitiBpmnFile || !activitiBpmnFile.exists()) {
        throw new Exception("file no exists!");
    }//from  ww  w .j  av a  2 s  .c om
    if (!activitiBpmnFile.getName().endsWith(_SUB_NAME)) {
        throw new Exception("not resource file.");
    }
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document doc = db.parse(activitiBpmnFile);
    Element element = doc.getDocumentElement();
    if (!"definitions".equals(element.getNodeName())) {
        throw new Exception("not resource file.");
    }
    String processId = activitiBpmnFile.getName();
    if (processId.endsWith(_SUB_NAME)) {
        processId = processId.substring(0, processId.length() - _SUB_NAME.length());
    }
    NodeList nodes = element.getChildNodes();
    for (int i = 0; i < nodes.getLength(); i++) {
        Node node = nodes.item(i);
        if ("process".equals(node.getNodeName())) {
            NamedNodeMap nodeMap = node.getAttributes();
            for (int attr = 0; attr < nodeMap.getLength(); attr++) {
                Node processAttr = nodeMap.item(attr);
                if ("id".equals(processAttr.getNodeName())) {
                    processId = processAttr.getNodeValue();
                }
            }
        }
    }
    return processId;
}