Repository Interface

Wednesday, November 08, 2006

The following program will show you how to extract xml elements from an existing xml file.



import org.jdom.*;
import org.jdom.input.*;
import org.jdom.output.*;
import java.util.*;
import java.io.*;

public class repos
{


public static void main(String[] args) throws IOException{
repos b = new repos();
b.buildAndListDocument(args[0]);
}

public void buildAndListDocument(String filename) throws IOException{
try {
SAXBuilder builder = new SAXBuilder();
Document doc = builder.build(filename);
listElements(doc.getRootElement());
} catch (JDOMException e) {
e.printStackTrace();
}
}

private void listElements(Element e) {
System.out.println(e.getText()) ;

List c = e.getChildren();
for (Iterator i = c.iterator();i.hasNext();) {
Element n = (Element)i.next();
listElements(n);
}
}

}

0 Comments:

Post a Comment

<< Home