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])));
    }
}
badour_maAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ADSLMarkCommented:
package com.psol.xbe2;

import java.io.*;

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

public class MySAXApp1
    extends DefaultHandler
{
    private final static String[] searchData = {"XML", "HTML", "Java", "SQL"};

    private String prevData;
    private boolean insideTag;

    public MySAXApp1()
    {
        this.prevData = null;
        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).trim();

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

        for(int i=0;i<searchData.length;i++)
        {
            if(data.equalsIgnoreCase(searchData[i]))
            {
                this.prevData = data;
                break;
            }
        }
    }

    //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])));
    }
}

Like i posted in the other thread.
Mark
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ADSLMarkCommented:
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
0
Ajay-SinghCommented:
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());
      }
0
Exploring SharePoint 2016

Explore SharePoint 2016, the web-based, collaborative platform that integrates with Microsoft Office to provide intranets, secure document management, and collaboration so you can develop your online and offline capabilities.

ADSLMarkCommented:
@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...............
0
badour_maAuthor Commented:
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
0
ADSLMarkCommented:
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
0
badour_maAuthor Commented:
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??
0
ADSLMarkCommented:
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
0
badour_maAuthor Commented:
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???
0
ADSLMarkCommented:
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.
0
badour_maAuthor Commented:
>>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
0
ADSLMarkCommented:
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
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.