Results 1 to 2 of 2
  1. #1
    Join Date
    Mar 2008
    Posts
    606

    translate this Javascript function to Java?

    I need the following Javascript function translated to Java (org.w3c.dom). Is it possible? If someone can write it, I will be really happy.

    http://snippets.dzone.com/posts/show/4349

    function eval_xpath(xpath)
    {if (document.evaluate) //firefox
    {return document.evaluate(xpath, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
    }
    else //ie
    {//can not use xmldocument.selectSingleNode(xpath);
    var tags=xpath.slice(1).split('/');
    var ele=document;
    for (var i=0; i<tags.length; ++i)
    {var idx=1;
    if (tags[i].indexOf('[')!=-1)
    {idx=tags[i].split('[')[1].split(']')[0];
    tags[i]=tags[i].split('[')[0];
    }
    var ele=ele.getElementsByTagName(tags[i])[idx-1];
    }
    return ele;
    }
    }
    Last edited by jjk2; 03-16-2011 at 11:44 PM.

  2. #2
    Join Date
    Aug 2002
    Location
    Superior, CO, USA
    Posts
    633
    Taken from my config code. This assumes Java 6.

    Code:
    package com.xigole.utilities;
    
    import java.io.IOException;
    import java.io.InputStream;
    
    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;
    import javax.xml.parsers.ParserConfigurationException;
    import javax.xml.xpath.XPath;
    import javax.xml.xpath.XPathConstants;
    import javax.xml.xpath.XPathExpressionException;
    import javax.xml.xpath.XPathFactory;
    
    import org.w3c.dom.Document;
    import org.w3c.dom.Node;
    import org.w3c.dom.NodeList;
    import org.xml.sax.SAXException;
    
    public class SysConfig {
    	
    public NodeList getConfigurationParameters( String path ) throws Exception {   
        Document configDocument = null;
        XPath xpath;
    		
        InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream( "systemConfig.xml" );
    
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setNamespaceAware(true);
    
        DocumentBuilder db = null;
        try {
            db = dbf.newDocumentBuilder();
            configDocument = db.parse(inputStream);
        }
        catch (ParserConfigurationException pce) {
            throw new SAXException("Can't get new DocumentBuilder", pce);
        }
        catch (IOException ioe) {
            throw new SAXException( "Can't get parse document", ioe);
        }
        xpath = XPathFactory.newInstance().newXPath();
    
        Node parentNode = configDocument.getDocumentElement();
    
        NodeList nodeList = null;
        try {
            nodeList = (NodeList)xpath.evaluate( path, parentNode, XPathConstants.NODESET);
        }
        catch (XPathExpressionException xpe) {
            throw new SAXException("Cannot evaluate xpath path", xpe);
        }
    
        if( (nodeList == null) || (nodeList.getLength() == 0) )
            return null;
    
        return nodeList;
        }
    }
    Need Java help? Want to help people who do? Sit down with a cup of Java at the hotjoe forums.

Similar Threads

  1. Javascript, call more than one function
    By raulgonzalez in forum Programming Discussion
    Replies: 3
    Last Post: 02-23-2011, 01:36 PM
  2. JavaScript Function
    By NorthWest in forum Programming Discussion
    Replies: 21
    Last Post: 04-10-2006, 04:06 PM
  3. javascript function has no properties in javascript console
    By mrblack in forum Programming Discussion
    Replies: 2
    Last Post: 05-25-2005, 02:15 PM
  4. Javascript Function
    By ecal in forum Other Offers & Requests
    Replies: 2
    Last Post: 02-22-2004, 12:36 AM
  5. Java eval()-like function?
    By Falco1199 in forum Programming Discussion
    Replies: 3
    Last Post: 09-21-2003, 03:36 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •