Tuesday, December 13, 2011

Generate random string of certain length

function genRandomString()

{
$length = 10;
$characters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
$string = null;
for ($p = 0; $p < $length; $p++) {
$string .= $characters[mt_rand(0, strlen($characters))];
}
return $string;
}



$x=genRandomString();
echo substr($x,0,6);

?>

Monday, December 5, 2011

XML Parser in JSP

/**
*
* @author yashx1@gmail.com
*/

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
import java.io.InputStream;
import java.net.URL;
import com.ahoy.ubid.utils.ParseUtil;

public class XmlParserHelp {

public static void main(String argv[]) {
try {
ParseUtil p = new ParseUtil();
URL xmlURL = new URL("http://www.newsletter.uahoy.com:8081/WeBid/api.auctions.php");
InputStream xml = xmlURL.openStream();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(xml);
xml.close();
doc.getDocumentElement().normalize();
NodeList nList = doc.getElementsByTagName("result");
for (int temp = 0; temp < nList.getLength(); temp++) {
Node nNode = nList.item(temp);
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
System.out.println("auctionTitle : " + p.getTagValue("auctionTitle", eElement));

//System.out.println("auctioneerId : " + p.getTagValue("auctioneerId", eElement));
}
}
} catch (Exception e) {
System.out.println(e);
}
}

}