extremely simple xml parser?

Discussion in 'Java' started by Mike, Jun 17, 2004.

  1. Mike

    Mike Guest

    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
     
    Mike, Jun 17, 2004
    #1
    1. Advertising

  2. 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);
    }
    }
    }
     
    Araxes Tharsis, Jun 17, 2004
    #2
    1. Advertising

  3. Mike

    someone Guest

    In article <>,
    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


    nanoxml ?
    <http://nanoxml.cyberelf.be/>
     
    someone, Jun 18, 2004
    #3
  4. Mike

    Mike Guest

    In article <>, 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;
    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;
    }
    ---------------------------------
     
    Mike, Jun 18, 2004
    #4
  5. Mike

    Mike Guest

    In article <>, Mike wrote:
    > In article <>, 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
     
    Mike, Jun 18, 2004
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. loquak

    Extremely simple XML

    loquak, Apr 11, 2004, in forum: Java
    Replies:
    5
    Views:
    427
    Sunwest Technologies
    Apr 12, 2004
  2. arne
    Replies:
    0
    Views:
    370
  3. Erik Wasser
    Replies:
    5
    Views:
    482
    Peter J. Holzer
    Mar 5, 2006
  4. Sean
    Replies:
    3
    Views:
    303
    robic0
    Oct 3, 2006
  5. Sean
    Replies:
    0
    Views:
    382
Loading...

Share This Page