Example usage for org.w3c.dom Element getChildNodes

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

Introduction

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

Prototype

public NodeList getChildNodes();

Source Link

Document

A NodeList that contains all children of this node.

Usage

From source file:DOMUtils.java

public static Element getElementByID(Element el, String id) {
    if (el == null)
        return null;
    String thisId = el.getAttribute("id");
    if (id.equals(thisId))
        return el;

    NodeList list = el.getChildNodes();
    for (int i = 0; i < list.getLength(); i++) {
        Node node = list.item(i);
        if (node instanceof Element) {
            Element ret = getElementByID((Element) node, id);
            if (ret != null)
                return ret;
        }//from ww w  .j  a v a  2  s. c  o  m
    }

    return null;
}

From source file:Main.java

private static void attributize(Element root) {
    NamedNodeMap attributeMap = root.getAttributes();
    for (int i = 0; i < attributeMap.getLength(); i++) {
        org.w3c.dom.Attr attr = (Attr) attributeMap.item(i);

        Element attrElement = root.getOwnerDocument().createElement(attr.getName());
        attrElement.setTextContent(attr.getValue());
        root.appendChild(attrElement);/* ww w  . j av a2  s . com*/
    }

    NodeList children = root.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        if (children.item(i) instanceof Element) {
            attributize((Element) children.item(i));
        }
    }
}

From source file:DOMEdit.java

private static void outputElement(Element node, String indent) {
    System.out.print(indent + "<" + node.getTagName());
    NamedNodeMap nm = node.getAttributes();
    for (int i = 0; i < nm.getLength(); i++) {
        Attr attr = (Attr) nm.item(i);
        System.out.print(" " + attr.getName() + "=\"" + attr.getValue() + "\"");
    }//from  ww w .  j  av  a  2  s.  c  o m
    System.out.println(">");
    NodeList list = node.getChildNodes();
    for (int i = 0; i < list.getLength(); i++)
        outputloop(list.item(i), indent + TAB);
    System.out.println(indent + "</" + node.getTagName() + ">");
}

From source file:com.impetus.kundera.ejb.PersistenceXmlLoader.java

/**
 * Get the content of the given element.
 * // w ww .  j  ava2 s  .  c o m
 * @param element
 *            The element to get the content for.
 * @param defaultStr
 *            The default to return when there is no content.
 * @return The content of the element or the default.
 * @throws Exception
 *             the exception
 */
private static String getElementContent(Element element, String defaultStr) throws Exception {
    if (element == null) {
        return defaultStr;
    }

    NodeList children = element.getChildNodes();
    StringBuilder result = new StringBuilder("");
    for (int i = 0; i < children.getLength(); i++) {
        if (children.item(i).getNodeType() == Node.TEXT_NODE
                || children.item(i).getNodeType() == Node.CDATA_SECTION_NODE) {
            result.append(children.item(i).getNodeValue());
        }
    }
    return result.toString().trim();
}

From source file:eidassaml.starterkit.EidasMetadataNode.java

/**
 * Parse an metadata.xml//  www .  java  2  s.c om
 * 
 * @param is
 * @return
 * @throws XMLParserException
 * @throws UnmarshallingException
 * @throws CertificateException
 * @throws IOException
 * @throws ErrorCodeException 
 * @throws DOMException 
 */
public static EidasMetadataNode Parse(InputStream is) throws XMLParserException, UnmarshallingException,
        CertificateException, IOException, DOMException, ErrorCodeException {
    EidasMetadataNode eidasMetadataService = new EidasMetadataNode();
    BasicParserPool ppMgr = new BasicParserPool();
    Document inCommonMDDoc = ppMgr.parse(is);
    Element metadataRoot = inCommonMDDoc.getDocumentElement();
    UnmarshallerFactory unmarshallerFactory = Configuration.getUnmarshallerFactory();
    Unmarshaller unmarshaller = unmarshallerFactory.getUnmarshaller(metadataRoot);
    EntityDescriptor metaData = (EntityDescriptor) unmarshaller.unmarshall(metadataRoot);

    eidasMetadataService.setId(metaData.getID());
    eidasMetadataService.setEntityId(metaData.getEntityID());
    eidasMetadataService.setValidUntil(metaData.getValidUntil().toDate());
    if (metaData.getExtensions() != null) {
        Element extension = metaData.getExtensions().getDOM();
        for (int i = 0; i < extension.getChildNodes().getLength(); i++) {
            Node n = extension.getChildNodes().item(i);
            if ("SPType".equals(n.getLocalName())) {
                eidasMetadataService.spType = EidasRequestSectorType.GetValueOf(n.getTextContent());
                break;
            }
        }
    }

    SPSSODescriptor ssoDescriptor = metaData.getSPSSODescriptor("urn:oasis:names:tc:SAML:2.0:protocol");

    ssoDescriptor.getAssertionConsumerServices().forEach(s -> {
        String bindString = s.getBinding();
        if ("urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST".equals(bindString)) {
            eidasMetadataService.setPostEndpoint(s.getLocation());
        }
    });

    for (KeyDescriptor k : ssoDescriptor.getKeyDescriptors()) {
        if (k.getUse() == UsageType.ENCRYPTION) {
            eidasMetadataService.encCert = GetFirstCertFromKeyDescriptor(k);
        } else if (k.getUse() == UsageType.SIGNING) {
            eidasMetadataService.sigCert = GetFirstCertFromKeyDescriptor(k);
        }
    }

    return eidasMetadataService;
}

From source file:Main.java

public static Element findSingleElement(Element parent, String fullXPath) {
    Element elt = null;/*from w  w w  .  ja va  2s  .  c o m*/
    if (parent != null) {
        if (parent.hasChildNodes()) {
            if (fullXPath.startsWith("/"))
                fullXPath = fullXPath.substring(1);
            int index = fullXPath.indexOf("/");
            String childName = ((index != -1) ? fullXPath.substring(0, index) : fullXPath);

            NodeList list = parent.getChildNodes();
            for (int i = 0; i < list.getLength(); i++) {
                Node child = list.item(i);
                if (child.getNodeType() == Node.ELEMENT_NODE) {
                    if (child.getNodeName().equalsIgnoreCase(childName)) {
                        if (index == -1) {
                            elt = (Element) child;
                            break;
                        } else {
                            fullXPath = fullXPath.substring(index + 1);
                            elt = findSingleElement((Element) child, fullXPath);
                        }
                    }
                }
            }
        }
    }
    return elt;
}

From source file:ValidateLicenseHeaders.java

/**
 * Get all non-comment content from the element.
 * /*from  w  ww .ja v  a2 s  .c om*/
 * @param element
 * @return the concatenated text/cdata content
 */
public static String getElementContent(Element element) {
    if (element == null)
        return null;

    NodeList children = element.getChildNodes();
    StringBuffer result = new StringBuffer();
    for (int i = 0; i < children.getLength(); i++) {
        Node child = children.item(i);
        if (child.getNodeType() == Node.TEXT_NODE || child.getNodeType() == Node.CDATA_SECTION_NODE) {
            result.append(child.getNodeValue());
        } else if (child.getNodeType() == Node.COMMENT_NODE) {
            // Ignore comment nodes
        } else {
            result.append(child.getFirstChild());
        }
    }
    return result.toString().trim();
}

From source file:DOMUtils.java

private static void trimEmptyTextNodes(Node node) {
    Element element = null;
    if (node instanceof Document) {
        element = ((Document) node).getDocumentElement();
    } else if (node instanceof Element) {
        element = (Element) node;
    } else {//  www. j  av a  2  s . co  m
        return;
    }

    List<Node> nodesToRemove = new ArrayList<Node>();
    NodeList children = element.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        Node child = children.item(i);
        if (child instanceof Element) {
            trimEmptyTextNodes(child);
        } else if (child instanceof Text) {
            Text t = (Text) child;
            if (t.getData().trim().length() == 0) {
                nodesToRemove.add(child);
            }
        }
    }

    for (Node n : nodesToRemove) {
        element.removeChild(n);
    }
}

From source file:it.greenvulcano.gvesb.virtual.gv_multipart.MultipartCallOperation.java

/**
 * Get the text from a CDATA/* w ww  . j  a  v a2s.com*/
 * 
 * @param e
 * @return
 */
public static String getCharacterDataFromElement(Element element) {
    NodeList list = element.getChildNodes();
    String data;

    for (int index = 0; index < list.getLength(); index++) {
        if (list.item(index) instanceof CharacterData) {
            CharacterData child = (CharacterData) list.item(index);
            data = child.getData();
            if (data != null && data.trim().length() > 0)
                return child.getData();
        }
    }
    return "";
}

From source file:DOMCopy.java

private static void outputElement(Element node, String indent) {
        System.out.print(indent + "<" + node.getTagName());
        NamedNodeMap nm = node.getAttributes();
        for (int i = 0; i < nm.getLength(); i++) {
            Attr attr = (Attr) nm.item(i);
            System.out.print(" " + attr.getName() + "=\"" + attr.getValue() + "\"");
        }//www.java2s.  c  o m
        System.out.println(">");
        NodeList list = node.getChildNodes();
        for (int i = 0; i < list.getLength(); i++)
            outputloop(list.item(i), indent + TAB);
        System.out.println(indent + "</" + node.getTagName() + ">");
    }