Solved

how to make a Java XML parser ?previous

Posted on 2000-05-17
3
151 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Need Help! Getting a syntax error and don't understand why 3 53
Running JavaFX on the Raspberry Pi 27 270
junit example issue 2 52
dao vs facade design patterns 2 65
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses

739 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