KickJava   Java API By Example, From Geeks To Geeks.

Java > Java SE, EE, ME > javax > xml > xpath > XPath

javax.xml.xpath
Interface XPath

See Also:
Top Examples, Source Code, XPathConstants, setNamespaceContext(NamespaceContext nsContext), setXPathFunctionResolver(XPathFunctionResolver resolver), XPathFunctionResolver, XPathExpressionException, setXPathVariableResolver(XPathVariableResolver resolver), XPathVariableResolver

XPathExpression compile(String expression)
                        throws XPathExpressionException
See Also:
NullPointerException, XPathFunctionResolver, XPathFunction
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


String evaluate(String expression,
                Object item)
                throws XPathExpressionException
See Also:
NullPointerException, XPathConstants.STRING, evaluate(String expression, Object item, QName returnType)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[1529]Use XSL to retrieve a node
By Lalith Weerasinghe on 2005/09/08 00:00:12  Rate
 
 The xml node I use is as follows 
  
  
  < ?xml version="1.0" encoding="ISO-8859-1"? >  
  < catalog >  
    < cd >  
      < a >  
        < b >  
          < c >  
            < d >  
              < title > Empire Burlesque < /title >  
              < artist > Bob Dylan < /artist >  
              < country > USA < /country >  
              < company > Columbia < /company >  
              < price > 10.00 < /price >  
              < year > 1985 < /year >  
            < /d >  
          < /c >  
        < /b >  
      < /a >  
    < /cd >  
  < /catalog >  
  
  
 Use follwoing XSL to retrieve the node 
  
  
  < ?xml version="1.0" encoding="iso-8859-1"? >  
  < xsl:stylesheet version="1.0" 
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:myns="CatalogTest" 
   xmlns:lxslt="http://xml.apache.org/xslt" >  
    < xsl:output method="xml" / >  
    < lxslt:component prefix="myns" functions="transform" >  
      < lxslt:script lang="javaclass" SRC="CatalogTest" / >  
    < /lxslt:component >  
  
  
    < xsl:template match="/" >  
      < xsl:for-each select="catalog/cd" >  
        < xsl:variable name="priceValue" select="." / >  
       Price:   < xsl:value-of select="myns:transform ( $priceValue ) " / >  
      < /xsl:for-each >  
    < /xsl:template >    
  < /xsl:stylesheet >  
  
  
  
 Now i'm going to access the  < price >  tag value as follows using "transform" method of my "CatalogTest.java" class 
   public static String transform ( Node node )   {  
  
  
     Double rate = 0.00; 
     if  ( node != null )   {  
  
  
       try  {  
  
  
         XPath xpath = XPathFactory.newInstance (  ) .newXPath (  ) ; 
         String expression = "//cd/a/b/c/d/price"; 
         String tmpStr = xpath.evaluate ( expression, node ) ; 
         price= Double.parseDouble ( tmpStr ) ; 
         price= price* 1.25; 
        }  catch  ( Exception e )   {  
         // TODO Auto-generated catch block 
         e.printStackTrace (  ) ; 
        }  
  
  
       return rate.toString (  ) ; 
      }  
  
  
 You may need to create a seperate clas with the  
 javax.xml.transform.Transformer  
 using xsl file and call "transform ( xxx,yyy ) " method of it  
 to see the out put. 
 


Object evaluate(String expression,
                Object item,
                QName returnType)
                throws XPathExpressionException
See Also:
NullPointerException, XPathConstants, IllegalArgumentException, NODESET, NODE, BOOLEAN, STRING, NUMBER
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


String evaluate(String expression,
                InputSource source)
                throws XPathExpressionException
See Also:
NullPointerException, XPathConstants.STRING, evaluate(String expression, InputSource source, QName returnType)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


Object evaluate(String expression,
                InputSource source,
                QName returnType)
                throws XPathExpressionException
See Also:
NullPointerException, XPathConstants, IllegalArgumentException, evaluate(String expression, Object item, QName returnType)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[1361]How to access the ancestors of an element???
By shan { dot } bourne { at } gmail { dot } com on 2005/07/30 14:41:03  Rate
Hi,  
  
  
 I was trying to access the ancestors of an element but its not working for me, is there any way that I can acess all the parents  
 of a particular element using java 1.5 javax.xml.xpath package. 
 I am using java SAX parser to parse the document. 
  
  
 This is top urgent any feedback would be much appreciated. please reply directly to my e-mail address


NamespaceContext getNamespaceContext()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


XPathFunctionResolver getXPathFunctionResolver()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


XPathVariableResolver getXPathVariableResolver()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


void reset()
See Also:
Object.equals(Object obj), NamespaceContext, XPathVariableResolver, XPathFunctionResolver, XPathFactory.newXPath()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


void setNamespaceContext(NamespaceContext nsContext)
See Also:
NullPointerException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[1385]Implement the NamespaceContext interface
By Anonymous on 2005/04/07 17:44:21  Rate
Would love to know where I can get a NamespaceContext from in order to pass one inter here. Do I have to implement this myself?

[1391]_
By kickjava { at } e-henry { dot } net on 2005/06/07 23:19:44  Rate
To use this method you will need to implement by yourself the NamespaceContext interface. You will need to fully implement getNamespaceURI and getPrefix. 
  
  
 The following is custom for the example: 
 + Fields: namespaceURI, prefix 
 + Constructor: MyNamespace 
  
  
 public class MyNamespace implements NamespaceContext  {  
   private String namespaceURI; 
   private String prefix; 
    
   public MyNamespace ( String documentPrefix, String documentNamespaceURI )   {  
     prefix = documentPrefix; 
     namespaceURI = documentNamespaceURI; 
    }  
  
  
   public String getNamespaceURI ( String prefixToLookUp )  throws IllegalArgumentException {  
     if  ( ! ( prefixToLookUp == null )  )   {  
       if ( prefixToLookUp.equals ( prefix )  )   {  
         return namespaceURI; 
        }  else if  ( prefixToLookUp.equals ( XMLConstants.XML_NS_PREFIX )  )   {  
         return XMLConstants.XML_NS_URI; 
        }  else if  ( prefixToLookUp.equals ( XMLConstants.XMLNS_ATTRIBUTE )  )  {  
         return XMLConstants.XMLNS_ATTRIBUTE_NS_URI; 
        }  else  {  
         return XMLConstants.NULL_NS_URI; 
        }  
      }  else  {  
       throw new IllegalArgumentException (  ) ; 
      }  
    }  
      
  
  
   public String getPrefix ( String namespaceURIToLookUp )  throws IllegalArgumentException  {  
     if  ( ! ( namespaceURIToLookUp == null )  )   {  
       if  ( namespaceURIToLookUp.equals ( namespaceURI )  )   {  
         return prefix; 
        }  else if  ( namespaceURIToLookUp.equals ( XMLConstants.XML_NS_URI )  )   {  
         return XMLConstants.XML_NS_PREFIX; 
        }  else if  ( namespaceURIToLookUp.equals ( XMLConstants.XMLNS_ATTRIBUTE_NS_URI )  )   {  
         return XMLConstants.XMLNS_ATTRIBUTE; 
        }  else  {  
         return null; 
        }  
      }  else  {  
       throw new IllegalArgumentException (  ) ; 
      }  
    }  
    
  
  
   public Iterator getPrefixes ( String namespaceURIToLookUp )   {  
     //TODO You should implement this for multiple namespaces 
     return null; 
    }  
  } 


void setXPathFunctionResolver(XPathFunctionResolver resolver)
See Also:
NullPointerException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


void setXPathVariableResolver(XPathVariableResolver resolver)
See Also:
NullPointerException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  

Popular Tags