alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/*  $Id: xml_parser.java,v 1.8 2001/03/03 18:31:26 agarcia3 Exp $ 
    webEditor. The new way in content management
    Copyright (C) 2001  Alfredo Garcia

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
*/

package webEditor.util;

import java.io.*;
import java.util.Enumeration;
import java.util.Hashtable;

//import javax.xml.parsers.*;
import org.w3c.dom.*;

import org.apache.xerces.dom.*;
import org.apache.xerces.parsers.DOMParser;

/**
 * Generic parser implementation for webEditor  
 * @author <a href="mailto:agarcia@mundofree.com">Alfredo Garcia
 */
public class xml_parser 
{
   /**
    * Perform the initial parse	of a configuration file
    * @param confFile	Configuration file
    * @return Hashtable sorted content
    */
   public Hashtable parseConfFile (String confFile) 
   {
	Hashtable myTable = new Hashtable();
	Hashtable auxTable = null;
	String categoryName = null;

try {
	// The parser begins!
	DOMParser parser = new DOMParser();
	confFile = new File (confFile).getAbsolutePath();
	confFile = "file:" + confFile;
	parser.parse(confFile);
	Document doc = parser.getDocument();
	
	// Get the root's childs
	Element root = doc.getDocumentElement();
	String rootTag = root.getTagName();
	NodeList list = doc.getElementsByTagName(rootTag).item(0).getChildNodes();

	// For every child category, creates a hashtable with the content
	for (int i=0; i < list.getLength(); i++) {
		Node myCategory = list.item(i);
		if (myCategory.getNodeName().equals("category")) {
			categoryName = myCategory.getAttributes().getNamedItem("name").getNodeValue();
			NodeList itemNodes = myCategory.getChildNodes();

			// For every child item, adds the code:value pairs
			auxTable = new Hashtable();
			for (int j=0; j < itemNodes.getLength(); j++) {
				Node myItem = itemNodes.item(j);
				if (myItem.getNodeName().equals("data")) {
					auxTable.put(myItem.getAttributes().getNamedItem("code").getNodeValue(),
					myItem.getFirstChild().getNodeValue());
        			}   
			}
			// And then, we add the auxTable to the main table
			myTable.put (categoryName, auxTable);
        	}   
      	} 

} catch (Exception e) {
	e.printStackTrace();
}
   	return (myTable);
   }

   /**
    * Translates a simple DOM tree into a HashTable
    * @param content	  DOM tree of the document	
    * @return Hashtable   sorted content of the document
    */
   public Hashtable parseDOM (Document content) 
   {
	Hashtable 	myTable;
	myTable = new Hashtable();

try {
	Element root = content.getDocumentElement();
	String rootTag = root.getTagName();
	NodeList list = content.getElementsByTagName(rootTag).item(0).getChildNodes();

	// For every child add the codes and the words
	for (int i=0; i < list.getLength(); i++) {
		if (list.item(i).getNodeName().equals("data")) {
			myTable.put(list.item(i).getAttributes().getNamedItem("code").getNodeValue(),
			list.item(i).getFirstChild().getNodeValue());
        	}   
      	} 

} catch (Exception e) {
	e.printStackTrace();
}
   	return (myTable);
  }

  /**
    * Given a multilevel Hashtable with pairs <key,value>, returns a DOM tree with the "same" structure.
    * @param docRoot	  It could be empty if you want to process the hole tree
    * @param content	  Values to write
    * @return Document	  output DOM tree 
    */
   public Document writeDOM (String docRoot, Hashtable content) 
   {
	Document doc= new DocumentImpl();
	Element item;
	Element itemCategory;
	Element root = doc.createElement(docRoot); 

	Enumeration myList = content.keys();

	for (; myList.hasMoreElements() ;) {
		String name = (String) myList.nextElement();
		// We are going to assume that there is no configuration items 
		// outside a category.
		itemCategory = doc.createElement ("category");
		itemCategory.setAttribute ("name", name);

		Hashtable category = (Hashtable) content.get(name);
		Enumeration myItems = category.keys();
		for (; myItems.hasMoreElements() ;) {
			String code = (String) myItems.nextElement();
			String value = (String) category.get(code);

			item = doc.createElement ("data");
			item.setAttribute ("code", code);
			item.appendChild (doc.createTextNode(value));
			itemCategory.appendChild (item);
		}
		root.appendChild (itemCategory);
	}
	doc.appendChild (root);

	return (doc);
   }
 
}
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.