Solved

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

Posted on 2007-03-29
12
186 Views
Last Modified: 2010-03-31
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])));
    }
}
0
Comment
Question by:badour_ma
  • 7
  • 4
12 Comments
 
LVL 10

Accepted Solution

by:
ADSLMark earned 500 total points
ID: 18820240
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
 
LVL 10

Expert Comment

by:ADSLMark
ID: 18820306
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
 
LVL 23

Expert Comment

by:Ajay-Singh
ID: 18821568
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
 
LVL 10

Expert Comment

by:ADSLMark
ID: 18821949
@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
 

Author Comment

by:badour_ma
ID: 18822160
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
 
LVL 10

Expert Comment

by:ADSLMark
ID: 18822206
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

Author Comment

by:badour_ma
ID: 18822225
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
 
LVL 10

Expert Comment

by:ADSLMark
ID: 18822243
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
 

Author Comment

by:badour_ma
ID: 18822290
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
 
LVL 10

Expert Comment

by:ADSLMark
ID: 18822300
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
 

Author Comment

by:badour_ma
ID: 18822309
>>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
 
LVL 10

Expert Comment

by:ADSLMark
ID: 18822315
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

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

708 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now