Example usage for org.w3c.dom NamedNodeMap removeNamedItemNS

List of usage examples for org.w3c.dom NamedNodeMap removeNamedItemNS

Introduction

In this page you can find the example usage for org.w3c.dom NamedNodeMap removeNamedItemNS.

Prototype

public Node removeNamedItemNS(String namespaceURI, String localName) throws DOMException;

Source Link

Document

Removes a node specified by local name and namespace URI.

Usage

From source file:Main.java

public static Node removeNamedItemNS(final NamedNodeMap nodeMap, final String namespaceURI,
        final String localName) {
    return nodeMap == null ? null : nodeMap.removeNamedItemNS(namespaceURI, localName);
}

From source file:org.adl.parsers.dom.ADLDOMParser.java

/**
 * Traverses the DOM Tree and removes Ignorable Whitespace Text Nodes and
 * Comment Text.  The function also removes extension elements and
 * attributes that are not defined by SCORM.  If extensions are found
 * the function also sets a flag stating that extensions are present in the
 * input XML instance.//from w w  w.ja  va 2s  .  co m
 *
 * @param iNode The node to be pruned of whitespace and comments<br>
 * @param iXMLFileName The XML file to be pruned
 */
private void pruneTree(Node iNode, String iXMLFileName) {
    String value;

    // is there anything to do?
    if (iNode == null)
        return;

    switch (iNode.getNodeType()) {
    case Node.PROCESSING_INSTRUCTION_NODE: {
        break;
    }
    case Node.DOCUMENT_NODE: {
        pruneTree(((Document) iNode).getDocumentElement(), iXMLFileName);
        break;
    }
    case Node.ELEMENT_NODE: {
        log.debug("Processing Element Node: [" + iNode.getLocalName() + "]");
        log.debug("************************************************");
        log.debug("Processing Element Node: [" + iNode.getLocalName() + "]");

        checkForSchemaLocations(iNode, iXMLFileName);

        // Get the list of attributes of the element
        NamedNodeMap attrList = iNode.getAttributes();

        // Loop over the attributes for this element, remove any attributes
        // that are extensions
        log.debug("Processing " + attrList.getLength() + " attributes");
        for (int i = 0; i < attrList.getLength(); i++) {
            Attr currentAttribute = (Attr) attrList.item(i);

            if (!(DOMTreeUtility.isSCORMAppProfileNode(currentAttribute, iNode))) {
                log.debug("Extension attribute, removing: [" + currentAttribute.getNamespaceURI() + "] "
                        + currentAttribute.getLocalName() + " from the its parent node ["
                        + iNode.getNamespaceURI() + "] " + iNode.getLocalName());

                // Remove the Element Node from the DOM
                attrList.removeNamedItemNS(currentAttribute.getNamespaceURI(), currentAttribute.getLocalName());
                i--;
                mExtensionsFound = true;
            } else {
                log.debug("Valid SCORM attribute, keeping attribute: [" + currentAttribute.getNamespaceURI()
                        + "] " + currentAttribute.getLocalName());
            }
        }

        log.debug("Done processing attributes for node: [" + iNode.getNamespaceURI() + "] "
                + iNode.getLocalName());
        log.debug("************************************************");

        // Done looping over the attributes for this element, now loop over
        // the set of children nodes.

        log.debug("");
        log.debug("************************************************");
        log.debug("Processing direct-descendances for node: [" + iNode.getNamespaceURI() + "] "
                + iNode.getLocalName());

        NodeList children = iNode.getChildNodes();
        if (children != null) {
            // Loop over set of children elements for this element, remove
            // any elements that are extensions
            log.debug("Processing " + children.getLength() + " elements");
            for (int z = 0; z < children.getLength(); z++) {
                Node childNode = children.item(z);

                if (childNode.getNodeType() == Node.ELEMENT_NODE) {
                    log.debug("Processing element: [" + childNode + "]");
                    log.debug("Elements Namespace: [" + childNode.getNamespaceURI() + "]");
                    log.debug("Elements Parent Node: [" + iNode.getLocalName() + "]");
                    log.debug("Parent Nodes Namespace: [" + iNode.getNamespaceURI() + "]");

                    if (!(DOMTreeUtility.isSCORMAppProfileNode(children.item(z),
                            children.item(z).getParentNode()))) {
                        // Before we remove the element see if the elemen
                        // contains any xsi:schemaLocations.  We need
                        // to add them to the list of schema locations for 
                        // parsing
                        checkForSchemaLocations(childNode, iXMLFileName);

                        log.debug("Extension Element Found, removing from DOM Tree ");

                        // Remove the Element Node from the DOM
                        children.item(z).getParentNode().removeChild(children.item(z));
                        z--;
                        mExtensionsFound = true;
                    } else {
                        log.debug("ADL SCORM Element Found, leaving " + "element in DOM Tree");
                        pruneTree(children.item(z), iXMLFileName);
                    }
                } // end if NodeType == ELEMENT_NODE

                if (childNode instanceof TextImpl) {
                    value = children.item(z).getNodeValue().trim();

                    if (((TextImpl) children.item(z)).isIgnorableWhitespace()) {
                        iNode.removeChild(children.item(z));
                        z--;
                    } else if (value.length() == 0) {
                        iNode.removeChild(children.item(z));
                        z--;
                    }
                } else if (children.item(z).getNodeType() == Node.COMMENT_NODE) {
                    iNode.removeChild(children.item(z));
                    z--;
                }
            } // end looping over children nodes
        } // end if there are children

        log.debug("Done processing direct-descendants for node: [" + iNode.getNamespaceURI() + "] "
                + iNode.getLocalName());
        log.debug("**************************************************");

        break;

    }

    // handle entity reference nodes
    case Node.ENTITY_REFERENCE_NODE: {

        NodeList children = iNode.getChildNodes();
        if (children != null) {
            int len = children.getLength();
            for (int i = 0; i < len; i++) {
                pruneTree(children.item(i), iXMLFileName);
            }
        }
        break;
    }

    // text
    case Node.COMMENT_NODE: {
        break;
    }
    case Node.CDATA_SECTION_NODE: {
        break;
    }
    case Node.TEXT_NODE: {
        break;
    }
    default: {
        break;
    }
    }
}