- It is a light weight xml dom parser.
- It provides ma features like filter,namespace.
- The filters are categorized mainly Element filter,content filter.
- The API download Link :-
Project Structure:-
Test Jdom Api:-
package com.sandeep.jdom.xml;
import java.io.File;
import java.io.IOException;
import java.util.List;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.filter.ElementFilter;
import org.jdom2.filter.Filter;
import org.jdom2.input.SAXBuilder;
public class JDomTest {
public static void main(String[] args) {
File xmlFile = new File("input-xml-file.xml");
SAXBuilder builder = new SAXBuilder();
Document jdomDocument = null;
try {
jdomDocument = builder.build(xmlFile);
/**
* Finding root element .
* Here Countries is root
***/
Element rootElement = jdomDocument.getRootElement();
/***
* FInding all the element after the root element Here Country
* elements are adjacent of Countries root Element
***/
Filterfilter1 = new ElementFilter();
ListlistOfElements = rootElement.getContent(filter1);
/** Filtering only Food Items From both the Countries **/
Filterfilter2 = new ElementFilter("Food");
for (Element element : listOfElements) {
ListfoodList = element.getContent(filter2);
System.out.println("Famous Food Items : ");
for (Element foodItem : foodList) {
System.out.println(foodItem.getValue());
}
}
} catch (JDOMException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}