Java 8 XPathFactory - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Java 8 XPathFactory class.

This code example shows how to use the following methods:newXPath
  public XPathWrapper(Document doc) {
    this.doc = doc;
  } 
 
  public String getValue(String pathExpr) throws Exception {
    XPath xPath = FACTORY.newXPath();
    return xPath.evaluate(pathExpr, doc);
  } 
 
  public Node getNode(String pathExpr) throws Exception {
    XPath xPath = FACTORY.newXPath();
    return (Node)xPath.evaluate(pathExpr, doc, XPathConstants.NODE);
  } 
 
  public NodeList getNodeList(String pathExpr) throws Exception {
    XPath xPath = FACTORY.newXPath();
    return (NodeList)xPath.evaluate(pathExpr, doc, XPathConstants.NODESET);
  } 
} 
6
CodeRank
This code example shows how to use the following methods:newXPath
  public Node getNode( String xml, String path ) throws Exception {
    return getNode( getDocument(xml), path );
  } 
   
  public Node getNode( Document doc, String path ) throws Exception {
    XPath xpath = xpathFactory.newXPath();
    return (Node)xpath.evaluate(path, doc, XPathConstants.NODE);
  } 
   
  public Document getDocument( String xml ) throws Exception {
    return builder.parse(new InputSource(new StringReader(xml)));
  } 
} 
6
CodeRank
This code example shows how to use the following methods:newXPath
     * Creates a new XPath object, specifying which prefix in the query is used for the 
     * android namespace. 
     * @param androidPrefix The namespace prefix. 
     */ 
    public static XPath newXPath(String androidPrefix) {
        XPath xpath = sFactory.newXPath();
        xpath.setNamespaceContext(new AndroidNamespaceContext(androidPrefix));
        return xpath;
    } 
 
    /** 
     * Creates a new XPath object using the default prefix for the android namespace. 
     * @see #DEFAULT_NS_PREFIX 
     */ 
    public static XPath newXPath() {
        XPath xpath = sFactory.newXPath();
        xpath.setNamespaceContext(AndroidNamespaceContext.getDefault());
        return xpath;
    } 
} 
5
CodeRank
This code example shows how to use the following methods:newXPath
        "xml", "application/xml"); 
 
    XSDValidator.validate(resp, XMLSCHEMA, XSDRESOURCE,false);
     
    InputSource source = new InputSource(new StringReader(resp));
    XPath xp = xpathFactory.newXPath();
    NodeList result = (NodeList) xp.evaluate("/response/activity", source,
        XPathConstants.NODESET);
    assertEquals(1, result.getLength());
    Node n = result.item(0);
 
    Map<String, List<String>> v = childNodesToMap(n);
 
    assertEquals(4, v.size());
    assertActivitiesEqual(johnsActivity, v);
  } 
 
  /** 
   * Expected response for a list of activities in json: 
   *  
5
CodeRank
This code example shows how to use the following methods:newXPath
  protected String getRawResponse() {
    return rawResponse;
  } 
 
  protected Node getNode(String xPath) throws XPathExpressionException {
    return (Node)xPathFact.newXPath().compile(xPath).evaluate(doc, XPathConstants.NODE);
  } 
  private NodeList getNodes(String n1, String n2, String n3, String element, String n4) throws XPathExpressionException {
    // Construct the XPath expression. The form better not change or all these will fail. 
    StringBuilder sb = new StringBuilder("/response/lst[@name='stats']/lst[@name='").append(n1).append("']");
    sb.append("/lst[@name='").append(n2).append("']");
    sb.append("/lst[@name='").append(n3).append("']");
    sb.append("//").append(element).append("[@name='").append(n4).append("']");
    return (NodeList)xPathFact.newXPath().compile(sb.toString()).evaluate(doc, XPathConstants.NODESET);
 
  } 
  protected ArrayList<String> getStringList(String n1, String n2, String n3, String element, String n4)
      throws XPathExpressionException {
    ArrayList<String> ret = new ArrayList<>();
    NodeList nodes = getNodes(n1, n2, n3, element, n4);
5
CodeRank
This code example shows how to use the following methods:newXPath
  public Document getDocument() {
    return doc;
  } 
 
  public XPath getXPath() {
    return xpathFactory.newXPath();
  } 
 
  private String normalize(String path) {
    return (prefix==null || path.startsWith("/")) ? path : prefix+path;
  } 
   
  public void substituteProperties() { 
    DOMUtil.substituteProperties(doc, getSubstituteProperties());
  } 
 
 
  public Object evaluate(String path, QName type) {
    XPath xpath = xpathFactory.newXPath();
    try { 
5
CodeRank
This code example shows how to use the following methods:newXPath
     * Creates a new XPath object, specifying which prefix in the query is used for the 
     * android namespace. 
     * @param androidPrefix The namespace prefix. 
     */ 
    public static XPath newXPath(String androidPrefix) {
        XPath xpath = sFactory.newXPath();
        xpath.setNamespaceContext(new AndroidNamespaceContext(androidPrefix));
        return xpath;
    } 
 
    /** 
     * Creates a new XPath object using the default prefix for the android namespace. 
     * @see #DEFAULT_NS_PREFIX 
     */ 
    public static XPath newXPath() {
        XPath xpath = sFactory.newXPath();
        xpath.setNamespaceContext(AndroidNamespaceContext.getDefault());
        return xpath;
    } 
} 
5
CodeRank
This code example shows how to use the following methods:newXPath
	private static final int SUGG_LIMIT = 3; 
	 
	public GoogleTrafficHandler() 
	{ 
		xpf = XPathFactory.newInstance(); 
		xpath = xpf.newXPath(); 
	} 
	 
	/*** 
	 * Method connect to google maps with the query provided ("from", "to") and returns the duration in milli. 
	 * @param from - origin place in latitude,longitude 
	 * @param to - destination place with same format as origin 
	 */ 
	public TrafficData calculateTrafficInfo(String from, String to) throws Exception 
	{ 
		TrafficData trafficData = new TrafficData(); 
		 
		from = URLEncoder.encode(from, "UTF-8"); 
		to = URLEncoder.encode(to, "UTF-8"); 
		 
4
CodeRank
This code example shows how to use the following methods:newXPath
   private static final String[] XML_ESCAPES = new String[] {"&amp;", "&lt;", "&gt;", "&quot;"};
   private static final String LINEFEED = "&#10;";
   private static final String CARRIAGE_RETURN = "&#13;";
   private static final Pattern squareBracket = Pattern.compile("\\]");
   public final static XPathFactory myXPathFactory = XPathFactory.newInstance();
   public final static XPath myXPath = myXPathFactory.newXPath();
   public final static String wordLeader1 =
      "<?xml version='1.0' encoding='UTF-8' standalone='yes'?>" + "<?mso-application progid='Word.Document'?>"; 
   public final static String wordLeader2 =
      "<w:wordDocument xmlns:w='http://schemas.microsoft.com/office/word/2003/wordml' xmlns:v='urn:schemas-microsoft-com:vml' xmlns:w10='urn:schemas-microsoft-com:office:word' xmlns:sl='http://schemas.microsoft.com/schemaLibrary/2003/core' xmlns:aml='http://schemas.microsoft.com/aml/2001/core' xmlns:wx='http://schemas.microsoft.com/office/word/2003/auxHint' xmlns:o='urn:schemas-microsoft-com:office:office' xmlns:dt='uuid:C2F41010-65B3-11d1-A29F-00AA00C14882' xmlns:wsp='http://schemas.microsoft.com/office/word/2003/wordml/sp2' xmlns:ns0='http://www.w3.org/2001/XMLSchema' xmlns:ns1='http://eclipse.org/artifact.xsd' xmlns:st1='urn:schemas-microsoft-com:office:smarttags' w:macrosPresent='no' w:embeddedObjPresent='no' w:ocxPresent='no' xml:space='preserve'>"; 
   public final static String wordLeader = wordLeader1.concat(wordLeader2);
   public final static String wordBody = "<w:body></w:body>";
   public final static String wordTrailer = "</w:wordDocument> ";
   public final SimpleNamespaceContext mySimpleNamespaceContext = new SimpleNamespaceContext();
   public final static Matcher theFunnyQuoteMatcher = Pattern.compile("�").matcher("");
 
   /** 
    * TODO Optimize algorithm 
    *  
    * @return Returns a string with entity reference characters unescaped. 
4
CodeRank
This code example shows how to use the following methods:newXPath
            this.dateFormat = dateFormat; 
            this.nodes = nodes; 
        } 
 
        private void initExpressions() { 
            XPath xPath = xpathFactory.newXPath(); 
            try { 
                this.keyExp = xPath.compile("key"); 
            } catch (XPathExpressionException e) { 
                log.error("Failed to initialize xpath expressions", e); 
            } 
        } 
 
        private Track assembleTrack(Node dictionaryNode) throws XPathExpressionException { 
            ItunesTrack track = new ItunesTrack(); 
 
            NodeList keys = (NodeList) keyExp.evaluate(dictionaryNode, XPathConstants.NODESET); 
            for (int j = 0; j < keys.getLength(); j++) { 
                Node key = keys.item(j); 
                setTrackProperty(track, key); 
4
CodeRank

Related Java 8 XPathFactory Questions & Answers:

See Code Examples for Java 8 XPathFactory Methods: