Does someone have a class to do this? I'm not thinking
of examining DTD's off the net or trying to validate
the XML contents of a file. I just want something to
read the file in and give me the parsed contents when
I ask for it.
Mike
Thanks for the suggestions. Here is what I wrote last night.
It works with my two test cases... I need to test it more.
---------------------------------
// $Id$
// $Log$
// parse xml into a datastructure
// :!javac % && time java ac.XMLReader test.xml
package ac;
import java.util.*;
import java.io.*;
public class XMLReader {
private final static String REENDTAG = " ENDTAG ";
private final static String REBEGINENDTAG = " BEGINENDTAG ";
private final static String REBEGINTAG = " BEGINTAG ";
private final static String REPROPERTY = " PROPERTY ";
private final static String REENDBRACKET = " ";
private final static String ENDTAG = "ENDTAG";
private final static String BEGINENDTAG = "BEGINENDTAG";
private final static String BEGINTAG = "BEGINTAG";
private final static String PROPERTY = "PROPERTY";
private final static String ENDBRACKET = " ";
private item items[];
// read the contents of the indicated file
// pass the contents to parseString
public void parseFile(String fn) throws IOException {
StringBuffer sb = new StringBuffer();
BufferedReader in = new BufferedReader(new FileReader(fn));
String line;
while((line = in.readLine()) != null) {
sb.append(line);
}
in.close();
parseString(sb.toString());
}
// parse the contents of the String
public void parseString(String s) {
s = s.replaceAll("</", REENDTAG);
s = s.replaceAll("/>", REBEGINENDTAG);
s = s.replaceAll("<", REBEGINTAG);
s = s.replaceAll(">", REENDBRACKET);
s = s.replaceAll("=", REPROPERTY);
String parts[] = s.split("\\s+");
item i = null;
ArrayList al = new ArrayList();
while((i = getItem(parts, 0)) != null) {
al.add(i);
}
items = (item[]) al.toArray(new item[al.size()]);
}
private item getItem(String parts[], int start) {
System.out.println("getItem() starting at " + start);
item item = new item();
item.properties = new Hashtable();
ArrayList al = new ArrayList();
boolean foundFirst = false;
String tag = null;
for(int i = start; i < parts.length; i++) {
String s = parts
;
parts = "";
System.out.println("i=" + i + " s='" + s + "' " + s.length());
if(s.length() == 0) {
System.out.println("continue");
continue;
}
if(s.compareTo(BEGINTAG) == 0 && foundFirst == false) {
foundFirst = true;
tag = parts[++i];
parts = "";
System.out.println("tag=" + tag);
item.name = tag;
} else if(s.compareTo(BEGINTAG) == 0 && foundFirst == true) {
parts = BEGINTAG;
al.add(getItem(parts, i));
} else if(s.compareTo(ENDTAG) == 0) {
if(tag.compareToIgnoreCase(parts[++i]) == 0) {
System.out.println(tag + " = " + parts);
parts = "";
break;
}
} else if(s.compareTo(PROPERTY) == 0) {
String pt = item.value;
item.value = null;
parts[i++] = "";
String v = parts.replaceAll("\"", "");
System.out.println("property: " + pt + "->" + v);
item.properties.put(pt, v);
} else if(s.compareTo(BEGINENDTAG) == 0) {
} else {
item.value = s;
System.out.println("value=" + item.value);
}
}
item.items = (item[]) al.toArray(new item[al.size()]);
if(item.name == null) {
return null;
}
return item;
}
public void list(PrintStream out) {
for(int i = 0; i < items.length; i++) {
listItem(items, out);
}
}
private void listItem(item item, PrintStream out) {
out.println("name/value: " + item.name + "/" + item.value);
out.println("---- properties ----");
for(Enumeration e = item.properties.keys(); e.hasMoreElements(); ) {
String key = (String) e.nextElement();
String val = (String) item.properties.get(key);
out.println(key + "->" + val);
}
out.println("---- sub items -----");
for(int i = 0; i < item.items.length; i++) {
listItem(item.items, out);
}
out.println("---- done ----------");
}
public static void main(String args[]) throws Exception {
XMLReader xml = new XMLReader();
try {
xml.parseFile(args[0]);
xml.list(System.out);
} catch(IOException e) {
e.printStackTrace();
}
}
}
class item {
public String name;
public Hashtable properties;
public item items[];
public String value;
}
class token {
public static final int NONE = 0;
public static final int TAGBEGIN = 1;
public static final int TAGEND = 2;
public static final int PROPERTY = 3;
public int type = NONE;
public String label = null;
public String value = null;
}
---------------------------------