FileDocCategorySizeDatePackage
DOMUtils.javaAPI DocApache Axis 1.48389Sat Apr 22 18:56:52 BST 2006samples.addr

DOMUtils

public class DOMUtils extends Object
author
Matthew J. Duftler
author
Sanjiva Weerawarana

Fields Summary
private static String
NS_URI_XMLNS
The namespaceURI represented by the prefix xmlns.
Constructors Summary
Methods Summary
public static intcountKids(org.w3c.dom.Element elem, short nodeType)
Count number of children of a certain type of the given element.

param
elem the element whose kids are to be counted
return
the number of matching kids.

        int nkids = 0;
        for (Node n = elem.getFirstChild (); n != null; n = n.getNextSibling ()) {
            if (n.getNodeType () == nodeType) {
                nkids++;
            }
        }
        return nkids;
    
public static org.w3c.dom.ElementfindChildElementWithAttribute(org.w3c.dom.Element elem, java.lang.String attrName, java.lang.String attrValue)
Return the first child element of the given element which has the given attribute with the given value.

param
elem the element whose children are to be searched
param
attrName the attrib that must be present
param
attrValue the desired value of the attribute
return
the first matching child element.

        for (Node n = elem.getFirstChild (); n != null; n = n.getNextSibling ()) {
            if (n.getNodeType () == Node.ELEMENT_NODE) {
                if (attrValue.equals (DOMUtils.getAttribute ((Element) n, attrName))) {
                    return (Element) n;
                }
            }
        }
        return  null;
    
public static java.lang.StringgetAttribute(org.w3c.dom.Element el, java.lang.String attrName)
Returns the value of an attribute of an element. Returns null if the attribute is not found (whereas Element.getAttribute returns "" if an attrib is not found).

param
el Element whose attrib is looked for
param
attrName name of attribute to look for
return
the attribute value

    
                                                              
            
        String sRet = null;
        Attr   attr = el.getAttributeNode(attrName);
        
        if (attr != null) {
            sRet = attr.getValue();
        }
        return sRet;
    
public static java.lang.StringgetAttributeNS(org.w3c.dom.Element el, java.lang.String namespaceURI, java.lang.String localPart)
Returns the value of an attribute of an element. Returns null if the attribute is not found (whereas Element.getAttributeNS returns "" if an attrib is not found).

param
el Element whose attrib is looked for
param
namespaceURI namespace URI of attribute to look for
param
localPart local part of attribute to look for
return
the attribute value

        String sRet = null;
        Attr   attr = el.getAttributeNodeNS (namespaceURI, localPart);
        
        if (attr != null) {
            sRet = attr.getValue ();
        }
        
        return sRet;
    
public static java.lang.StringgetChildCharacterData(org.w3c.dom.Element parentEl)
Concat all the text and cdata node children of this elem and return the resulting text.

param
parentEl the element whose cdata/text node values are to be combined.
return
the concatanated string.

        if (parentEl == null) {
            return null;
        }
        Node          tempNode = parentEl.getFirstChild();
        StringBuffer  strBuf   = new StringBuffer();
        CharacterData charData;
        
        while (tempNode != null) {
            switch (tempNode.getNodeType()) {
                case Node.TEXT_NODE :
                case Node.CDATA_SECTION_NODE : charData = (CharacterData)tempNode;
                    strBuf.append(charData.getData());
                    break;
            }
            tempNode = tempNode.getNextSibling();
        }
        return strBuf.toString();
    
public static org.w3c.dom.ElementgetElementByID(org.w3c.dom.Element el, java.lang.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;
            }
        }
        
        return null;
    
public static org.w3c.dom.ElementgetFirstChildElement(org.w3c.dom.Element elem)
Return the first child element of the given element. Null if no children are found.

param
elem Element whose child is to be returned
return
the first child element.

        for (Node n = elem.getFirstChild (); n != null; n = n.getNextSibling ()) {
            if (n.getNodeType () == Node.ELEMENT_NODE) {
                return (Element) n;
            }
        }
        return null;
    
public static java.lang.StringgetNamespaceURIFromPrefix(org.w3c.dom.Node context, java.lang.String prefix)
Given a prefix and a node, return the namespace URI that the prefix has been associated with. This method is useful in resolving the namespace URI of attribute values which are being interpreted as QNames. If prefix is null, this method will return the default namespace.

param
context the starting node (looks up recursively from here)
param
prefix the prefix to find an xmlns:prefix=uri for
return
the namespace URI or null if not found

        short nodeType = context.getNodeType ();
        Node tempNode = null;
        
        switch (nodeType)
        {
            case Node.ATTRIBUTE_NODE :
                {
                    tempNode = ((Attr) context).getOwnerElement ();
                    break;
                }
            case Node.ELEMENT_NODE :
                {
                    tempNode = context;
                    break;
                }
            default :
                {
                    tempNode = context.getParentNode ();
                    break;
                }
        }
        
        while (tempNode != null && tempNode.getNodeType () == Node.ELEMENT_NODE)
        {
            Element tempEl = (Element) tempNode;
            String namespaceURI = (prefix == null)
                ? getAttribute (tempEl, "xmlns")
                : getAttributeNS (tempEl, NS_URI_XMLNS, prefix);
            
            if (namespaceURI != null)
            {
                return namespaceURI;
            }
            else
            {
                tempNode = tempEl.getParentNode ();
            }
        }
        
        return null;
    
public static org.w3c.dom.ElementgetNextSiblingElement(org.w3c.dom.Element elem)
Return the next sibling element of the given element. Null if no more sibling elements are found.

param
elem Element whose sibling element is to be returned
return
the next sibling element.

        for (Node n = elem.getNextSibling (); n != null; n = n.getNextSibling ()) {
            if (n.getNodeType () == Node.ELEMENT_NODE) {
                return (Element) n;
            }
        }
        return null;