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:Main.java

public static NodeList findElements(Element parent, String fullXPath) {
    NodeList list = null;/*from  w w w .  j av  a 2s .c om*/
    Element elt = findSingleElement(parent, fullXPath);
    if (elt != null)
        list = ((Element) elt.getParentNode()).getElementsByTagName(elt.getNodeName());
    return list;
}

From source file:cz.incad.kramerius.rest.api.k5.client.utils.SOLRUtils.java

public static <T> List<T> array(final Element doc, final String attributeName, Class<T> clz) {
    synchronized (doc.getOwnerDocument()) {
        List<T> ret = new ArrayList<T>();
        List<Element> elms = XMLUtils.getElements(doc, new XMLUtils.ElementsFilter() {

            @Override/*from   w  w w . ja v  a2s. c  o  m*/
            public boolean acceptElement(Element element) {
                return (element.getNodeName().equals("arr") && element.hasAttribute("name")
                        && element.getAttribute("name").equals(attributeName));
            }
        });
        for (Element e : elms) {
            ret.add(value(e.getTextContent(), clz));
        }
        return ret;
    }
}

From source file:cz.incad.kramerius.rest.api.k5.client.utils.SOLRUtils.java

public static <T> T value(final Element doc, final String attributeName, Class<T> clz) {
    if (doc == null) {
        throw new IllegalArgumentException("element must not be null");
    }//from  w w w .  j a va  2  s.  c  om
    synchronized (doc.getOwnerDocument()) {
        final String expectedTypeName = SOLR_TYPE_NAMES.get(clz);
        List<Element> elms = XMLUtils.getElements(doc, new XMLUtils.ElementsFilter() {

            @Override
            public boolean acceptElement(Element element) {
                return (element.getNodeName().equals(expectedTypeName) && element.hasAttribute("name")
                        && element.getAttribute("name").equals(attributeName));
            }
        });
        Object obj = elms.isEmpty() ? null : elms.get(0).getTextContent();
        if (obj != null)
            return value(obj.toString(), clz);
        else
            return null;
    }
}

From source file:Main.java

/** Helper method - Converts an XML <I>Element</I> to a <I>String</I> object.
   @param pElement An <I>org.w3c.dom.Element</I> object to be serialized to a <I>String</I>.
   @return <I>String</I> representation of the <I>Element</I>.
 *//*w  w  w. ja v  a  2s .  c  om*/
public static String convertElementToString(Element pElement) {
    // Local variables.
    int nCount = 0;

    // Start the element.
    String strName = pElement.getNodeName();
    String strReturn = "<" + strName;

    // Get the list of attributes.
    NamedNodeMap pNodeMap = pElement.getAttributes();
    nCount = pNodeMap.getLength();

    // Build the attributes.
    for (int i = 0; i < nCount; i++) {
        Attr pAttr = (Attr) pNodeMap.item(i);
        strReturn += " " + pAttr.getNodeName() + "=\"" + pAttr.getNodeValue() + "\"";
    }

    // Get the list of children.
    NodeList pChildren = pElement.getChildNodes();
    nCount = pChildren.getLength();

    // If no children exist, return a single node.
    if (0 == nCount)
        return strReturn + " />";

    // Close node.
    strReturn += ">";

    // Build out the children nodes.
    for (int i = 0; i < nCount; i++) {
        Node pChild = pChildren.item(i);

        if (pChild instanceof CharacterData)
            strReturn += ((CharacterData) pChild).getData();
        else if (pChild instanceof Element)
            strReturn += convertElementToString((Element) pChild);
    }

    return strReturn + "</" + strName + ">";
}

From source file:Main.java

/**
 * @return the child element, throws exception if the parent does not
 *          have exactly one sub element with name
 *//*w  ww  .  java2  s .c o  m*/
public static Element getSingleSubElement(Element parentElement, String name) {
    NodeList nodes = parentElement.getElementsByTagName(name);
    if (nodes.getLength() != 1) {
        throw new IllegalArgumentException("Expected the element " + parentElement.getNodeName()
                + " to have one child called " + name + " but found " + nodes.getLength());
    }
    return (Element) nodes.item(0);
}

From source file:cz.incad.kramerius.rest.api.k5.client.utils.SOLRUtils.java

public static <T> List<T> narray(final Element doc, final String attributeName, Class<T> clz) {
    synchronized (doc.getOwnerDocument()) {
        List<T> ret = new ArrayList<T>();
        List<Element> elms = XMLUtils.getElements(doc, new XMLUtils.ElementsFilter() {
            @Override//from  w  w  w  .  ja  v a 2 s  .c o m
            public boolean acceptElement(Element element) {
                return (element.getNodeName().equals("arr") && element.hasAttribute("name")
                        && element.getAttribute("name").equals(attributeName));
            }
        });

        if (elms.size() >= 1) {
            Element parentE = elms.get(0);
            NodeList chnds = parentE.getChildNodes();
            for (int i = 0, ll = chnds.getLength(); i < ll; i++) {
                Node n = chnds.item(i);
                if (n.getNodeType() == Node.ELEMENT_NODE) {
                    ret.add(value(n.getTextContent(), clz));
                }
            }
        }
        return ret;
    }
}

From source file:Main.java

/**
 * Used for debuging//from   w w w.  jav  a2 s .  c o  m
 * 
 * @param parent
 *            Element
 * @param out
 *            PrintStream
 * @param deep
 *            boolean
 * @param prefix
 *            String
 */
public static void printChildElements(Element parent, PrintStream out, boolean deep, String prefix) {
    out.print(prefix + "<" + parent.getNodeName());
    NamedNodeMap attrs = parent.getAttributes();
    Node node;
    for (int i = 0; i < attrs.getLength(); i++) {
        node = attrs.item(i);
        out.print(" " + node.getNodeName() + "=\"" + node.getNodeValue() + "\"");
    }
    out.println(">");

    // String data = getElementTextValueDeprecated(parent);
    String data = parent.getNodeValue();
    if (data != null && data.trim().length() > 0) {
        out.println(prefix + "\t" + data);
    }

    data = getElementCDataValue(parent);
    if (data != null && data.trim().length() > 0) {
        out.println(prefix + "\t<![CDATA[" + data + "]]>");
    }

    NodeList nodes = parent.getChildNodes();
    for (int i = 0; i < nodes.getLength(); i++) {
        node = nodes.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            if (deep) {
                printChildElements((Element) node, out, deep, prefix + "\t");
            } else {
                out.println(prefix + node.getNodeName());
            }
        }
    }

    out.println(prefix + "</" + parent.getNodeName() + ">");
}

From source file:Main.java

/**
 * /*from ww w .j  a  va  2 s . c  o m*/
 * @param nodelist
 * @param name
 * @return
 */
public static Element getXMLElement(final NodeList nodelist, final String name) {
    Element element = null;
    int size = (nodelist == null) ? 0 : nodelist.getLength();
    for (int i = 0; i < size; i++) {
        element = (Element) nodelist.item(i);
        if (name.equalsIgnoreCase(element.getNodeName())) {
            break;
        }
    }
    return element;
}

From source file:com.iggroup.oss.restdoclet.plugin.util.XmlUtils.java

/**
 * Returns the child-elements of a XML node with a particular name.
 * //  w  w w .ja v  a2 s . c  o m
 * @param node the XML node.
 * @param name the name of the child-elements.
 * @return the collection of child-elements or an empty collection if no
 *         children with the name were found.
 */
public static Collection<Element> children(final Node node, final String name) {
    final Collection<Element> elements = new ArrayList<Element>();

    for (int i = 0; i < node.getChildNodes().getLength(); i++) {
        if (node.getChildNodes().item(i).getNodeType() == ELEMENT_NODE) {
            final Element child = (Element) node.getChildNodes().item(i);

            if (StringUtils.equals(name, child.getNodeName())) {
                elements.add(child);
            }
        }
    }
    return elements;
}

From source file:com.iggroup.oss.restdoclet.plugin.util.XmlUtils.java

/**
 * Returns the <i>first</i> child-element of a XML node with a particular
 * name./*from   w  w  w  .  j a va  2 s.  c om*/
 * 
 * @param node the XML node.
 * @param name the name of the child-element.
 * @return the child-element or <code>null</code> if no child with the name
 *         was found.
 */
public static Element child(final Node node, final String name) {
    Element element = null;

    for (int i = 0; i < node.getChildNodes().getLength(); i++) {
        if (node.getChildNodes().item(i).getNodeType() == ELEMENT_NODE) {
            final Element child = (Element) node.getChildNodes().item(i);

            if (StringUtils.equals(name, child.getNodeName())) {
                element = child;
                break;
            }
        }
    }
    return element;
}