XML Server Getting Started    
Appendix C : XPathAPI   Helper Class used in this tutorial 
package com.cti.xmlsp; 
import org.xml.sax.SAXException; 
import org.w3c.dom.Node; 
import org.w3c.dom.Document; 
import org.w3c.dom.NodeList; 
import org.apache.xalan.xpath.XPathSupport; 
import org.apache.xalan.xpath.XPath; 
import org.apache.xalan.xpath.XPathProcessorImpl; 
import org.apache.xalan.xpath.xml.XMLParserLiaisonDefault; 
import org.apache.xalan.xpath.xml.PrefixResolverDefault; 
import org.apache.xalan.xpath.XObject; 
public class XPathAPI 
{ 
   public XPathAPI() 
  { 
    _xpathSupport = new XMLParserLiaisonDefault(); 
    _xpath = new XPath(); 
    _xpathProcessor = new XPathProcessorImpl(_xpathSupport); 
  } 
   
  public void setPrefixResolver(Node contextNode, Node namespaceNode) { 
    if(null == namespaceNode) namespaceNode = contextNode; 
   
 _prefixResolver = new PrefixResolverDefault((namespaceNode.getNodeType() == Node.DOCUMENT_NODE) 
                                                         ? ((Document)namespaceNode).getDocumentElement() : 
                                                           namespaceNode); 
  } 
  public void setPrefixResolver(Node contextNode) { 
 setPrefixResolver(contextNode, contextNode);    
  } 
   
 /** 
   * Use an XPath string to select a single node. XPath namespace 
   * prefixes are resolved from the context node, which may not 
   * be what you want (see the next method). 
   * 
   * @param contextNode The node to start searching from. 
   * @param str A valid XPath string. 
   * @return The first node found that matches the XPath, or null. 
   */ 
  public Node selectSingleNode(Node contextNode, String str) 
    throws SAXException 
  { 
    return selectSingleNode(contextNode, str, contextNode); 
  } 
 2000 2002 Computer Technology Inc. All rights reserved. 
Page 65 




J2EE Web Hosting Tundra XML Server Pages J2EE Hosting




 
TotalRoute.net Business web hosting division of Vision Web Hosting Inc. All rights reserved.