Header Ad

Showing posts with label web. Show all posts
Showing posts with label web. Show all posts

Friday, August 17, 2007

Reading XML from another Site using Java code

import="com.sun.org.apache.xerces.internal.util.URI"
import="java.io.InputStream"
import="java.net.URL"
import="javax.xml.parsers.DocumentBuilderFactory"
import="javax.xml.parsers.DocumentBuilder"
import="org.w3c.dom.Document"
import="org.w3c.dom.NodeList"
import="org.w3c.dom.NamedNodeMap"
import="org.w3c.dom.Node"

URL url= new URL("www.google.com");
InputStream in=url.openStream();
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
DocumentBuilder builder=factory.newDocumentBuilder();
Document document=builder.parse(in);

If you want to get retrive entire node list that can be done by

NodeList nodeList=document.getElementsByTagName("NameofTheNode");

By these all the node will come in that Node List.
If you want to retrive the attributes with in that Node,that can be done by

Node node= nodeList.item(indx);
NamedNodeMap nnm=node.getAttributes();
String href=nnm.getNamedItem("AttributeNamewithinthatNode").getNodeValue();