badour_ma
asked on
help me in my java code
Hi,
I wrote a java file that parse the XML document using SAX parser. But I want to print the edition date if the publisher is “Oracle Publishing”
So how can I do that in my public void characters (char ch[], int start, int length)??
<?xml version = '1.0' encoding = 'UTF-8'?>
<catalog>
<journal>
<journal_title>Dell</journ al_title>
<publisher>Oracle Publishing</publisher>
<edition>Sept-Oct 2003</edition>
<article_section>XML</arti cle_sectio n>
<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</arti cle_sectio n>
<title>The Active Database</title>
<author> Cameron ORourke </author>
</journal>
</catalog>
package com.psol.xbe2;
import java.io.FileReader;
import java.lang.String;
import org.xml.sax.XMLReader;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.helpers.XMLRea derFactory ;
import org.xml.sax.helpers.Defaul tHandler;
public class MySAXApp1 extends DefaultHandler
{
public static void main (String args[])
throws Exception
{
XMLReader xr = XMLReaderFactory.createXML Reader();
MySAXApp1 handler = new MySAXApp1();
xr.setContentHandler(handl er);
xr.setErrorHandler(handler );
// Parse each file provided on the
// command line.
for (int i = 0; i < args.length; i++) {
FileReader r = new FileReader(args[i]);
xr.parse(new InputSource(r));
}
}
public MySAXApp1 ()
{
super();
}
////////////////////////// ////////// ////////// ////////// ////////// //
// Event handlers.
////////////////////////// ////////// ////////// ////////// ////////// //
public void startDocument ()
{
System.out.println("Start document");
}
public void endDocument ()
{
System.out.println("End document");
}
public void startElement (String uri, String name,
String qName, Attributes atts)
{
//if ("".equals (uri))
// System.out.println("Start element: " + qName);
//else
// System.out.println("Start element: {" + uri + "}" + name);
}
public void endElement (String uri, String name, String qName)
{
// if ("".equals (uri))
// System.out.println("End element: " + qName);
//else
// System.out.println("End element: {" + uri + "}" + name);
}
public void characters (char ch[], int start, int length)
{
String a;
//System.out.print("Charac ters: \"");
a= String.valueOf (ch, start,length);
if (a.equals("Oracle Publishing "))
System.out.print("XXXX"+ a+ "XXXX");
// here I want to print the edtion date- how can I call the element that follow
//System.out.print("\"\n") ;
}
}
I wrote a java file that parse the XML document using SAX parser. But I want to print the edition date if the publisher is “Oracle Publishing”
So how can I do that in my public void characters (char ch[], int start, int length)??
<?xml version = '1.0' encoding = 'UTF-8'?>
<catalog>
<journal>
<journal_title>Dell</journ
<publisher>Oracle Publishing</publisher>
<edition>Sept-Oct 2003</edition>
<article_section>XML</arti
<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</arti
<title>The Active Database</title>
<author> Cameron ORourke </author>
</journal>
</catalog>
package com.psol.xbe2;
import java.io.FileReader;
import java.lang.String;
import org.xml.sax.XMLReader;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.helpers.XMLRea
import org.xml.sax.helpers.Defaul
public class MySAXApp1 extends DefaultHandler
{
public static void main (String args[])
throws Exception
{
XMLReader xr = XMLReaderFactory.createXML
MySAXApp1 handler = new MySAXApp1();
xr.setContentHandler(handl
xr.setErrorHandler(handler
// Parse each file provided on the
// command line.
for (int i = 0; i < args.length; i++) {
FileReader r = new FileReader(args[i]);
xr.parse(new InputSource(r));
}
}
public MySAXApp1 ()
{
super();
}
//////////////////////////
// Event handlers.
//////////////////////////
public void startDocument ()
{
System.out.println("Start document");
}
public void endDocument ()
{
System.out.println("End document");
}
public void startElement (String uri, String name,
String qName, Attributes atts)
{
//if ("".equals (uri))
// System.out.println("Start element: " + qName);
//else
// System.out.println("Start element: {" + uri + "}" + name);
}
public void endElement (String uri, String name, String qName)
{
// if ("".equals (uri))
// System.out.println("End element: " + qName);
//else
// System.out.println("End element: {" + uri + "}" + name);
}
public void characters (char ch[], int start, int length)
{
String a;
//System.out.print("Charac
a= String.valueOf (ch, start,length);
if (a.equals("Oracle Publishing "))
System.out.print("XXXX"+ a+ "XXXX");
// here I want to print the edtion date- how can I call the element that follow
//System.out.print("\"\n")
}
}
ASKER
hi boonleng,
thanks alot but I alrady use ValueOf() to get the string and its works
I am asking how to print the edition data when i find the Oracle Publishing??
thanks alot but I alrady use ValueOf() to get the string and its works
I am asking how to print the edition data when i find the Oracle Publishing??
Sorry, didn't see that you are using ValueOf().
Coz Sax Parser is reading from top to bottom, so you need to put an indicator/flag to tell next element to print the edition date.
Example:
public class MySAXApp1 extends DefaultHandler {
private boolean isOracle = false;
public void endElement (String uri, String name, String qName)
{
if ("edition".equals (qName)) {
isOracle = false;
}
}
public void characters (char ch[], int start, int length)
{
String a;
//System.out.print("Charac ters: \"");
a= String.valueOf (ch, start,length);
if (isOracle) {
// here I want to print the edtion date
}
else if (a.equals("Oracle Publishing ")) {
isOracle = true;
}
}
}
Hope this help.
Coz Sax Parser is reading from top to bottom, so you need to put an indicator/flag to tell next element to print the edition date.
Example:
public class MySAXApp1 extends DefaultHandler {
private boolean isOracle = false;
public void endElement (String uri, String name, String qName)
{
if ("edition".equals (qName)) {
isOracle = false;
}
}
public void characters (char ch[], int start, int length)
{
String a;
//System.out.print("Charac
a= String.valueOf (ch, start,length);
if (isOracle) {
// here I want to print the edtion date
}
else if (a.equals("Oracle Publishing ")) {
isOracle = true;
}
}
}
Hope this help.
This is just a temporary solution, not a very good way of doing it.
If the position of the "edition" element changed, then you will encounter problem.
I would suggest that read the entire XML and store everything into beans/maps, and later loop the list of beans to print the data out.
Another way is to use XPath to read the data, this way you can navigate the XML wherever you want.
If the position of the "edition" element changed, then you will encounter problem.
I would suggest that read the entire XML and store everything into beans/maps, and later loop the list of beans to print the data out.
Another way is to use XPath to read the data, this way you can navigate the XML wherever you want.
ASKER
Hi again boonleng,
thanks for helping me.
who about if I want to print the data of second element after "Oracle Publishing" what ever the element is. so the most important is the position of the element not the element it self.
So how can I print the data of the element fount after Oracle Publishing???
thanks for helping me.
who about if I want to print the data of second element after "Oracle Publishing" what ever the element is. so the most important is the position of the element not the element it self.
So how can I print the data of the element fount after Oracle Publishing???
keep a flag and when you encounter the starting element publisher set the flag to true. do that in startElement (). every time startElement is called, check if the flag is true, if so, you have the next element. do what ever with it and then set the flag back to false.
ASKER
hi Kawas
can u please show me how to do that by code?
can u please show me how to do that by code?
ASKER
hi I wrote this code to set a flage if the elment data is "XML" and to print the data of the second elemnt but I had the following error:
MySAXApp1.java:84: variable x might not have been initialized
if (x==1)
^
but I can not intilize x coz if I do that I will lose the flage value!!!!
public void characters (char ch[], int start, int length)
{
String a;
int x;
//System.out.print("Charac ters: \"");
a= String.valueOf (ch, start,length);
if (x==1)
{
System.out.print("yyy"+ a+ "yyy");
x=0;
}
if (a.equals("XML"))
{
System.out.print("XXXX"+ a+ "XXXX");
x=1;}
//System.out.print("\"\n") ;
}
MySAXApp1.java:84: variable x might not have been initialized
if (x==1)
^
but I can not intilize x coz if I do that I will lose the flage value!!!!
public void characters (char ch[], int start, int length)
{
String a;
int x;
//System.out.print("Charac
a= String.valueOf (ch, start,length);
if (x==1)
{
System.out.print("yyy"+ a+ "yyy");
x=0;
}
if (a.equals("XML"))
{
System.out.print("XXXX"+ a+ "XXXX");
x=1;}
//System.out.print("\"\n")
}
SAX is not appropriate for this - you need DOM. Use XPath
ASKER
hi CEHJ
can u please help me to write the code in DOM. How can I change it??
can u please help me to write the code in DOM. How can I change it??
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
but CEHJ I do not know waht is the elements name I only know that if I find 'Oracle Publishing' as data in one of the elemnts then I have to print the data in the next element???
>>but CEHJ I do not know waht is the elements name
I thought you said it was 'edition'?
I thought you said it was 'edition'?
ASKER
hi CEHJ
I said edtion for example but as I said befor I am looking for certin data 'Oracle Publishing' and when I find it I have to print the next data in the next element. So both I do not know what is the elemnt name??!!
So do I still have to use Xpath??
I said edtion for example but as I said befor I am looking for certin data 'Oracle Publishing' and when I find it I have to print the next data in the next element. So both I do not know what is the elemnt name??!!
So do I still have to use Xpath??
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Example:
public class MySAXApp1 extends DefaultHandler {
private CharArrayWriter contents;
public void startDocument ()
{
contents = new CharArrayWriter();
}
public void startElement (String uri, String name, String qName, Attributes atts)
{
contents.reset();
}
public void endElement (String uri, String name, String qName)
{
String s = contents.toString();
if ("publisher".equals (qName) && "Oracle Publishing".equals(s)) {
// here I want to print the edtion date
}
}
public void characters (char ch[], int start, int length)
{
contents.write(ch, start, length);
}
}