A RSS
Reader in Java
(For more code samples,
visit Official VTD-XML Blog)
(Separate code-only
VTD-XML tutorials are available in
C,
C++,
Java
and
C#)
This example shows how to process a
RSS news feed using Java version of VTD-XML. The corresponding XML file and
Java files can be downloaded using the links below:
servers.xml
RSSReader.java
(Without XPath)
RSSReader3.java
(Without XPath, using VTDGen's readFile(..))
RSSReader2.java
(Using XPath)
RSSReader4.java (Using XPath and VTDGen's readFile(...))
The following packages are
imported:
import
com.ximpleware.*;
import com.ximpleware.xpath.*;
import java.io.*; |
The first step is to read the raw
bytes of servers.xml into a memory buffer. Notice that the
following code snippet throws java.io.IOexception.
// open a file and read the
content into a byte array
File f = new File("./servers.xml");
FileInputStream fis = new FileInputStream(f);
byte[] b = new byte[(int) f.length()];
fis.read(b); |
The second step is to parse the byte
buffer using the "parse(...)" method of VTDGen, which throws
com.ximpleware.ParseException.
VTDGen vg = new VTDGen();
vg.setDoc(b);
vg.parse(true); // set namespace awareness to true |
Next,
retrieve the VTDNav object from VTDGen and instantiate the AutoPilot object.
This part of the code throws com.ximpleware.NavException.
VTDNav vn = vg.getNav();
AutoPilot ap = new AutoPilot(vn);
// select name space here; * matches any local name
ap.selectElementNS("purl.org/dc/elements/1.1/","*"); |
The last step without XPath is shown below. The basic
idea is that calling the "iterate(...)" method of AutoPilot returns
the VTD index value and moves the cursor automatically to the corresponding
node position.
int count = 0;
while(ap.iterate()){
System.out.print(""+vn.getCurrentIndex()+" ");
System.out.print("Element name ==> "+vn.toString(vn.getCurrentIndex()));
int t = vn.getText(); // get the index of the text (char data or
CDATA)
if (t!=-1)
System.out.println(" Text ==> "+vn.toNormalizedString(t));
System.out.println("\n ============================== ");
count++;
}
System.out.println("Total # of element "+count); |
The code using the XPath feature of VTD-XML version
1.0 is a little different. The XPath expression is compiled by calling "selectXPath(...)"
of AutoPilot. Calling evalXPath(...) moves the cursor to the
selected nodes in the node set. Additional exceptions are
com.ximpleware.xpath.XPathEvalException and
com.ximpleware.xpath.XPathParseException.
AutoPilot ap = new AutoPilot(vn);
ap.declareXPathNameSpace("ns1","purl.org/dc/elements/1.1/");
ap.selectXPath("//ns1:*");
int result = -1;
int count = 0;
while((result = ap.evalXPath())!=-1){
System.out.print(""+result+" ");
System.out.print("Element name ==> "+vn.toString(result));
int t = vn.getText(); // get the
index of the text (char data or CDATA)
if (t!=-1)
System.out.println(" Text ==> "+vn.toNormalizedString(t));
System.out.println("\n
============================== ");
count++;
}
System.out.println("Total # of element "+count); |