Package com.ibm.commons.xml.xpath

Examples of com.ibm.commons.xml.xpath.NodeListImpl


     * @return NodeList - matching elements that are children of element.
     */
    public static NodeList getChildElementsByTagName(Element element, String tag) {
        NodeList found = element.getElementsByTagName(tag);
       
        NodeListImpl children = new NodeListImpl();
       
        for (int i = 0; i < found.getLength(); i++) {
            Node node = found.item(i);
           
            Node parentNode = node.getParentNode();
           
            if (parentNode.equals(element)) {
                children.add(node);
            }
        }
       
        return children;
    }
View Full Code Here


     * will be returned from each branch. 
     * For example a panel within a panel, only the parent panel would be returned.
     * @return the elements found, or null if none are found or if there is an error.
     */
    public static NodeList getAllChildElementsByName( Element parent, String name ){
      NodeListImpl result = new NodeListImpl();
      NodeList l = parent.getChildNodes();
      int count = l.getLength();
      for( int i=0; i<count; i++ ) {
        Node n = l.item(i);
        if(n.getNodeType()==Node.ELEMENT_NODE) {
          Element e = (Element)n;
              String nodeName = e.getNodeName();
              if( name.equals(nodeName) ) {
                    result.add(e);
              }
              else{
                NodeList nl = getAllChildElementsByName((Element)n, name);
                if(nl.getLength()>0){
                  result = concatNodeLists(result, nl);
View Full Code Here

     * @param nl1
     * @param nl2
     * @return
     */
    public static NodeListImpl concatNodeLists(NodeList nl1, NodeList nl2){
      NodeListImpl results = new NodeListImpl();
      for(int i=0; i<nl1.getLength(); i++){
        results.add(nl1.item(i));
      }
      for(int i=0; i<nl2.getLength(); i++){
        results.add(nl2.item(i));
      }
      return results;
    }
View Full Code Here

     *
     * @return NodeList of all child elements of the parent
     */
    public static NodeList getChildNodesToNLevels( Node parent){
      NodeList l = parent.getChildNodes();
      NodeListImpl returnList = new NodeListImpl();
      int count = l.getLength();
      for( int i=0; i<count; i++ ) {
        Node n = l.item(i);
        returnList.add(n);
        NodeList children = getChildNodesToNLevels(n);
          if(null != children){
            returnList = concatNodeLists(returnList, children);
        }
      }
View Full Code Here

    }
    public XResult evaluateXPath(NodeList nodeList, Object xpath, NamespaceContext nsContext) throws XPathException {
        try {
            if(nodeList.getLength()>0) {
                XResult result = null;
                NodeListImpl nodes = null;
                PrefixResolver pr = nsContext!=null ? new NSResolver(nsContext) : null;
                for( int i=0; i<nodeList.getLength(); i++ ) {
                    XObject res = XPathAPI.eval(nodeList.item(i),(String)xpath,pr);
                    switch(res.getType()) {
                        case XObject.CLASS_BOOLEAN: {
                            if(nodes!=null) {
                                throw new XPathException(null,"XPath result cannot contain both values and nodes"); // $NLS-AbstractXercesDriver.AnXPathresultcannotcontainbothval-1$
                            }
                            if(result!=null) {
                                throw new XPathException(null,"XPath exception cannot return multiple values"); // $NLS-AbstractXercesDriver.AnXPathExceptioncannotreturnmulti-1$
                            }
                            result = new XResultUtils.BooleanValue(res.bool());
                        } break;
                        case XObject.CLASS_NUMBER: {
                            if(nodes!=null) {
                                throw new XPathException(null,"XPath result cannot contain both values and nodes"); // $NLS-AbstractXercesDriver.AnXPathresultcannotcontainbothval.1-1$
                            }
                            if(result!=null) {
                                throw new XPathException(null,"XPath exception cannot return multiple values"); // $NLS-AbstractXercesDriver.AnXPathExceptioncannotreturnmulti.1-1$
                            }
                            result = new XResultUtils.NumberValue(res.num());
                        } break;
                        case XObject.CLASS_STRING: {
                            if(nodes!=null) {
                                throw new XPathException(null,"XPath result cannot contain both values and nodes"); // $NLS-AbstractXercesDriver.AnXPathresultcannotcontainbothval.2-1$
                            }
                            if(result!=null) {
                                throw new XPathException(null,"XPath exception cannot return multiple values"); // $NLS-AbstractXercesDriver.AnXPathExceptioncannotreturnmulti.2-1$
                            }
                            result = new XResultUtils.StringValue(res.str());
                        } break;
                        case XObject.CLASS_NODESET: {
                            if(result!=null) {
                                throw new XPathException(null,"XPath result cannot contain both values and nodes"); // $NLS-AbstractXercesDriver.AnXPathresultcannotcontainbothval.3-1$
                            }
                            NodeList nl = res.nodelist();
                            int len = nl.getLength();
                            if(len>0) {
                                if(nodes==null) {
                                    nodes = new NodeListImpl();
                                }
                                for( int j=0; j<nl.getLength(); j++ ) {
                                    nodes.add(nl.item(j));
                                }
                            }
                        }
                    }
                }
                if(result!=null) {
                    return result;
                }
                if(nodes!=null) {
                    int len = nodes.getLength();
                    if(len==0) {
                        return XResultUtils.emptyResult;
                    } else if(len==1) {
                        return new XResultUtils.XMLNode(nodes.item(0));
                    } else {
                        return new XResultUtils.XMLNodeList(nodes);
                    }
                }
            }
View Full Code Here

   
    public void compute() throws XPathException {
        // compute the result of this xpath against the parent results, or the main document
        XResult r = null;
        if(!_isAbsolute && _parent!=null) {
            NodeListImpl parentNodes = _parent._contextNodes;
            if (parentNodes == null) {
                return;
            }
            r = _xPathExpression.eval(parentNodes, null);
        }
        else {
            if (_doc.getDocumentElement() == null) {
                return;
            }
            r = _xPathExpression.eval(_doc, null);
        }
       
        // and fill the current nodes
        for( Iterator it=r.getNodeIterator(); it.hasNext(); ) {
            if(_contextNodes==null) {
                _contextNodes = new NodeListImpl();
            }
            _contextNodes.add(it.next());
        }
    }
View Full Code Here

            }
           
            // And evaluate the XPath
            Object o = _xPathExpression.createNodes(ctx, null);
            if(o!=null) {
                _contextNodes = new NodeListImpl();
                _contextNodes.add(o);
            }
        }
    }
View Full Code Here

TOP

Related Classes of com.ibm.commons.xml.xpath.NodeListImpl

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.