• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 158
  • Last Modified:

how to make a Java XML parser ?previous

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
chaos50
Asked:
chaos50
  • 2
1 Solution
 
karthicrajaCommented:

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
 
chaos50Author Commented:
Answer accepted
0
 
chaos50Author Commented:
Sorry, its not worth any thing but I gave the points in the other question to techtime.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now