Solved

how to make a Java XML parser ?previous

Posted on 2000-05-17
3
144 Views
Last Modified: 2011-04-14
please go to :-
http://www1.experts-exchange.com/Computers/Programming/Languages/Java/Q_10340380.html

for the question. It aint closed yet... and I still need help!
0
Comment
Question by:chaos50
  • 2
3 Comments
 
LVL 1

Accepted Solution

by:
karthicraja earned 0 total points
ID: 2816597

EXAMPLE.
 
<?xml version="1.0" encoding="UTF-8"?>
<rulefile>
      <field name="page">
            <start>images/rightarrow.gif</start>
            <end>Show next</end>
      </field>
      <field name="url">
            <start>Position Title:</start>
            <end>&gt;</end>
      </field>
      <field Name="nextpage">
            <start>images/rightarrow.gif</start>
            <end>Show next</end>
      </field>
</rulefile>


// THIS CODE WILL GIVE YOU AN IDEA ABOUT HOW TO PROCEED IN, NOT THE EXACT SOLUTION FOR YOUR QUESTION


public class XMLExtractor
{

    private Hashtable fields = new Hashtable();

    private XMLDocument doc;

    public XMLExtractor()
    {
        fields = new Hashtable();
    }
   
    public void configureSettings(String pstrFileName) throws Exception
    {

        DOMParser parser = new DOMParser();
       
        URL url = URLCreator.getURL(pstrFileName); // given the file name gives you the URL like file:/d:/abc.xml

        parser.setErrorStream(System.err);

        parser.setValidationMode(false);
       
        parser.showWarnings(true);

        parser.parse(url);

        doc = parser.getDocument();
    }

    public void validate(Object obj)
    {
        if(obj == null)
        {
            System.out.println("Initialisation of XML object failure");
           
            System.exit(0);
        }
    }

    // THIS FUNCTION EXTRACTS THE FIELDS IN THE ABOVE XML IT IS "field" TAG


    public void getElements(String tagName)
    {

        NodeList list = doc.getElementsByTagName(tagName);
       
        int len = list.getLength();
       
        for (int i=0; i < len; i++)
        {

            Element e = (Element)list.item(i);

            NamedNodeMap nodeMap = e.getAttributes();

            if (nodeMap != null)
            {

                for (int j=0; j<nodeMap.getLength(); j++)
                {

                    Node n = nodeMap.item(j);

                    String  attName = n.getNodeValue();

                    fields.put(attName,e);

                }
            }
        }
    }

    // THIS FUNCTION EXTRACTS THE ATTRIBUTES. LIKE HERE IN THE XML, IMAGES\RIGHTARROW.GIF

    public SearchTag getValue(String pstrAttribute)
    {

        // THIS IS THE OBJECT WHERE YOU ARE GOING TO STORE YOUR VALUES, IT ALL DEPENDS ON YOU TO FRAME THIS.

        SearchTag stag = new SearchTag();
       
        Element e = (Element) fields.get(pstrAttribute);

        validate(e);

        NodeList list = e.getChildNodes();
       
        if(list != null)
        {
            stag.setLength( list.getLength()-1);

            for(int i=0;i<list.getLength(); i++)
            {
                NodeList n2 = list.item(i).getChildNodes();

                if(n2 != null)
                {
                    if(list.item(i).getNodeName().compareToIgnoreCase("start") == 0)
                    {
                        stag.setStartTag( n2.item(0).getNodeValue() );
                    }
                   
                    else
                       
                        stag.setEndTag( n2.item(0).getNodeValue() );
                }
            }
        }
        return stag;
    }

    static public void main(String[] argv)
    {
        try
        {
            Hashtable fields = new Hashtable();
           
            XMLExtractor a = new XMLExtractor();
           
            a.configureSettings("123.xml");
           
            a.getElements("fields");
           
            a.getValue("page");
           
            a.getValue("url");
           
            a.getValue("nextpage");
           
        }
        catch (Exception e)
        {
           
            System.out.println(e.toString());
           
        }
    }
}



0
 

Author Comment

by:chaos50
ID: 2894008
Answer accepted
0
 

Author Comment

by:chaos50
ID: 2894009
Sorry, its not worth any thing but I gave the points in the other question to techtime.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now