p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   J2EE (http://p2p.wrox.com/forumdisplay.php?f=94)
-   -   Java Applet that can read an XML file (http://p2p.wrox.com/showthread.php?t=7608)

sankar December 24th, 2003 02:29 AM

Java Applet that can read an XML file
 
Hi experts,
I am looking for a Java Applet that can read an XML file and display the text according to some specific terms (e.g., if Title, display in bold,if link, display as well as provide the link, etc). I only need the framework to read the XML file. Rest I'll do.
Any help in this regard is appreciated.
Thx


Sankar Sengupta

Yashraj Chauhan December 25th, 2003 05:40 AM

HI
Following is the code for XML parsing. The code is an application which take xml file name as command line parameter. I am using XERCES xml parser. I hope this helps u can get back in case of any queries.

import java.io.IOException;
import org.xml.sax.SAXException;

import org.xml.sax.XMLReader;
import org.apache.xerces.parsers.SAXParser;
import org.xml.sax.Locator;
import org.xml.sax.ContentHandler;
import org.xml.sax.Attributes;
public class TestXml
{
    public void performDemo(String uri)
    {
        System.out.println("parsing XML File :" + uri + "\n\n");
        ContentHandler content = new MyContentHandler();
        try
        {
            XMLReader parser = (XMLReader)new SAXParser();
            parser.setContentHandler(content);
            parser.parse(uri);
        }
        catch(IOException ex)
        {
            System.out.println("Error reading URI:" + ex.getMessage());
        }
        catch(SAXException sx)
        {
            System.out.println("Error in parsing:" + sx.getMessage());
        }

    }

    public static void main(String[] args)
    {
        if(args.length != 1)
        {
            System.out.println("Usage : java TestXml [XML URI]");
            System.exit(0);
        }
        String uri = args[0];
        TestXml test = new TestXml();
        test.performDemo(uri);
    }
}

class MyContentHandler implements ContentHandler
{
    private Locator locator;

    public void setDocumentLocator(Locator locator)
    {
        System.out.println(" * setDocumentLocator() called");
        this.locator = locator;
    }

    public void startDocument() throws SAXException
    {
        System.out.println("Parsing Begins.....");
    }

    public void endDocument() throws SAXException
    {
        System.out.println(".........Parsing Ends");
    }

    public void processingInstruction(String target, String data) throws SAXException
    {
        System.out.println("PI: Target:" + target + "and Data: " + data);
    }

    public void startPrefixMapping(String prefix, String uri)
    {
        System.out.println("Mapping starts for prefix " + prefix + " mapped to URI " + uri);
    }

    public void endPrefixMapping(String prefix)
    {
        System.out.println("Mapping ends for prefix " + prefix);
    }

    public void startElement(String namespaceURI, String localName, String rawName, Attributes atts) throws SAXException
    {
        System.out.println("StartElement: " + localName);
        if(!namespaceURI.equals(""))
        {
            System.out.println("in namespace " + namespaceURI + "( " + rawName + " )");
        }
        else
        {
            System.out.println("has no associated namespace");
        }
        for(int i=0; i<atts.getLength();i++)
        {
            System.out.println("Attribute: " + atts.getLocalName(i) + "=" + atts.getValue(i));
        }
    }

    public void endElement(String namespaceURI, String localName, String rawName) throws SAXException
    {
        System.out.println("endElement: " + localName + "\n");
    }

    public void characters(char[] ch, int start, int end) throws SAXException
    {
        String s = new String(ch, start, end);
        System.out.println("characters: " + s);
    }

    public void ignorableWhitespace(char[] ch, int start, int end) throws SAXException
    {
        String s = new String(ch, start, end);
        System.out.println("ignorableWhitespace: [" + s +" ]");

    }

    public void skippedEntity(String name) throws SAXException
    {
        System.out.println("SkippedEntity " + name);
    }
}

Regards



Yashraj Chauhan
Java\J2EE Specialist
Wiley Support Team


All times are GMT -4. The time now is 01:53 PM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.