Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > extremely simple xml parser?

Reply
Thread Tools

extremely simple xml parser?

 
 
Mike
Guest
Posts: n/a
 
      06-17-2004
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
 
Reply With Quote
 
 
 
 
Araxes Tharsis
Guest
Posts: n/a
 
      06-17-2004
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);
}
}
}







 
Reply With Quote
 
 
 
 
someone
Guest
Posts: n/a
 
      06-17-2004
In article <(E-Mail Removed)>,
Mike <(E-Mail Removed)> wrote:

> 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


nanoxml ?
<http://nanoxml.cyberelf.be/>
 
Reply With Quote
 
Mike
Guest
Posts: n/a
 
      06-18-2004
In article <(E-Mail Removed)>, Mike wrote:
> 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[i];
parts[i] = "";
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[i] = "";
System.out.println("tag=" + tag);
item.name = tag;
} else if(s.compareTo(BEGINTAG) == 0 && foundFirst == true) {
parts[i] = BEGINTAG;
al.add(getItem(parts, i));
} else if(s.compareTo(ENDTAG) == 0) {
if(tag.compareToIgnoreCase(parts[++i]) == 0) {
System.out.println(tag + " = " + parts[i]);
parts[i] = "";
break;
}
} else if(s.compareTo(PROPERTY) == 0) {
String pt = item.value;
item.value = null;
parts[i++] = "";
String v = parts[i].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[i], 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[i], 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;
}
---------------------------------
 
Reply With Quote
 
Mike
Guest
Posts: n/a
 
      06-18-2004
In article <(E-Mail Removed)>, Mike wrote:
> In article <(E-Mail Removed)>, Mike wrote:
>> 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.
>


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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Different results parsing a XML file with XML::Simple (XML::Sax vs. XML::Parser) Erik Wasser Perl Misc 5 03-05-2006 10:09 PM
Extremely simple XML loquak Java 5 04-12-2004 03:27 PM
ANNOUNCE: xsdb -- the eXtremely Simple Database goes alpha Aaron Watters Python 26 01-06-2004 02:31 PM
Extremely Simple Question Mike Lundell C++ 10 07-30-2003 07:59 PM
extremely simple question Mike Lundell C++ 1 07-30-2003 12:56 PM



Advertisments