For years I have been using the classic DOM implementations using Xerces, etc. Lots of people claim that the DOM API in Java is a bit strange, I always thought that the Java XML APIs in general were quite clean and easy to learn.Well anyway. I have not used JDOM since now! Manipulating DOM trees and making changes could not have been easier! So here is some sample code that illustrates JDOM's basic functions (the API has lots of things). Its for beginners I suppose!
Have fun with JDOM!
———————————————————————
/* * PlayWithJDom.java * @author javaneze@gmail.com */ package jforjava; import java.io.File; import java.io.IOException; import org.jdom.Document; import org.jdom.Element; import org.jdom.JDOMException; import org.jdom.input.SAXBuilder; import org.jdom.output.DOMOutputter; import org.jdom.output.XMLOutputter; public class PlayWithJDom { XMLOutputter printer = null; public PlayWithJDom() { //lets create an XML Printer! printer = new XMLOutputter(); } /** * Parsing an XML file and then * playing with some basic JDOM functionality! * @param anXmlDocFile */ public void parseAndPlay(File anXmlDocFile){ //create a parser SAXBuilder parser = new SAXBuilder(); try { //get the dom-document Document doc = parser.build(anXmlDocFile); // illustrate some jdom tree walk and print sample System.out.println(doc.getRootElement().getName()); Element firstChild = (Element)doc.getRootElement().getChildren().get(0); System.out.println(">> "+firstChild.getName()+":"+ firstChild.getAttribute("type").getValue()); System.out.println(">>>>" +firstChild.getText()); //easily print this.printJDom(doc); // do you want an classic DOM? no prob! DOMOutputter out = new DOMOutputter(); org.w3c.dom.Document domDoc= out.output(doc); System.out.println(domDoc.getImplementation().toString()); //lets print in the console the JDomTree this.printJDom(doc); //lets make some changes! Element child =(Element) doc.getRootElement().getChildren().get(0); child.setName("NewNAME"); child.setText("Water Mellon"); //lets print in the console the altered JDomTree this.printJDom(doc); } catch (JDOMException ex) { ex.printStackTrace(); } catch (IOException ex) { ex.printStackTrace(); } } /** * Using the XMLOuputer prints in the Console * the JDOM tree! * @param aJDOMdoc A JDOM Document * @throws java.io.IOException */ public void printJDom(Document aJDOMdoc) throws IOException{ System.out.println("<------------XML DOCUMENT------------>"); this.printer.output(aJDOMdoc, System.out); } /** * Main Method - Program Start! * @param args */ public static void main(String[] args){ PlayWithJDom play = new PlayWithJDom(); play.parseAndPlay(new File(args[0])); } }
- —–USE THIS SAMPLE XML IF YOU WISH———————
<?xml version="1.0"?>
<food>
<name type="fruit">Avocado Dip</name>
<mfr>Sunnydale</mfr>
<serving units="g">29</serving>
<calories total="110" fat="100"/>
<total-fat>11</total-fat>
<saturated-fat>3</saturated-fat>
<cholesterol>5</cholesterol>
<sodium>210</sodium>
<carb>2</carb>
<fiber>0</fiber>
<protein>1</protein>
<vitamins>
<a>0</a>
<c>0</c>
</vitamins>
<minerals>
<ca>0</ca>
<fe>0</fe>
</minerals>
</food>