Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XML
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
XML General XML discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XML section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old December 24th, 2003, 02:25 AM
Authorized User
 
Join Date: Jul 2003
Location: Kolkata, West Bengal, India.
Posts: 51
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to sankar
Default Java Applet that can read XML DOM and display text

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
__________________
Sankar Sengupta
Striving for the BEST
Reply With Quote
  #2 (permalink)  
Old December 24th, 2003, 08:08 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: Nor Hachin, Kotayk, Armenia.
Posts: 147
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to armmarti
Default

Try this (not tested):

Code:
import java.applet.*;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;


public class ReadXML extends Applet {
  private boolean isStandalone = false;
  private DocumentBuilderFactory dbf;
  private DocumentBuilder db;
  private Document doc;

  //Get a parameter value
  public String getParameter(String key, String def) {
    return isStandalone ? System.getProperty(key, def) :
      (getParameter(key) != null ? getParameter(key) : def);
  }

  //Construct the applet
  public ReadXML() {
  }

  //Initialize the applet
  public void init() {
    try {
      dbf = DocumentBuilderFactory.newInstance();
      db = dbf.newDocumentBuilder();
    }
    catch(Exception e) {
      e.printStackTrace();
    }
  }

  public void start()
  {
    try {
      doc = db.parse(getParameter("xmlDocURI"));
      // here process the document...
    } catch(Exception e) {
      e.printStackTrace();
    }
  }
}
Regards,
Armen
Reply With Quote
  #3 (permalink)  
Old December 25th, 2003, 05:45 AM
Authorized User
 
Join Date: May 2003
Location: New Delhi, Delhi, India.
Posts: 50
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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
Reply With Quote
  #4 (permalink)  
Old February 16th, 2004, 11:16 AM
Registered User
 
Join Date: Feb 2004
Location: Delhi, Delhi, India.
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi expert

I am looking for a Java Applet that can read XML File and display the DOM structure accordingly.

Nitin

Quote:
quote:Originally posted by sankar
 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
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Read, write, delete and display xml file in php rapraj XML 0 December 27th, 2007 02:18 AM
Applet needs full browser pane w/ DOM access. Philibuster BOOK: Beginning Java 2 1 August 15th, 2006 09:22 PM
XML DOM parser in java... recommend, plz sonicDace XML 0 February 3rd, 2004 02:38 PM
display element (layer) over java applet mateenmohd Java GUI 0 January 6th, 2004 12:39 AM
Java Applet that can read an XML file sankar J2EE 1 December 25th, 2003 05:40 AM



All times are GMT -4. The time now is 07:12 AM.


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