Read XML from URL
XOM (XML Object Model) is a tree based java API for processing XML
by taking the best ideas from SAX and DOM. It is simple, fast and easy to use.
This requires the library xom-1.2.7.jar to be in classpath.
The following example shows reading an XML document from a URL.
package com.bethecoder.tutorials.xom.tests;
import java.io.IOException;
import nu.xom.Builder;
import nu.xom.Document;
import nu.xom.Element;
import nu.xom.ParsingException;
import nu.xom.ValidityException;
public class ReadXMLFromURL {
/**
* @param args
* @throws IOException
* @throws ParsingException
* @throws ValidityException
*/
public static void main ( String [] args ) throws ValidityException, ParsingException, IOException {
Builder builder = new Builder () ;
//Reads and parses the XML
Document doc = builder.build ( "http://bethecoder.com/rss.xml" ) ;
//Get channel element
Element channel = doc.getRootElement () .getChildElements ( "channel" ) .get ( 0 ) ;
//Get description
String desc = channel.getChildElements ( "description" ) .get ( 0 ) .getValue () ;
System.out.println ( desc ) ;
}
}
It gives the following output,