/*
 * Created on 13.11.2003
 *
 * To change the template for this generated file go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
package de.kksoftware.demo.xml;

import java.io.*;
import java.util.*;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;

/**
 * @author administrator
 *
 * To change the template for this generated type comment go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
public class JDomReadFile {

	public static void main(String[] args) throws Exception {
		// create a XML parser and read the XML file
		SAXBuilder oBuilder = new SAXBuilder();
		Document oDoc = oBuilder.build(new File("myconfig.xml"));
		
		// get the root node of the document
		Element oRoot = oDoc.getRootElement();
		System.out.println("Root element is "+oRoot.getName());
		// get first <name> node and print its content (see below)
		System.out.println("Configuration name is "+oRoot.getChildTextTrim("name"));
		
		// get all children of the root node of type <path>
		// other variants:
		//   oRoot.getChildren() would retrieve all sub-nodes
		List oPathes = oRoot.getChildren("path");
		System.out.println("There are "+oPathes.size()+" pathes defined:");
		Iterator iter = oPathes.iterator();
		while (iter.hasNext()) {
			Element oPath = (Element)iter.next();
			// read the "id" attribute
			String strId = oPath.getAttributeValue("id");
			// print out node content:
			//   - getText() just returns text
			//   - getTextNormalize() removes surrounding white space
			//      and compresses multiple spaces to a single one
			//   - getTextTrim() removes surrounding white space
			System.out.println("Path "+strId+" is "+oPath.getTextTrim());
			// get sub-nodes <filter>
			Iterator iterFilter = oPath.getChildren("filter").iterator();
			while (iterFilter.hasNext()) {
				Element oFilter = (Element) iterFilter.next();
				System.out.println("  defined filter: "+oFilter.getTextTrim());
			}
		}
	}
}

