extremely simple xml parser?

M

Mike

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
 
A

Araxes Tharsis

Simply use SAX... it is very simple... here it is an Echo Parser:

/*
* SAXEcho.java
*
* Created on 9 de Maio de 2004, 22:28
*/

/**
*
* @author UAL - LPP2
*/

import java.io.*;
import org.xml.sax.*;
import org.xml.sax.helpers.DefaultHandler;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;


public class SAXEcho extends DefaultHandler
{
public void startElement(String namespaceURI,
String sName,
String qName,
Attributes attrs
) throws SAXException
{
System.out.println("Element start:" + qName);

// Mostra os atributos do elemento
if (attrs != null)
{
for(int i=0; i<attrs.getLength(); i++)
{
System.out.println("Attribute: " + attrs.getQName(i) + " - "
+ attrs.getValue(i));
}
}
}

public void endElement(String uri,
String localName,
String qName
) throws SAXException
{
System.out.println("Element end:" + qName);
}

public void characters(char buf[], int offset, int len) throws
SAXException
{
String s = new String(buf, offset, len);
s = s.trim();

if (!s.equals(""))
{
System.out.println("Value: " + s);
}
}

public final void startDocument() throws SAXException
{
System.out.println("Inicio do documento.");
}

public final void endDocument() throws SAXException
{
System.out.println("Fim do documento.");
}

public static void main(String[] args)
{
// Ficheiro com o documento XML a ler
File f = new File("Employees-NWind.xml");

// Objecto criado a partir da própria classe vai servir de SAX
Handler
DefaultHandler handler = new SAXEcho();

// É utilizado o default parser (que é um non-validating parser)
SAXParserFactory factory = SAXParserFactory.newInstance();

try
{
// Cria o parser
SAXParser saxParser = factory.newSAXParser();

// Inicia o parsing
saxParser.parse(f, handler);
}
catch(Throwable t)
{
System.out.println(t);
}
}
}
 
M

Mike

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;
}
---------------------------------
 
M

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.

Just remembered that I didn't finish last night. Though the
parsing works with my test cases, I still need to add methods
for retrieving the data and possibly for writing the data
back to an xml file.

Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,169
Latest member
ArturoOlne
Top