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
Solved

how to make a Java XML parser ?previous

Posted on 2000-05-17
3
149 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
eclipse shortcuts 9 62
Java: How do I open the default windows program for a given file type 3 39
Bot application - advice 3 37
running on tomcat not jboss eap 7.0 3 17
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

791 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