Link to home
Start Free TrialLog in
Avatar of badour_ma
badour_ma

asked on

how to search for many data in one XML document??

in this code I want to I wnat to search for 15 data in one XML doucemnt !!!
ex.
XML
Java
c
c++
...
...
so it is not logical to use 15 flag!!!
what can I do???


package com.psol.xbe2;

import java.io.*;

import org.xml.sax.*;
import org.xml.sax.helpers.*;

public class MySAXApp1
    extends DefaultHandler
{
    private boolean foundXML;
    private boolean insideTag;

    public MySAXApp1()
    {
        this.foundXML = false;
        this.insideTag = false;
    }

    public void startElement(String uri, String name, String qName, Attributes atts)
    {
        this.insideTag = true;
    }

    public void endElement(String uri, String name, String qName)
    {
        this.insideTag = false;
    }

    public void characters(char ch[], int start, int length)
    {
        String data = String.valueOf(ch, start, length);

        if(this.insideTag && this.foundXML)
        {
            System.out.print("Data in next tag after founding 'XML': "+data);
            this.foundXML = false;
        }

        if(data.equals("XML"))
            this.foundXML = true;
    }

    //Program entry
    public static void main (String args[])
        throws Exception
    {
        MySAXApp1 handler = new MySAXApp1();

        // Create xml reader
        XMLReader xr = XMLReaderFactory.createXMLReader();
        xr.setContentHandler(handler);
        xr.setErrorHandler(handler);

        // Parse each file provided on the command line.
        for(int i=0;i<args.length;i++)
            xr.parse(new InputSource(new FileReader(args[i])));
    }
}
ASKER CERTIFIED SOLUTION
Avatar of ADSLMark
ADSLMark

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ADSLMark
ADSLMark

Whoops, just noticed a little pretty printing error:

System.out.print("Data in next tag after founding '"+prevData+"': "+data);

should be:

System.out.println("Data in next tag after founding '"+prevData+"': "+data);

otherwise everything gets printed after eachother.. not that it really matters since you probably are going to adjust it anyway, but still :-)
Mark
Another version (You don't need flag to check if characters is called when inside tag is true - characters is ALWAYS called when its inside tag):
 
public class MySAXApp1 extends DefaultHandler {

      private List<String>      m_list;

      public MySAXApp1(List<String> list) {
            m_list = list;
      }


      public void characters(char ch[], int start, int length) {
            if (m_list.isEmpty())
                  return;

            String data = String.valueOf(ch, start, length);
            int index = m_list.indexOf(data);
            if (index < 0)
                  return;
            m_list.remove(index);
      }

      //Program entry
      public static void main(String args[]) throws Exception {
            List<String> list = new ArrayList<String>(Arrays.asList(new String[]{
                        "XML", "Java", "a"
            }));
            MySAXApp1 handler = new MySAXApp1(list);

            // Create xml reader
            XMLReader xr = XMLReaderFactory.createXMLReader();
            xr.setContentHandler(handler);
            xr.setErrorHandler(handler);

            // Parse each file provided on the command line.
            xr.parse(new InputSource(new FileReader(args[0])));
            System.out.println("Found all? " + list.isEmpty());
      }
@Ajay-Singh: that's weird, my compiler DOES print whitespace if I compile this:

import java.io.*;

import org.xml.sax.*;
import org.xml.sax.helpers.*;

public class MySAXApp1
    extends DefaultHandler
{
    public void characters(char ch[], int start, int length)
    {
        String data = String.valueOf(ch, start, length);
        System.out.println("\""+data+"\"");
    }

    //Program entry
    public static void main (String args[])
        throws Exception
    {
        MySAXApp1 handler = new MySAXApp1();

        // Create xml reader
        XMLReader xr = XMLReaderFactory.createXMLReader();
        xr.setContentHandler(handler);
        xr.setErrorHandler(handler);
        xr.parse(new InputSource(new FileReader(args[0])));
    }
}

Notice that you have a document which looks like:
<?xml version = '1.0' encoding = 'UTF-8'?>
<catalog>
    <journal>
        <journal_title>Open:</journal_title>
        <publisher>Oracle Publishing</publisher>
        <edition>Sept-Oct 2003</edition>
        <article_section>XML</article_section>
        <title>Parsing XML Efficiently</title>
        <author>Julie Basu</author>
    </journal>
    <journal>
        <journal_title>Oracle Magazine</journal_title>
        <publisher>Oracle Publishing</publisher>
        <edition>Nov-Dec 2003</edition>
        <article_section>SQL</article_section>
        <title>The Active Database</title>
        <author> Cameron ORourke </author>
    </journal>
</catalog>

Mark
PS: your version doesn't do what is asked. It just find those strings, but doesn't return the data in the *next* tag...............
Avatar of badour_ma

ASKER

HI MArk
1-also I want to search for a date  in the XML document. So any date appear I have to print it and I do not the format of the date and after I get it I have to change it to sepacial format??
2- How can I make the search not a case sensetive ex XML==Xml==xml??

thanks again
if(data.equalsIgnoreCase(searchData[i]))
is already case insensitive.

You want to search for a date.. but you do not know how the date looks like? Hmm, sounds like you need a regular expression for that.. are all date formats possible? Or just a few?

Mark
really I do not know how many date format are possible ??
but I want if I find a date i insert it in a database??
Ok, so basically you want to search for something but you do not know how it looks like.. and that something must be formatted in such a way that you can put it in a database. Well that sounds vague, doesn't it?
Dates can have all sorts of formats like:
2007-3-30 or
2007-30-3 or
30-3-2007 or
2007 3 30 or
2007 30 3 or
30 3 2007 or
2007, march 30 or
2007, 30 march or
30 march 2007 etc. etc.

So i guess you need to restrict yourself here. For example how would you know the difference between 2007 1 2 and 2007 1 2, where the first is januari 2 and the second is februari 1.

Mark
you are right!!
ok lets stick to the system format. So  Iwill get the date from the system not from the xml document.
 so how to do that???
Date today = new Date();
Use a SimpleDateFormat to format the date into sth else.

If you are going to put the date in a database, well then you have to look at PreparedStatement class and how you can do that.
>>If you are going to put the date in a database, well then you have to look at PreparedStatement class and how you can do that.

what u mean
http://java.sun.com/j2se/1.4.2/docs/api/java/sql/PreparedStatement.html

You build your SQL query with ?-marks inside and then you use setXXX to set these question marks to something useful. Search the internet for examples, there are lots.

Mark