Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / June 2004

Tip: Looking for answers? Try searching our database.

extremely simple xml parser?

Thread view: 
Mike - 17 Jun 2004 20:56 GMT
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
Araxes Tharsis - 17 Jun 2004 23:44 GMT
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);
       }
   }
}
someone - 18 Jun 2004 00:03 GMT
> Does someone have a class to do this? I'm not thinking
> of examining DTD's off the net or trying to validate
[quoted text clipped - 3 lines]
>
> Mike

nanoxml ?
<http://nanoxml.cyberelf.be/
Mike - 18 Jun 2004 12:55 GMT
> Does someone have a class to do this? I'm not thinking
> of examining DTD's off the net or trying to validate
[quoted text clipped - 3 lines]
>
> 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;
}
---------------------------------
Mike - 18 Jun 2004 13:40 GMT
>> Does someone have a class to do this? I'm not thinking
>> of examining DTD's off the net or trying to validate
[quoted text clipped - 6 lines]
> 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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.